1.首先是寫一個查詢的方法
//查詢資料 private void startQuery() { Uri uri=Sms.CONTENT_URI; String selection=" thread_id=?"; String[] selectionArgs=new String[]{thread_id}; //查詢 //projection 查詢的欄位 queryHandler.startQuery(0, null, uri, SMS_PROJECTION, selection, selectionArgs, " date DESC"); }
2.把需要查詢的欄位給抽取出來。
/** * 將查詢所用的欄位處理出來 */ private static final String[] SMS_PROJECTION=new String[]{ "_id", "address", "date", "type", "body", }; /** * 當前欄位所對應的位置 */ private static final int ID_COLUMN_INDEX = 0; private static final int ADDRESS_COLUMN_INDEX = 1; private static final int DATE_COLUMN_INDEX = 2; private static final int TYPE_COLUMN_INDEX = 3; private static final int BODY_COLUMN_INDEX = 4;
3.然後繼承非同步查詢方塊架
private class QueryHandler extends AsyncQueryHandler{ public QueryHandler(ContentResolver cr) { super(cr); } //查詢執行完成後悔執行這個方法 protected void onQueryComplete(int token, Object cookie, Cursor cursor) { // TODO Auto-generated method stub super.onQueryComplete(token, cookie, cursor); //介面更新 adapter.changeCursor(cursor); } }