未调用内容提供者query()(Android TV) [英] Content provider query() not being called (Android TV)

查看:97
本文介绍了未调用内容提供者query()(Android TV)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据

}

Searchable.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:label="test leanback api demo"
android:hint="searching for videos test"

具有:

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

我不确定android:searchSettingsDescription,因为在某个地方它说:字符串资源",但在详细描述中,它是简单的字符串"

I'm trying to include my app into the Android TV global search, according to the documentation I have to create the following:

  • 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.

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:

<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.

I included the provider and intent filter for the searchable.xml also in the manifest:

<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. However the problem is that Android TV when searching doesn't go through my app, the query method does not get called.

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. 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.

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

这篇关于未调用内容提供者query()(Android TV)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆