[SearchInvoke.java]
Button mStartSearch;
Spinner mMenuMode;
EditText mQueryPrefill;
EditText mQueryAppData;
final static int MENUMODE_SEARCH_KEY = 0;
final static int MENUMODE_MENU_ITEM = 1;
final static int MENUMODE_TYPE_TO_SEARCH = 2;
final static int MENUMODE_DISABLED = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate our UI from its XML layout description.
setContentView(R.layout.search_invoke);
// Get display items for later interaction
mStartSearch = (Button) findViewById(R.id.btn_start_search);
mMenuMode = (Spinner) findViewById(R.id.spinner_menu_mode);
mQueryPrefill = (EditText) findViewById(R.id.txt_query_prefill);
mQueryAppData = (EditText) findViewById(R.id.txt_query_appdata);
// Populate items
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.search_menuModes, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mMenuMode.setAdapter(adapter);
// Create listener for the menu mode dropdown. We use this to demonstrate control
// of the default keys handler in every Activity. More typically, you will simply set
// the default key mode in your activity's onCreate() handler.
mMenuMode.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
if (position == MENUMODE_TYPE_TO_SEARCH) {
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
} else {
setDefaultKeyMode(DEFAULT_KEYS_DISABLE);
}
}
public void onNothingSelected(AdapterView<?> parent) {
setDefaultKeyMode(DEFAULT_KEYS_DISABLE);
}
});
// Attach actions to buttons
mStartSearch.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
onSearchRequested();
}
});
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem item;
// first, get rid of our menus (if any)
menu.removeItem(0);
menu.removeItem(1);
// next, add back item(s) based on current menu mode
switch (mMenuMode.getSelectedItemPosition())
{
case MENUMODE_SEARCH_KEY:
item = menu.add( 0, 0, 0, "(Search Key)");
break;
case MENUMODE_MENU_ITEM:
item = menu.add( 0, 0, 0, "Search");
item.setAlphabeticShortcut(SearchManager.MENU_KEY);
break;
case MENUMODE_TYPE_TO_SEARCH:
item = menu.add( 0, 0, 0, "(Type-To-Search)");
break;
case MENUMODE_DISABLED:
item = menu.add( 0, 0, 0, "(Disabled)");
break;
}
item = menu.add(0, 1, 0, "Clear History");
return true;
}
/** Handle the menu item selections */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
switch (mMenuMode.getSelectedItemPosition()) {
case MENUMODE_SEARCH_KEY:
new AlertDialog.Builder(this)
.setMessage("To invoke search, dismiss this dialog and press the search key" +
" (F5 on the simulator).")
.setPositiveButton("OK", null)
.show();
break;
case MENUMODE_MENU_ITEM:
onSearchRequested();
break;
case MENUMODE_TYPE_TO_SEARCH:
new AlertDialog.Builder(this)
.setMessage("To invoke search, dismiss this dialog and start typing.")
.setPositiveButton("OK", null)
.show();
break;
case MENUMODE_DISABLED:
new AlertDialog.Builder(this)
.setMessage("You have disabled search.")
.setPositiveButton("OK", null)
.show();
break;
}
break;
case 1:
clearSearchHistory();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onSearchRequested() {
// If your application absolutely must disable search, do it here.
if (mMenuMode.getSelectedItemPosition() == MENUMODE_DISABLED) {
return false;
}
final String queryPrefill = mQueryPrefill.getText().toString();
Bundle appDataBundle = null;
final String queryAppDataString = mQueryAppData.getText().toString();
if (queryAppDataString != null) {
appDataBundle = new Bundle();
appDataBundle.putString("demo_key", queryAppDataString);
}
// Now call the Activity member function that invokes the Search Manager UI.
startSearch(queryPrefill, false, appDataBundle, false);
// Returning true indicates that we did launch the search, instead of blocking it.
return true;
}
private void clearSearchHistory() {
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.clearHistory();
}
}
[SearchQueryResoults.java]
TextView mQueryText;
TextView mAppDataText;
TextView mDeliveredByText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate our UI from its XML layout description.
setContentView(R.layout.search_query_results);
// Get active display items for later updates
mQueryText = (TextView) findViewById(R.id.txt_query);
mAppDataText = (TextView) findViewById(R.id.txt_appdata);
mDeliveredByText = (TextView) findViewById(R.id.txt_deliveredby);
// get and process search query here
final Intent queryIntent = getIntent();
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction)) {
doSearchQuery(queryIntent, "onCreate()");
}
else {
mDeliveredByText.setText("onCreate(), but no ACTION_SEARCH intent");
}
}
@Override
public void onNewIntent(final Intent newIntent) {
super.onNewIntent(newIntent);
// get and process search query here
final Intent queryIntent = getIntent();
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction)) {
doSearchQuery(queryIntent, "onNewIntent()");
}
else {
mDeliveredByText.setText("onNewIntent(), but no ACTION_SEARCH intent");
}
}
private void doSearchQuery(final Intent queryIntent, final String entryPoint) {
// The search query is provided as an "extra" string in the query intent
final String queryString = queryIntent.getStringExtra(SearchManager.QUERY);
mQueryText.setText(queryString);
// Record the query string in the recent queries suggestions provider.
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.saveRecentQuery(queryString, null);
// If your application provides context data for its searches,
// you will receive it as an "extra" bundle in the query intent.
// The bundle can contain any number of elements, using any number of keys;
// For this Api Demo we're just using a single string, stored using "demo key".
final Bundle appData = queryIntent.getBundleExtra(SearchManager.APP_DATA);
if (appData == null) {
mAppDataText.setText("<no app data bundle>");
}
if (appData != null) {
String testStr = appData.getString("demo_key");
mAppDataText.setText((testStr == null) ? "<no app data>" : testStr);
}
// Report the method by which we were called.
mDeliveredByText.setText(entryPoint);
}
}
[arrays.xml]
<string-array name="search_menuModes">
<item>Search Key</item>
<item>Menu Item</item>
<item>Type-To-Search</item>
<item>Disabled</item>
</string-array>
</resources>
'old > API_Demo' 카테고리의 다른 글
APP_Service_LocalServiceBinding (0) | 2010.03.25 |
---|---|
APP_Service_ForegroundServiceController (0) | 2010.03.24 |
APP_Preferences_PreferencesFromCode (0) | 2010.03.24 |
APP_Preferences_DefaultValues (0) | 2010.03.24 |
APP_PreferenceDependencies (0) | 2010.03.24 |