我们试图连接我们的AndroidTV应用程序以将结果附加到全局搜索中。我遇到了一个问题,我无法通过api调用来获取结果,因为系统在主线程上调用了我的内容提供者。在全球搜索的内容提供商处调用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#查询被调用。如果我尝试在当前线程上执行api调用,则会收到networkonmainthreadexception。
我试图通过游标提示数据已经改变但没有成功。
getContext().getContentResolver().notifyChange(Uri.parse("content://com.foo.test"), null);
...
cursor.setNotificationUri(getContext().getContentResolver(), Uri.parse("content://com.foo.test"));
反正我有可以强制O.S呼吁在一个单独的线程的内容提供商,或者至少通知光标有新的内容搜索?
谢谢
请问您可以看看下面的答案吗? – Sebastiano 2014-10-14 08:12:27
@dextor我想你可以尝试改变你的searchableinfo出现在SearchManager.getSearchablesInGlobalSearch()列表中的顺序。我认为你可以通过更改可搜索活动的名称来完成此操作。所以你的应用程序搜索将由搜索应用程序最后完成,但我不确定它是否会工作。进一步说,创建一个独立进程的目的是让查询不会在主线程上运行,如果您只设置了策略宽松,查询仍然会在主线程中运行,并且UI操作可能会延迟,或者您可能会收到ANR – nandeesh 2014-10-14 10:00:00
我的搜索结果已经显示为最后结果,所以这是不行的。我知道,在单独的流程上运行“解决”了这个问题,但是我注意到,由此产生的延迟高于在同一流程上运行。我无法弄清楚为什么。 – Sebastiano 2014-10-14 10:08:19