安卓读取短信数据库详解

首先,把短信数据库导出来

安卓数据库的名字叫mmssms.db,在/data/data/com.android.providers.telephony目录下,该目录需要手机root之后才能看

用SQLite DataBase Browser打开

如图,一共有这么多的表:

安卓读取短信数据库详解_第1张图片

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

Canonical_addresses表————如下:_id对应threads表里面的recipient_ids,address对应sms表里面的address

安卓读取短信数据库详解_第2张图片
Threads表————这个相当于手机打短信后按联系人分的组的那个界面,是按联系人分的会话列表



Sms表————手机所有短信都在这里

安卓读取短信数据库详解_第3张图片

接下来,需要了解一些基本的知识

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>



你可能感兴趣的:(android)