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字段。
Cursor cursor = cr.query(Uri.parse("content://sms/"),
newString[] {"* from threads--"},null,null,null);
查询Threads表。
网上说Threads的URI为:"content://mms-sms/conversations"
不过由于本人使用这个Uri查询出错,故使用content://sms/ 通过构造查询字段数组来查询Threads表。
publicstaticList getSession(ContentResolver cr) {
Cursor cursor = cr.query(Uri.parse("content://sms/"),
newString[] {"* from threads--"},null,null,null);
list =newArrayList();
if(cursor.moveToFirst()) {
do{
if(threads ==null) {
threads =newThreads();
}
threads.set_id(cursor.getInt(ID));
threads.setDate(cursor.getLong(DATE));
threads.setError(cursor.getInt(ERROR));
threads.setHas_attachment(cursor.getInt(HAS_ATTACHMENT));
threads.setMessage_count(cursor.getInt(MESSAGE_COUNT));
threads.setRead(cursor.getInt(READ));
threads.setRecipient_ids(cursor.getString(RECIPIENT_IDS));
threads.setSnippet(cursor.getString(SNIPPET));
threads.setSnippet_cs(cursor.getInt(SNIPPET_CS));
threads.setType(cursor.getInt(TYPE));
list.add(threads);
threads =null;
}while(cursor.moveToNext());
}
returnlist;
}
最后通过获取到的thread_id作为参数再去查询sms表。就可以获取每个会话的所有信息。
packagewu.lis.bu.utils;
importjava.text.SimpleDateFormat;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.List;
importwu.lis.bu.bean.Status;
importandroid.content.ContentResolver;
importandroid.database.Cursor;
importandroid.net.Uri;
importandroid.util.Log;
publicclassSmsService {
privatefinalString SMS_URI_ALL ="content://sms/";
privatefinalString SMS_URI_INBOX ="content://sms/inbox";
privatefinalString SMS_URI_SEND ="content://sms/send";
privatefinalString SMS_URI_DRAFT ="content://sms/draft";
List sms_list =null;
Status status =null;
publicList getSmsInphone(ContentResolver cr, Integer thread_id) {
sms_list =newArrayList();
String[] projection =newString[] {"_id","address","person",
"body","date","type"};
Uri uri = Uri.parse(SMS_URI_ALL);
Cursor cursor = cr.query(uri, projection,"thread_id=?",
newString[] { Integer.toString(thread_id) },"date desc");
if(cursor.moveToFirst()) {
String name;
String phoneNumber;
String smsBody;
String date;
String type;
//int nameColumn = cursor.getColumnIndex("person");
intphoneNumberColumn = cursor.getColumnIndex("address");
intsmsBodyColumn = cursor.getColumnIndex("body");
intdateColumn = cursor.getColumnIndex("date");
inttypeColumn = cursor.getColumnIndex("type");
do{
status =newStatus();
//name = cursor.getString(nameColumn);
String pNumber ="";
phoneNumber = cursor.getString(phoneNumberColumn);
if(phoneNumber.length() >11) {
pNumber = phoneNumber.substring(phoneNumber.length() -11,
phoneNumber.length());
}else{
pNumber = phoneNumber;
}
name = PhoneService.getPeople(cr, pNumber);
smsBody = cursor.getString(smsBodyColumn);
SimpleDateFormat dateFormat =newSimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
Date d =newDate(Long.parseLong(cursor.getString(dateColumn)));
date = dateFormat.format(d);
inttypeId = cursor.getInt(typeColumn);
if(typeId ==1) {
type ="接收";
}elseif(typeId ==2) {
type ="发送";
}else{
type ="";
}
if(smsBody ==null) {
smsBody ="";
}
status.setPhoneNum(phoneNumber);
status.setContent(smsBody);
status.setLastReceive(date);
status.setPerson(name);
status.settype(type);
sms_list.add(status);
status =null;
}while(cursor.moveToNext());
}
for(Status status : sms_list) {
Log.i("Status", status.getPhoneNum());
}
returnsms_list;
}
}