4

我一直在尝试从我的应用中获取信息以显示在 Google Now 搜索页面中。我似乎已经设置了搜索建议,当我在平板电脑上进行平板电脑搜索时,我什至看到我的提供程序已执行。但什么也没有出现。

显现

    <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"
    />

可搜索的.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" />

我的内容提供者.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;
}
}
4

0 回答 0