如何让我的应用在Google即时显示"手机/平板电脑搜索"页

时间:2014-05-22 17:40:28

标签: android android-contentprovider google-now

我一直在尝试从我的应用中获取信息,以便在Google即时搜索页面中显示。我似乎设置了搜索建议,我甚至看到我的平板电脑上的平板电脑搜索时执行了我的提供商。但没有任何表现。

清单

    <activity
        android:name=".SearchActivity"
        android:label="@string/app_name"
    >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>
        <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
    </activity>
    <activity
        android:name=".SearchViewActivity"
        android:label="@string/app_name"
    >
        <meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" />
    </activity>
    <provider
        android:authorities=".MyContentProvider"
        android:name="org.test.MyContentProvider"
        android:exported="true"
        android:label="@string/app_name"
        android:logo="@drawable/ic_launcher"
    />

searchable.xml

<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/lbl_apptricity_search"
android:hint="@string/hnt_search"
android:searchSuggestAuthority="com.apptricity.asset.provider.AssetContentProvider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:includeInGlobalSearch="true" />

MyContentProvider .java

public class MyContentProvider extends ContentProvider {
@Override
public boolean onCreate() {
    Log.d("MyApp", "Provider: onCreate()");
    return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    try {
        Log.d("MyApp", "Provider: query(" + uri + ", " + projection + ", " + selection + ", " + (selectionArgs == null ? "null" : Arrays.asList(selectionArgs)) + ", " + sortOrder + ")");
        String uriTest = uri.toString();

        if (uriTest.startsWith("content://org.test.MyContentProvider/search_suggest_query/")) {
            int limit = 10;

            String query = uriTest.substring(82);
            Log.d("MyApp", "Provider: query: " + query);
            int index = query.indexOf('?');

            if (index != -1) {
                query = query.substring(0, index);
                Log.d("MyApp", "Provider: query: " + query);
            }

            query = StaticCore.urlDecode(query);
            Log.d("MyApp", "Provider: query: " + query);

            SQLiteDatabase sqlDb = new DbOpenHelper().getWritableDatabase();
            Cursor c = sqlDb.rawQuery("SELECT * FROM GNow_View WHERE searchText LIKE ? LIMIT " + Long.toString(limit), new String[] {"%" + query + "%"}, 0, 0);

            Log.d("MyApp", "Provider: Rows: " + Long.toString(c.getCount()));

            if (c.moveToFirst()) {
                String[] columns = c.getColumnNames();
                int r = 0;
                do {
                    StringBuilder builder = new StringBuilder();
                    for (int i=0, n=columns.length; i<n; i++) {
                        builder.append(columns[i]).append(": ").append(c.getString(i)).append("; ");
                    }
                    Log.d("MyApp", "Provider: Row" + r + ": " + builder.toString());
                    r++;
                } while (c.moveToNext());
            }

            return c;
        }
    } catch (Throwable t) {
        ALog.e(t.getMessage(), t);
    }

    return new MatrixCursor(new String[] {"SUGGEST_COLUMN_TEXT_1", "SUGGEST_COLUMN_TEXT_2", "SUGGEST_COLUMN_INTENT_DATA_ID"});
}

@Override
public String getType(Uri uri) {
    Log.d("MyApp", "Provider: getType(" + uri + ")");
    return "application/octet-stream-x";
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    Log.d("MyApp", "Provider: insert(" + uri + ", " + values + ")");
    return null;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    Log.d("MyApp", "Provider: delete(" + uri + ", ...)");
    return 0;
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    Log.d("MyApp", "Provider: update(" + uri + ", ...)");
    return 0;
}
}

0 个答案:

没有答案