Android 获取短信会话信息 .

来源:互联网 发布:sql for循环语句 编辑:程序博客网 时间:2024/05/01 03:23

Android 手机信息存放在mmssms.db数据库。

短讯息主要用到sms表和threads表。

查看其表结构

sms表,信息表

threads表

1.mesage_count该会话的消息数量
2.recipient_ids为联系人ID,这个ID不是联系人表中的_id,而是指向表canonical_address里的id,
canonical_address这个表同样位于mmssms.db,它映射了recipient_ids到一个电话号码,也就是说,
最终获取联系人信息,还是得通过电话号码;
3.snippet为最后收到/发出的信息

4._id为会话id,他关联到sms表中的thread_id字段。

[java] view plaincopyprint?
  1. Cursor cursor = cr.query(Uri.parse("content://sms/"),  
  2.                 new String[] { "* from threads--" }, nullnullnull);  

查询Threads表。

网上说Threads的URI为:"content://mms-sms/conversations"

不过由于本人使用这个Uri查询出错,故使用content://sms/ 通过构造查询字段数组来查询Threads表。

[java] view plaincopyprint?
  1. public static List<Threads> getSession(ContentResolver cr) {  
  2.         Cursor cursor = cr.query(Uri.parse("content://sms/"),  
  3.                 new String[] { "* from threads--" }, nullnullnull);  
  4.         list = new ArrayList<Threads>();  
  5.         if (cursor.moveToFirst()) {  
  6.             do {  
  7.                 if (threads == null) {  
  8.                     threads = new Threads();  
  9.                 }  
  10.                 threads.set_id(cursor.getInt(ID));  
  11.                 threads.setDate(cursor.getLong(DATE));  
  12.                 threads.setError(cursor.getInt(ERROR));  
  13.                 threads.setHas_attachment(cursor.getInt(HAS_ATTACHMENT));  
  14.                 threads.setMessage_count(cursor.getInt(MESSAGE_COUNT));  
  15.                 threads.setRead(cursor.getInt(READ));  
  16.                 threads.setRecipient_ids(cursor.getString(RECIPIENT_IDS));  
  17.                 threads.setSnippet(cursor.getString(SNIPPET));  
  18.                 threads.setSnippet_cs(cursor.getInt(SNIPPET_CS));  
  19.                 threads.setType(cursor.getInt(TYPE));  
  20.                 list.add(threads);  
  21.                 threads = null;  
  22.             } while (cursor.moveToNext());  
  23.         }  
  24.         return list;  
  25.     }  

最后通过获取到的thread_id作为参数再去查询sms表。就可以获取每个会话的所有信息。

[java] view plaincopyprint?
  1. package wu.lis.bu.utils;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.ArrayList;  
  5. import java.util.Date;  
  6. import java.util.List;  
  7.   
  8. import wu.lis.bu.bean.Status;  
  9. import android.content.ContentResolver;  
  10. import android.database.Cursor;  
  11. import android.net.Uri;  
  12. import android.util.Log;  
  13.   
  14. public class SmsService {  
  15.   
  16.     private final String SMS_URI_ALL = "content://sms/";  
  17.     private final String SMS_URI_INBOX = "content://sms/inbox";  
  18.     private final String SMS_URI_SEND = "content://sms/send";  
  19.     private final String SMS_URI_DRAFT = "content://sms/draft";  
  20.     List<Status> sms_list = null;  
  21.     Status status = null;  
  22.   
  23.     public List<Status> getSmsInphone(ContentResolver cr, Integer thread_id) {  
  24.         sms_list = new ArrayList<Status>();  
  25.   
  26.         String[] projection = new String[] { "_id""address""person",  
  27.                 "body""date""type" };  
  28.         Uri uri = Uri.parse(SMS_URI_ALL);  
  29.         Cursor cursor = cr.query(uri, projection, "thread_id=?",  
  30.                 new String[] { Integer.toString(thread_id) }, "date desc");  
  31.         if (cursor.moveToFirst()) {  
  32.             String name;  
  33.             String phoneNumber;  
  34.             String smsBody;  
  35.             String date;  
  36.             String type;  
  37.             //int nameColumn = cursor.getColumnIndex("person");  
  38.             int phoneNumberColumn = cursor.getColumnIndex("address");  
  39.             int smsBodyColumn = cursor.getColumnIndex("body");  
  40.             int dateColumn = cursor.getColumnIndex("date");  
  41.             int typeColumn = cursor.getColumnIndex("type");  
  42.             do {  
  43.                 status = new Status();  
  44.                 //name = cursor.getString(nameColumn);  
  45.                 String pNumber = "";  
  46.                 phoneNumber = cursor.getString(phoneNumberColumn);  
  47.                 if (phoneNumber.length() > 11) {  
  48.                     pNumber = phoneNumber.substring(phoneNumber.length() - 11,  
  49.                             phoneNumber.length());  
  50.                 } else {  
  51.                     pNumber = phoneNumber;  
  52.                 }  
  53.                 name = PhoneService.getPeople(cr, pNumber);  
  54.                 smsBody = cursor.getString(smsBodyColumn);  
  55.                 SimpleDateFormat dateFormat = new SimpleDateFormat(  
  56.                         "yyyy-MM-dd hh:mm:ss");  
  57.                 Date d = new Date(Long.parseLong(cursor.getString(dateColumn)));  
  58.                 date = dateFormat.format(d);  
  59.                 int typeId = cursor.getInt(typeColumn);  
  60.                 if (typeId == 1) {  
  61.                     type = "接收";  
  62.                 } else if (typeId == 2) {  
  63.                     type = "发送";  
  64.                 } else {  
  65.                     type = "";  
  66.                 }  
  67.                 if (smsBody == null) {  
  68.                     smsBody = "";  
  69.                 }  
  70.                 status.setPhoneNum(phoneNumber);  
  71.                 status.setContent(smsBody);  
  72.                 status.setLastReceive(date);  
  73.                 status.setPerson(name);  
  74.                 status.settype(type);  
  75.                 sms_list.add(status);  
  76.                 status = null;  
  77.             } while (cursor.moveToNext());  
  78.         }  
  79.         for (Status status : sms_list) {  
  80.             Log.i("Status", status.getPhoneNum());  
  81.         }  
  82.         return sms_list;  
  83.     }  
  84. }  
原创粉丝点击