2012-10-09 123 views
4

有人可以向我解釋什麼是runQueryOnBackgroundThread,因爲我已經閱讀了一些資源,但仍然不理解它?有人可以向我解釋runQueryOnBackgroundThread嗎?

@Override 
public Cursor runQueryOnBackgroundThread(CharSequence constraint){ 
    FilterQueryProvider filter = getFilterQueryProvider(); 
    if (filter != null){ 
     return filter.runQuery(constraint); 
    } 

    Uri uri = Uri.withAppendedPath(
       ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(constraint.toString())); 

    return content.query(uri, CONTACT_PROJECTION, null, null, null); 
} 

回答

3

我在適配器中的活動句柄和過濾器中的runQuery調用會在調用runQuery時在Activity上調用startManagingCursor。這並不理想,因爲後臺線程正在調用startManagingCursor,並且可能還有很多遊標保持打開狀態,直到Activity被銷燬。

添加以下到我的適配器,其對活動手柄是內

@Override 
public void changeCursor(Cursor newCursor) { 
Cursor oldCursor = getCursor(); 
super.changeCursor(newCursor); 
if(oldCursor != null && oldCursor != newCursor) { 
    // adapter has already dealt with closing the cursor 
    activity.stopManagingCursor(oldCursor); 
} 
activity.startManagingCursor(newCursor); 
} 

這可以確保適配器使用當前光標也由活動管理使用。當適配器管理通過活動關閉遊標時將被刪除。適配器持有的最後一個光標將被活動關閉,但仍由活動管理。

相關問題