第一种方式,采用 getContentResolver().query()方法在主线程中查询数据。这种查询方式是不是异步查询的,直接在UI线程中查询数据,代码如下:
Cursor cursor1 = getContentResolver().query(Sms.CONVESATION_URI, CONVERSATION_PROJECTION,null, null," sms.date desc"); while (cursor1.moveToNext()) { Log.i("cursor1", String.valueOf(cursor1.getInt(0))); Log.i("cursor1", cursor1.getString(1)); Log.i("cursor1", cursor1.getString(2)); } cursor1.close();
Cursor cursor2 = managedQuery(Sms.CONVESATION_URI, CONVERSATION_PROJECTION, null, null, "sms.date desc"); while (cursor2.moveToNext()) { Log.i("cursor2", String.valueOf(cursor2.getInt(0))); Log.i("cursor2", cursor2.getString(1)); Log.i("cursor2", cursor2.getString(2)); }
private void startQuery() { Uri uri = Sms.CONVESATION_URI; mQueryHandler.startQuery(0, null, uri, CONVERSATION_PROJECTION, null, null, "sms.date desc"); } // 写一个异步查询类 private final class QueryHandler extends AsyncQueryHandler { public QueryHandler(ContentResolver cr) { super(cr); } @Override protected void onQueryComplete(int token, Object cookie, Cursor cursor) { super.onQueryComplete(token, cookie, cursor); // 更新mAdapter的Cursor mAdapter.changeCursor(cursor); } }