我們試圖連接我們的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