android - Adding Custom Suggestions

The default behavior is for system to pass this URI and append it with the query text. For example:

content://your.authority/optional.suggest.path/SUGGEST_URI_PATH_QUERY/puppies
Instead of using the URI, you might decide it makes more sense for your  query()  method to receive everything it needs to perform the look-up and you want the  selection  and  selectionArgs  parameters to carry the appropriate values.

Declaring an Intent for Suggestions


When the user selects a suggestion from the list that appears below the search dialog or widget, the system sends a custom Intent to your searchable activity. You must define the action and data for the intent.

> Note : If you do not include the  android:searchSuggestIntentAction  attribute in your searchable configuration, then you  must  include a value in the  SUGGEST_COLUMN_INTENT_ACTION  column for every suggestion, or the intent will fail.

>You can define the data included with the intent in two ways:

  1. Define the data for each suggestion inside the SUGGEST_COLUMN_INTENT_DATA column of your suggestions table.

b.Fragment a data URI into two pieces: the portion common to all suggestions and the portion unique to each suggestion. Place these parts into the android:searchSuggestintentData attribute of the searchable configuration and the SUGGEST_COLUMN_INTENT_DATA_ID column of your suggestions table, respectively.

Declare the piece of the URI that is common to all suggestions in the android:searchSuggestIntentDataattribute of your searchable configuration.

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_label"
    android:hint="@string/search_hint"
    android:searchSuggestAuthority="com.example.MyCustomSuggestionProvider"
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:searchSuggestIntentData="content://com.example/datatable" 
android:searchSettingsDescription="@string/search_description" >
</searchable>
> Here's an example of how you can handle the intents during your activity  onCreate() callback:
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
    // Handle the normal search query case
    String query = intent.getStringExtra(SearchManager.QUERY);
    doSearch(query);
} else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
    // Handle a suggestions click (because the suggestions all use ACTION_VIEW)
    Uri data = intent.getData();
    showResult(data);
}

Note: You do not need to add an intent filter to your Android manifest file for the intent action you defined with the android:searchSuggestIntentAction attribute or SUGGEST_COLUMN_INTENT_ACTION column. The system opens your searchable activity by name to deliver the suggestion's intent, so the activity does not need to declare the accepted action.

You can rewrite the query text in the following ways:

  1. Add the android:searchMode attribute to your searchable configuration with the "queryRewriteFromText" value. In this case, the content from the suggestion's SUGGEST_COLUMN_TEXT_1 column is used to rewrite the query text.
  2. Add the android:searchMode attribute to your searchable configuration with the "queryRewriteFromData" value. In this case, the content from the suggestion's SUGGEST_COLUMN_INTENT_DATA column is used to rewrite the query text. This should only be used with URI's or other data formats that are intended to be user-visible, such as HTTP URLs. Internal URI schemes should not be used to rewrite the query in this way.
  3. Provide a unique query text string in the SUGGEST_COLUMN_QUERY column of your suggestions table. If this column is present and contains a value for the current suggestion, it is used to rewrite the query text (and override either of the previous implementations).

>The only scenario in which additional work is necessary is when your content provider demands a read permission. In which case, you need to add a special <path-permission> element for the provider to grant Quick Search Box read access to your content provider. For example:

<provider android:name="MySuggestionProvider"
          android:authorities="com.example.MyCustomSuggestionProvider"
          android:readPermission="com.example.provider.READ_MY_DATA"
          android:writePermission="com.example.provider.WRITE_MY_DATA">
  <path-permission android:pathPrefix="/search_suggest_query"
                   android:readPermission="android.permission.GLOBAL_SEARCH" />
</provider>

>Suggestions that the user selects from Quick Search Box can be automatically made into shortcuts. These are suggestions that the system has copied from your content provider so it can quickly access the suggestion without the need to re-query your content provider.

>Using this column, you can configure the shortcut behavior for each suggestion in one of the following ways:

a.Have Quick Search Box re-query your content provider for a fresh version of the suggestion shortcut.

b.Prevent the suggestion from being copied into a shortcut at all.

c.Allow the default shortcut behavior to apply.

Note: Quick Search Box ultimately decides whether or not to create a shortcut for a suggestion, considering these values as a strong request from your application—there is no guarantee that the behavior you have requested for your suggestion shortcuts will be honored.

Note: Quick Search Box ultimately decides whether or not to create a shortcut for a suggestion, considering these values as a strong request from your application—there is no guarantee that the behavior you have requested for your suggestion shortcuts will be honored.

There is no guarantee about how your suggestions are ranked, or whether your app's suggestions show at all for a given query. In general, you can expect that providing quality results increases the likelihood that your app's suggestions are provided in a prominent position and apps that provide low quality suggestions are more likely to be ranked lower or not displayed.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值