android SMS与MMS

sancaiodm Android应用 2021-08-23 1972 0

简称:

短信SMS(Short Messaging Service),//文字 表情信息

彩信MMS(Multimedia MessagingService)。//图片,视频,音频,VCard 等等

private static final Uri MMS_CONTENT_URI = Uri.parse("content://mms");      //Uri代表所有短信

private static final Uri SMS_CONTENT_URI = Uri.parse("content://sms");         //Uri代表所有彩信

private static final Uri MMSSMS_CONTENT_URI = Uri.parse("content://mms-sms");     // Uri 是表示所有的对话。


1)会话表:content://mms-sms/conversations/

2)短信表:content://sms

3)彩信表:content://mms、content://mms/part


URI主要有:


content://sms/               所有短信

content://sms/inbox        收件箱

content://sms/sent          已发送

content://sms/draft         草稿

content://sms/outbox     发件箱

content://sms/failed       发送失败

content://sms/queued    待发送列表



sms主要结构:  

_id => 短消息序号 如100     //此ID为短信序列号,从1开始, 接收到一条短信则加+1,与thread_id不同,

thread_id => 对话的序号 如100     //此会话ID若是同一个号码的短信此ID都相同的,从1开始,如是新号码发送过来的短信则此ID会加+1,不同的电话才会递加,若要删除同一号码的短信往来,则可用此thread_id会判断条件

address => 发件人地址,手机号.如+8613811810000  

person => 发件人,返回一个数字就是联系人列表里的序号,陌生人为null  

date => 日期  long型。如1256539465022  

protocol => 协议 0 SMS_RPOTO, 1 MMS_PROTO   

read => 是否阅读 0未读, 1已读   

status => 状态 -1接收,0 complete, 64 pending, 128 failed   

type => 类型 1是接收到的,2是已发出   

type     

    ALL    = 0;

    INBOX  = 1;

    SENT   = 2;

    DRAFT  = 3;

    OUTBOX = 4;

    FAILED = 5;

    QUEUED = 6; 

body => 短消息内容   

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      协议,分为: 0 SMS_RPOTO, 1 MMS_PROTO  

read           是否阅读 0未读, 1已读  

status         状态 -1接收,0 complete, 64 pending, 128 failed 

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


//监听此URI路径无法获取最新接收的短信内容,只能获取倒数第二条新短信
this.getContentResolver().registerContentObserver(Uri.parse("content://sms/raw")



            Uri uri = Uri.parse(SMS_URI_ALL);

            String[] projection = new String[] { "_id", "address", "person", "body", "date", "type", };

            Cursor cur = getContentResolver().query(uri, projection, null,  null, "date desc"); // 获取手机内部短信,且按时间排序

            // 获取短信中最新的未读短信

            // Cursor cur = getContentResolver().query(uri, projection, "read = ?", new String[]{"0"}, "date desc");

(cursor != && cursor.getCount() > && cursor.moveToFirst()) {
     {
         smsId = cursor.getInt(cursor.getColumnIndex("_id"));
         threadIdPos = cursor.getInt(cursor.getColumnIndex("thread_id"));
         body = cursor.getString(cursor.getColumnIndex("body"));
         long smsdate = cursor.getLong(cursor.getColumnIndex("date"));
         mSMSType = cursor.getInt(cursor.getColumnIndex("type"));
         number = cursor.getString(cursor.getColumnIndex("address"));
      
     }while(cursor.moveToNext());
  }
  
  如果你只是想拿到第一条,不想遍历所有信息内容则
   if(cursor.moveToNext()){//不遍历只拿当前最新的一条短信
      xxxxx
   }


评论