关闭

[置顶] android 4.2 SMS常见数据监听, 更新方式, ContentObserver, CursorLoader,AsyncQueryHandler

1387人阅读 评论(0) 收藏 举报
分类:


1. 注册数据库监听的ContentObserver, 数据库发生变化了的监听Observer, 代码如下:

   private void registerThreadsInfoObserver() {
        if (null == mThreadsInfoObserver) {
            mThreadsInfoObserver = new ThreadsInfoObserver(new Handler());
        }
        mActivity.getContentResolver().registerContentObserver(Threads.CONTENT_URI,
                true, mThreadsInfoObserver);
    }
    
    private void unRegisterThreadsInfoObserver() {
        if(mThreadsInfoObserver != null){
            getActivity().getContentResolver().unregisterContentObserver(mThreadsInfoObserver);
        }
    }   
    
    private class ThreadsInfoObserver extends ContentObserver {

        public ThreadsInfoObserver(Handler handler) {
            super(handler);
        }
        
        public void onChange(boolean selfChange) {
            if (mActivity != null) {
                 startAsyncQuery();
            }

        }
    }  
    
2. CursorLoader

        onCreate 中初始化:
        mLoaderManager = getLoaderManager();
        mLoaderManager.initLoader(0, null, callbacks);


        callbacks 实现:
        LoaderManager.LoaderCallbacks<Cursor> callbacks = new LoaderManager.LoaderCallbacks<Cursor>() {
        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            Log.i(TAG, "\n LoaderCallbacks -onCreateLoader mCursorFilter:" + mCursorFilter);
            Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI,
                    Uri.encode( mCursorFilter ));
            
            CursorLoader cursor = new CursorLoader(ComposeMessageActivity.this, uri, projection, null, null, null);
            return cursor;
        }

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
            Log.i(TAG, "\n LoaderCallbacks -onLoadFinished Ssize: " + cursor.getCount());

            //TODO

            mListSearchAdapter.notifyDataSetChanged();

        }

        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
            Log.i(TAG, "\n LoaderCallbacks -onLoaderReset");
             mAdapter.changeCursor(null);
        }
    };
    
    重新查询:
    mLoaderManager.restartLoader(0, null, callbacks);
    
    将游标设置到Adapter中:
    mListAdapter.changeCursor(cursor);



3.  异步handler 查询


   public static void startQueryForImportant(AsyncQueryHandler handler, int token, String selection) {
        handler.cancelOperation(token);
        
        //TODO query for important msg
        handler.startQuery(token, null, Uri.parse("content://sms"), IMPORTANT_PROJECTION, selection, null, Conversations.DEFAULT_SORT_ORDER);
    }
    
    
        private final class ThreadListQueryHandler extends AsyncQueryHandler {
        public ThreadListQueryHandler(ContentResolver contentResolver) {
            super(contentResolver);
        }

        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            Log.i(TAG, "important onQueryComplete tab index=" + mTabType
                    + "cursor.getCount: " + cursor.getCount());
            switch (token) {
                case THREAD_LIST_QUERY_TOKEN:
                mListAdapter.changeCursor(cursor);

                if (mListAdapter.getCount() == 0) {
                    ((TextView)(getListView().getEmptyView())).setText(R.string.no_conversations);
                }
                break;

            default:
                Log.e(TAG, "onQueryComplete called with unknown token " + token);
            }
        }
    }


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:72925次
    • 积分:1353
    • 等级:
    • 排名:千里之外
    • 原创:59篇
    • 转载:33篇
    • 译文:0篇
    • 评论:18条
    文章分类
    最新评论