1. 程式人生 > >安卓讀取簡訊資料庫詳解

安卓讀取簡訊資料庫詳解

首先,把簡訊資料庫匯出來

安卓資料庫的名字叫mmssms.db,在/data/data/com.android.providers.telephony目錄下,該目錄需要手機root之後才能看

用SQLite DataBase Browser開啟

如圖,一共有這麼多的表:


但是真正有用的只有3個,

Canonical_addresses表————如下:_id對應threads表裡面的recipient_ids,address對應sms表裡面的address


Threads表————這個相當於手機打簡訊後按聯絡人分的組的那個介面,是按聯絡人分的會話列表



Sms表————手機所有簡訊都在這裡


接下來,需要了解一些基本的知識

ContentResolver

Cursor

這個可以自己從網上搜看看

下面是部分關鍵程式碼,所有的在我的上傳裡能搜到:

從threads表獲取日期,訊息數量,部分訊息內容,從sms表裡面取得threads裡_id對應的電話號碼

<span style="font-family:Microsoft YaHei;">class MyTask2 extends AsyncTask<String, Integer, ArrayList<MyMessageList>>{

		@Override
		protected void onPostExecute(ArrayList<MyMessageList> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			progressBar.setVisibility(View.GONE);
			textView.setVisibility(View.GONE);
			MyAdapter2 adapter2 = new MyAdapter2(MainActivity.this, myMessageLists);
			listView.setAdapter(adapter2);
		}

		@Override
		protected ArrayList<MyMessageList> doInBackground(String... params) {
			// TODO Auto-generated method stub
			ContentResolver resolver = getContentResolver();
			Cursor cursor = resolver.query(Uri.parse("content://mms-sms/conversations"), 
					new String[]{ "* from threads--" }, 
					null, 
					null, 
					null);
			System.out.println(cursor.getCount());
			if(cursor.getCount()>0){
				while (cursor.moveToNext()) {
					MyMessageList messageList = new MyMessageList();
					messageList.setThreadid((cursor.getString(cursor.getColumnIndex("_id"))));
					SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm");
					Date date = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))));
					String time = sfd.format(date);
					messageList.setDate(time);
					messageList.setMessagecount(cursor.getString(cursor.getColumnIndex("message_count")));
					messageList.setSnippet(cursor.getString(cursor.getColumnIndex("snippet")));				
					
					ContentResolver resolver2 = getContentResolver();
					Cursor cursor2 = resolver2.query(Uri.parse("content://sms/"), 
							new String[]{"address"}, 
							"thread_id=?", 
							new String[]{cursor.getString((cursor.getColumnIndex("_id")))},
							null);
						if(cursor2.moveToNext()){
							System.out.println(cursor2.getString(cursor2.getColumnIndex("address")));
							messageList.setPhone(cursor2.getString(cursor2.getColumnIndex("address")));					
						}					
					cursor2.close();
					myMessageLists.add(messageList);
				}
				System.out.println(myMessageLists.size());
			}
			
			cursor.close();		
			
			
			
			return myMessageLists;
//			return null;
		}
		
	}</span>

下面是從sms中獲取具體的和某個人通話的簡訊
<span style="font-family:Microsoft YaHei;">class GetMessage extends AsyncTask<String, Integer, ArrayList<DetailMessage>>{

		@Override
		protected void onPostExecute(ArrayList<DetailMessage> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			MyAdapter3 adapter3 = new MyAdapter3(MessageDetailActivity.this, arrayList);
			listView.setAdapter(adapter3);
		}

		@Override
		protected ArrayList<DetailMessage> doInBackground(String... params) {
			// TODO Auto-generated method stub
			ContentResolver resolver = getContentResolver();
			Cursor cursor = resolver.query(Uri.parse("content://sms/"), 
					new String[]{"date","body","type"}, 
					"thread_id=?", 
					new String[]{threadidString},
					"date desc");				
			if(cursor.getCount()>0){
				cursor.moveToFirst();
				System.out.println(cursor.getPosition());
				DetailMessage detailMessage = new DetailMessage();
				detailMessage.setMessageString(cursor.getString(cursor.getColumnIndex("body")));
				SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm");
				Date date = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))));
				String time = sfd.format(date);
				System.out.println(time);
//				System.out.println(cursor.getString(cursor.getColumnIndex("date")));
				detailMessage.setDateString(time);
				detailMessage.setType(cursor.getInt(cursor.getColumnIndex("type")));
				arrayList.add(detailMessage);
				while (cursor.moveToNext()) {
					System.out.println(cursor.getPosition());
					detailMessage = new DetailMessage();
					detailMessage.setMessageString(cursor.getString(cursor.getColumnIndex("body")));
					date = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))));
					time = sfd.format(date);
					detailMessage.setDateString(time);
					detailMessage.setType(cursor.getInt(cursor.getColumnIndex("type")));
					arrayList.add(detailMessage);				
				}
			}						
			cursor.close();						
			return arrayList;
		}
		
	}</span>