1

我已经能够成功实现搜索小部件体验,以使用返回带有行数据的游标的内容提供程序。自定义搜索建议按预期很好地显示在 ActionBar 搜索框下的列表中。

我需要做的是将选定的搜索建议发送到自定义活动(大概在捆绑中?)它看起来很简单,但我无法弄清楚。

目前,此代码将询问我想使用什么应用程序来打开意图。我想将选定的建议数据发送到下面列出的清单中的“MainActivity”。

提前致谢!

可搜索的.xml

<?xml version="1.0" encoding="utf-8"?>

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint"
    android:searchSuggestAuthority="com.myapp.SearchProvider"
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:searchSuggestThreshold="2"
    android:searchMode="queryRewriteFromText" >


</searchable>

搜索活动

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);


        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            // Handle the normal search query case
            android.util.Log.w("****", "in ACTION_SEARCH");
            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)
            android.util.Log.w("****", "in ACTION_VIEW");
            doView(intent);
        }
    }

    private void doSearch(String query) {

        android.util.Log.w("search query:", query);

    }

    private void doView(final Intent queryIntent) {
        Uri uri = queryIntent.getData();
        String action = queryIntent.getAction();
        Intent i = new Intent(action);
        i.setData(uri);
        startActivity(i);
        this.finish();
    }
}

清单的搜索部分:

    <activity
        android:name="com.myapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

        <activity android:name="com.myapp.SearchableActivity" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data android:name="android.app.searchable"
                       android:resource="@xml/searchable"/>
        </activity>

        <provider android:authorities="com.myapp.SearchProvider" 
            android:name="com.myapp.SearchProvider" />

         <meta-data android:name="android.app.default_searchable" 
             android:value="com.myapp.SearchableActivity" />
4

1 回答 1

4

呸,我想通了。

在我的内容提供程序中,我必须在 Matrix 光标中添加一个名为:的列,SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA 并输入建议的值。

在我的搜索活动 doView() 方法中,我可以使用以下方法提取它:

Bundle extras = queryIntent.getExtras();
String data = extras.getString(SearchManager.EXTRA_DATA_KEY);
android.util.Log.w("keySet =", extras.keySet().toString()); // this showed me the keys available
android.util.Log.w(SearchManager.EXTRA_DATA_KEY, data);

从这里,我可以将其传递给我的自定义活动/意图。可能有更好的方法,但这有效!

于 2013-03-12T19:02:08.447 回答