URI主要有:

content://sms/ 所有短信
content://sms/inbox 收件箱
content://sms/sent 已发送
content://sms/draft 草稿
content://sms/outbox 发件箱
content://sms/failed 发送失败
content://sms/queued 待发送列表



sms主要结构:
  1. _id => 短消息序号 如100
  2. thread_id => 对话的序号 如100
  3. address => 发件人地址,手机号.如+8613811810000
  4. person => 发件人,返回一个数字就是联系人列表里的序号,陌生人为null
  5. date => 日期long型。如1256539465022
  6. protocol => 协议0SMS_RPOTO,1MMS_PROTO
  7. read => 是否阅读0未读,1已读
  8. status => 状态 -1接收,0complete,64pending,128failed
  9. type => 类型1是接收到的,2是已发出
  10. body => 短消息内容
  11. service_center => 短信服务中心号码编号。如+8613800755500
String[] projection = new String[]{"address", "body"};
Cursor cursor = getContentResolver().query(uri, projection, "where .." new String[]{"", ""}, "order by ..")





Android短信存储数据库

偶然发现了Android源码中的一个类MmsSmsDatabaseHelper.java,原来android将所有的短信信息都存入了mmssms.db中。

公开的SDK中没有这个类,不能直接使用。于是自己写了一个SQLiteOpenHelper,但是查询的时候发生SQL异常。看来不能为所欲为了,不过据网上资料介绍可以拷贝db文件来实现短信数据备份。


MmsSmsDatabaseHelper.java在Android源码中的路径:

packages/providers/TelephonyProvider/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java


sms数据库中的字段如下:

_id 一个自增字段,从1开始
thread_id 序号,同一发信人的id相同
address 发件人手机号码
person 联系人列表里的序号,陌生人为null
date 发件日期
protocol 协议,分为:0SMS_RPOTO,1MMS_PROTO
read 是否阅读0未读,1已读
status 状态-1接收,0complete,64pending,128failed
type
ALL = 0;
INBOX = 1;
SENT = 2;
DRAFT = 3;
OUTBOX = 4;
FAILED = 5;
QUEUED = 6;

body 短信内容
service_center 短信服务中心号码编号
subject 短信的主题
reply_path_present TP-Reply-Path
locked


sms数据库表字段类型的源码:


[java] view plain copy print ?
  1. privatevoidcreateSmsTables(SQLiteDatabasedb){
  2. //N.B.:Wheneverthecolumnsherearechanged,thecolumnsin
  3. //{@refMmsSmsProvider}mustbechangedtomatch.
  4. db.execSQL("CREATETABLEsms("+
  5. "_idINTEGERPRIMARYKEY,"+
  6. "thread_idINTEGER,"+
  7. "addressTEXT,"+
  8. "personINTEGER,"+
  9. "dateINTEGER,"+
  10. "date_sentINTEGERDEFAULT0,"+
  11. "protocolINTEGER,"+
  12. "readINTEGERDEFAULT0,"+
  13. "statusINTEGERDEFAULT-1,"+//aTP-Statusvalue
  14. //or-1ifit
  15. //statushasn't
  16. //beenreceived
  17. "typeINTEGER,"+
  18. "reply_path_presentINTEGER,"+
  19. "subjectTEXT,"+
  20. "bodyTEXT,"+
  21. "service_centerTEXT,"+
  22. "lockedINTEGERDEFAULT0,"+
  23. "error_codeINTEGERDEFAULT0,"+
  24. "seenINTEGERDEFAULT0"+
  25. ");");
  26. ....
  27. }


packages/providers/TelephonyProvider/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java


联系人为空

短信数据库里面如果你是先受到陌生短信之后再把陌生人添加到联系人列表的话,短信数据库里面的person字段就为null,如果你是先添加联系人再发短 信的话,短信数据库里面的person字段就不为空了,所以你要是想通过短信数据库里的字段取得联系人的其他信息的话,只能通过地址来取。

实例:
  1. publicStringgetSmsInPhone(){
  2. finalStringSMS_URI_ALL="content://sms/";
  3. finalStringSMS_URI_INBOX="content://sms/inbox";
  4. finalStringSMS_URI_SEND="content://sms/sent";
  5. finalStringSMS_URI_DRAFT="content://sms/draft";
  6. finalStringSMS_URI_OUTBOX="content://sms/outbox";
  7. finalStringSMS_URI_FAILED="content://sms/failed";
  8. finalStringSMS_URI_QUEUED="content://sms/queued";
  9. StringBuildersmsBuilder=newStringBuilder();
  10. try{
  11. Uriuri=Uri.parse(SMS_URI_ALL);
  12. String[]projection=newString[]{"_id","address","person","body","date","type"};
  13. Cursorcur=getContentResolver().query(uri,projection,null,null,"datedesc");//获取手机内部短信
  14. if(cur.moveToFirst()){
  15. intindex_Address=cur.getColumnIndex("address");
  16. intindex_Person=cur.getColumnIndex("person");
  17. intindex_Body=cur.getColumnIndex("body");
  18. intindex_Date=cur.getColumnIndex("date");
  19. intindex_Type=cur.getColumnIndex("type");
  20. do{
  21. StringstrAddress=cur.getString(index_Address);
  22. intintPerson=cur.getInt(index_Person);
  23. Stringstrbody=cur.getString(index_Body);
  24. longlongDate=cur.getLong(index_Date);
  25. intintType=cur.getInt(index_Type);
  26. SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss");
  27. Dated=newDate(longDate);
  28. StringstrDate=dateFormat.format(d);
  29. StringstrType="";
  30. if(intType==1){
  31. strType="接收";
  32. }elseif(intType==2){
  33. strType="发送";
  34. }else{
  35. strType="null";
  36. }
  37. smsBuilder.append("[");
  38. smsBuilder.append(strAddress+",");
  39. smsBuilder.append(intPerson+",");
  40. smsBuilder.append(strbody+",");
  41. smsBuilder.append(strDate+",");
  42. smsBuilder.append(strType);
  43. smsBuilder.append("]\n\n");
  44. }while(cur.moveToNext());
  45. if(!cur.isClosed()){
  46. cur.close();
  47. cur=null;
  48. }
  49. }else{
  50. smsBuilder.append("noresult!");
  51. }//endif
  52. smsBuilder.append("getSmsInPhonehasexecuted!");
  53. }catch(SQLiteExceptionex){
  54. Log.d("SQLiteExceptioningetSmsInPhone",ex.getMessage());
  55. }
  56. returnsmsBuilder.toString();
  57. }

更多相关文章

  1. android 生成xml文件
  2. android获取程序安装时间
  3. Android短信备份
  4. Android-NDK开发之基础--Android(安卓)JNI实例代码(二)-- 获取/
  5. 读取联系人 2.0
  6. android viewpage的施用
  7. Android(安卓)ApiDemos示例解析(102):Views->Auto Complete->4.
  8. android textview 显示表情和文字 表情带超链接
  9. android studio 权限类,不断更新中 如果你有权限我这边没有 请告

随机推荐

  1. Android简单的计步器应用实现
  2. Android时间工具类 本地转UTC,UTC转本地
  3. 【Android(安卓)内存优化】Bitmap 内存缓
  4. Android菜单实现两种方式
  5. Android(安卓)App 手機軟體20個私房推薦,
  6. Android(安卓)Setting中添加解除屏幕锁选
  7. android之File
  8. Android(安卓)Manager之SensorManager(传
  9. android 自定义TextView中Html超链接点击
  10. 我今天的面试题,注册广播有几种方式,这些方