据报道,我正在尝试将我的应用程序纳入 Android TV 全局搜索中文档 http://developer.android.com/training/tv/discovery/searchable.html我必须创建以下内容:
当然,将它们包含在清单中。
这就是我所做的,我的内容提供者非常简单。它不返回任何数据,但是当它收到 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 示例中复制的,所以我没有想法,该示例运行得很好,复制后它会立即中断。
任何帮助将不胜感激!