簡體   English   中英

內容提供者 query() 未被調用(Android TV)

[英]Content provider query() not being called (Android TV)

根據我必須創建的文檔,我正在嘗試將我的應用程序包含在 Android TV 全局搜索中:

  • 內容提供商
  • 可搜索文件

當然,將它們包含在清單中。 所以這就是我所做的,我的內容提供者非常簡單。 它不返回任何數據,但是當它獲得 query() 調用時,它會在日志中打印一些尚未完成的行。

public class VideoContentProvider extends ContentProvider {
private static String TAG = "VideoContentProvider";
public static String AUTHORITY = "test.tvsearch";

// UriMatcher stuff
private static final int SEARCH_SUGGEST = 0;
private static final int REFRESH_SHORTCUT = 1;
private static final UriMatcher URI_MATCHER = buildUriMatcher();

//private VideoDatabase mVideoDatabase;

/**
 * Builds up a UriMatcher for search suggestion and shortcut refresh queries.
 */
private static UriMatcher buildUriMatcher() {
    UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
    // to get suggestions...
    Log.d(TAG, "suggest_uri_path_query: " + SearchManager.SUGGEST_URI_PATH_QUERY);
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST);
    return matcher;
}

@Override
public boolean onCreate() {
    Log.d(TAG, "onCreate");
    //mVideoDatabase = new VideoDatabase(getContext());
    return true;
}

/**
 * Handles all the video searches and suggestion queries from the Search Manager.
 * When requesting a specific word, the uri alone is required.
 * When searching all of the video for matches, the selectionArgs argument must carry
 * the search query as the first element.
 * All other arguments are ignored.
 */
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
                    String sortOrder) {
    // Use the UriMatcher to see what kind of query we have and format the db query accordingly
    switch (URI_MATCHER.match(uri)) {
        case SEARCH_SUGGEST:
            Log.d(TAG, "search suggest: " + selectionArgs[0] + " URI: " + uri);
            if (selectionArgs == null) {
                throw new IllegalArgumentException(
                        "selectionArgs must be provided for the Uri: " + uri);
            }
            Log.i("...", "WORKED");
            return null;
        default:
            throw new IllegalArgumentException("Unknown Uri: " + uri);
    }
}

/**
 * This method is required in order to query the supported types.
 * It's also useful in our own query() method to determine the type of Uri received.
 */
@Override
public String getType(Uri uri) {
    switch (URI_MATCHER.match(uri)) {
        case SEARCH_SUGGEST:
            return SearchManager.SUGGEST_MIME_TYPE;
        case REFRESH_SHORTCUT:
            return SearchManager.SHORTCUT_MIME_TYPE;
        default:
            throw new IllegalArgumentException("Unknown URL " + uri);
    }
}

// Other required implementations...

@Override
public Uri insert(Uri uri, ContentValues values) {
    throw new UnsupportedOperationException();
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    throw new UnsupportedOperationException();
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    throw new UnsupportedOperationException();
}

}

可搜索的.xml:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="test leanback api demo"
    android:hint="searching for videos test"
    android:searchSettingsDescription="settings text desc"
    android:searchSuggestAuthority="test.tvsearch"
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:searchSuggestSelection=" ?"
    android:searchSuggestThreshold="1"
    android:includeInGlobalSearch="true"
    >

這段代碼幾乎直接來自 Android TV 的 Leanback 示例,我提取了這一部分,因為它是唯一處理全局搜索的部分。

我也在清單中包含了 searchable.xml 的提供者和意圖過濾器:

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.LEANBACK_LAUNCHER" />

            <action android:name="android.intent.action.SEARCH" />

        </intent-filter>

        <!-- Points to searchable meta data. -->
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />

    </activity>

    <provider
        android:name=".VideoContentProvider"
        android:authorities="test.tvsearch"
        android:exported="true" >...

正如我所看到的 ContentProvider 確實得到了 onCreate 調用,所以它在清單中是正確的。 然而問題是搜索時的 Android TV 沒有通過我的應用程序,查詢方法沒有被調用。

我還看到該應用程序沒有在 Android TV 設置 > 首選項 > 搜索 > 可搜索應用程序中列出,我認為這真的很奇怪,因為在 searchable.xml 中據說在全局搜索中包含提供程序。 因為這段代碼幾乎是從 Leanback 示例中復制的,所以我沒有想法,因此該示例運行良好,並且在復制時立即中斷。

任何幫助將不勝感激!

如果你打開它,你會看到android:labelandroid:hint都必須是“字符串資源”(@string/something),我認為構建系統(或 lint 工具或其他任何東西)現在可以捕獲這些情況(我花了幾個像你三四年前一樣的確切問題的幾個小時),但不,似乎開發人員即使現在也拉扯他們的頭發,所以只需替換:

android:label="test leanback api demo"
android:hint="searching for videos test"

和:

android:label="@string/search_label"
android:hint="@string/search_hint"

我不確定android:searchSettingsDescription因為在一個地方它說:“字符串資源”但在詳細描述中它是簡單的“字符串”

對於其他苦苦掙扎的人來說,Google 沒有在他們的教程中包含的內容是您必須在設備設置中啟用您的應用程序可搜索。 否則,您的內容提供程序已創建但從未被查詢。 可搜索配置中找到了這個:

android:includeInGlobalSearch 布爾值。 (需要在快速搜索框中提供搜索建議。)如果您希望您的建議包含在可全局訪問的快速搜索框中,請設置為“true”。 在您的建議出現在快速搜索框中之前,用戶仍必須在系統搜索設置中啟用您的應用程序作為可搜索項。

重要的是在清單下一個代碼中的一個活動中,它幫助了我:

<intent-filter>
            <action android:name="android.intent.action.SEARCH"/>

 <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM