我们正在尝试连接我们的 AndroidTV 应用程序以将结果附加到全局搜索中。我遇到了一个问题,因为系统在主线程上调用我的内容提供程序,所以我无法进行 api 调用来获取结果。
@Override
public Cursor query(Uri uri, String[] projection, String search, String[] selectionArgs, String searchOrder) {
... Logic here that calls the API using RxJava / Retrofit
return cursor;
}
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/foo"
android:searchSettingsDescription="@string/foo_results"
android:includeInGlobalSearch="true"
android:searchSuggestAuthority="com.foo.search.provider"
android:searchSuggestIntentAction="android.intent.action.VIEW" />
<provider
android:authorities="com.foo.search.provider"
android:name=".search.GlobalSearchProvider"
android:exported="true"/>
当我进行全局搜索时,我可以看到 ContentProvider#query 被调用。如果我尝试在当前线程上进行 api 调用,我会得到 networkonmainthreadexception。
我试图通知游标数据已更改但也没有成功。
getContext().getContentResolver().notifyChange(Uri.parse("content://com.foo.test"), null);
...
cursor.setNotificationUri(getContext().getContentResolver(), Uri.parse("content://com.foo.test"));
无论如何我可以强制操作系统在单独的线程上调用内容提供程序,或者至少通知搜索光标有新内容?
谢谢你