简体   繁体   English

内容提供者 query() 未被调用(Android TV)

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

I'm trying to include my app into the Android TV global search, according to the documentation I have to create the following:根据我必须创建的文档,我正在尝试将我的应用程序包含在 Android TV 全局搜索中:

  • ContentProvider内容提供商
  • Searchable.xml可搜索文件

And of course include them into the manifest.当然,将它们包含在清单中。 So that's what I did, my contentprovider is extemely simple.所以这就是我所做的,我的内容提供者非常简单。 It doesn't return any data, but when it gets the query() call it will print some lines in the log, which it has not done yet.它不返回任何数据,但是当它获得 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();
}

} }

Searchable.xml:可搜索的.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"
    >

This code comes nearly direct from the Android TV leanback example, I extracted this part because it's the only part which handles the global searching.这段代码几乎直接来自 Android TV 的 Leanback 示例,我提取了这一部分,因为它是唯一处理全局搜索的部分。

I included the provider and intent filter for the searchable.xml also in the manifest:我也在清单中包含了 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" >...

As I've seen the ContentProvider does get the onCreate call, so it is correctly in the manifest.正如我所看到的 ContentProvider 确实得到了 onCreate 调用,所以它在清单中是正确的。 However the problem is that Android TV when searching doesn't go through my app, the query method does not get called.然而问题是搜索时的 Android TV 没有通过我的应用程序,查询方法没有被调用。

I've also seen that the app does not get listed in the Android TV settings > Preferences > Searching > Searchable apps which I think is really weird, because in the searchable.xml it is said to include the provider in the global search.我还看到该应用程序没有在 Android TV 设置 > 首选项 > 搜索 > 可搜索应用程序中列出,我认为这真的很奇怪,因为在 searchable.xml 中据说在全局搜索中包含提供程序。 Because this code is nearly copied from the leanback example I'm out of ideas, the example works perfectly and when copied it breaks immediately.因为这段代码几乎是从 Leanback 示例中复制的,所以我没有想法,因此该示例运行良好,并且在复制时立即中断。

Any help will be appreciated!任何帮助将不胜感激!

If you open this you will see that both android:label and android:hint have to be "string resource" (@string/something) and I thought that build system (or lint tool or whatever) catch that cases now (I spent couple of hours on the exact issue like yours three or four years back), but no, it seems that developers pull hair of their head even now, so simply replace:如果你打开它,你会看到android:labelandroid:hint都必须是“字符串资源”(@string/something),我认为构建系统(或 lint 工具或其他任何东西)现在可以捕获这些情况(我花了几个像你三四年前一样的确切问题的几个小时),但不,似乎开发人员即使现在也拉扯他们的头发,所以只需替换:

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

with:和:

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

I am not sure about android:searchSettingsDescription since in one place it says: "string resource" but in detailed description it is simple "string"我不确定android:searchSettingsDescription因为在一个地方它说:“字符串资源”但在详细描述中它是简单的“字符串”

For anybody else struggling, what Google does not include in their tutorial is that you have to enable your app as searchable in device settings.对于其他苦苦挣扎的人来说,Google 没有在他们的教程中包含的内容是您必须在设备设置中启用您的应用程序可搜索。 Otherwise your content provider is created but never queried.否则,您的内容提供程序已创建但从未被查询。 Found this in searchable configuration :可搜索配置中找到了这个:

android:includeInGlobalSearch Boolean. android:includeInGlobalSearch 布尔值。 (Required to provide search suggestions in Quick Search Box.) Set to "true" if you want your suggestions to be included in the globally accessible Quick Search Box. (需要在快速搜索框中提供搜索建议。)如果您希望您的建议包含在可全局访问的快速搜索框中,请设置为“true”。 The user must still enable your application as a searchable item in the system search settings before your suggestions will appear in Quick Search Box.在您的建议出现在快速搜索框中之前,用户仍必须在系统搜索设置中启用您的应用程序作为可搜索项。

Important to have in one of your Activities in manifest next code, it helped me:重要的是在清单下一个代码中的一个活动中,它帮助了我:

<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