【发布时间】:2015-12-06 11:35:29
【问题描述】:
我正在尝试将我的应用纳入 Android TV 全局搜索,根据 documentation 我必须创建以下内容:
- 内容提供者
- 可搜索的.xml
当然也可以将它们包含在清单中。 这就是我所做的,我的内容提供者非常简单。它不返回任何数据,但是当它得到 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示例中复制的,所以我没有想法,所以该示例运行良好,复制后立即中断。
任何帮助将不胜感激!
【问题讨论】:
-
标签和提示不能是字符串文字
-
@pskink wow...我真的没想到会是这样,影响如此之小却如此之大...想要将其设置为答案以便我接受吗?跨度>
标签: android android-contentprovider android-search android-tv