android 添加手机短信,获取手机短信,删除手机短信和修改手机短信

注意添加权限:

    <uses-permission android:name="android.permission.READ_SMS"></uses-permission>

    <uses-permission android:name="android.permission.WRITE_SMS"></uses-permission>

代码如下:

android 添加手机短信,获取手机短信,删除手机短信和修改手机短信
//更新短信

private void UpdateSMS()

{

/*

update支持的协议有很多:

SMS_RAW_MESSAGE    

SMS_STATUS_PENDING    

SMS_ALL    

SMS_FAILED    

SMS_QUEUED    

SMS_INBOX    

SMS_SENT    

SMS_DRAFT    

SMS_OUTBOX    

SMS_CONVERSATIONS    

SMS_ALL_ID    

SMS_INBOX_ID    

SMS_FAILED_ID    

SMS_SENT_ID    

SMS_DRAFT_ID    

SMS_OUTBOX_ID    

SMS_CONVERSATIONS_ID    

SMS_STATUS_ID

*/

ContentValues cv = new ContentValues();    

cv.put("thread_id", "2");    

cv.put("address", "00000");    

cv.put("person", "11");    

//cv.put("date", "11111111");  

cv.put("body", "hello,modify content333333333");

getContentResolver().update(Uri.parse("content://sms/sent/3"), cv, null,null);  

//getContentResolver().update(Uri.parse("content://sms/sent/3"), cv, "id=?",new String[]{"3"});   

}

//删除短信

private void DeleteSMS()

{

/*

* 其中,delete方法中支持的协议为:

* SMS_ALL 根据参数中的条件删除sms表数据 

* SMS_ALL_ID 根据_id删除sms表数据

* SMS_CONVERSATIONS_ID 根据thread_id删除sms表数据,可以带其它条件 

* SMS_RAW_MESSAGE 根据参数中的条件删除 raw表

* SMS_STATUS_PENDING 根据参数中的条件删除 sr_pending表

* SMS_SIM 从Sim卡上删除数据

* 试一下SMS_CONVERSATIONS_ID:"content://sms/conversations/3 ",删除thread_id="3", _id="5"的数据 在eclipse中的Emulator

* Control中,以13800给模拟器发送三条数据,然后以13900发送一条

* this.getContentResolver().delete

* (Uri.parse("content://sms/conversations/3"), "_id=?", new

* String[]{"5"}); 成功删除一条数据。

* 在数据库中每个发送者的thread_id虽然一样,但不是固定的,如果把一个发送者的全部数据删除掉,

* 然后换一个新号码发送短信时,thread_id是以数据库中最大的id+1赋值的。

*/

/*

看了一下android的源代码,sms支持的协议有:

 

sURLMatcher.addURI("sms", null, SMS_ALL); 

sURLMatcher.addURI("sms", "#", SMS_ALL_ID); 

sURLMatcher.addURI("sms", "inbox", SMS_INBOX); 

sURLMatcher.addURI("sms", "inbox/#", SMS_INBOX_ID); 

sURLMatcher.addURI("sms", "sent", SMS_SENT); 

sURLMatcher.addURI("sms", "sent/#", SMS_SENT_ID); 

sURLMatcher.addURI("sms", "draft", SMS_DRAFT); 

sURLMatcher.addURI("sms", "draft/#", SMS_DRAFT_ID); 

sURLMatcher.addURI("sms", "outbox", SMS_OUTBOX); 

sURLMatcher.addURI("sms", "outbox/#", SMS_OUTBOX_ID); 

sURLMatcher.addURI("sms", "undelivered", SMS_UNDELIVERED); 

sURLMatcher.addURI("sms", "failed", SMS_FAILED); 

sURLMatcher.addURI("sms", "failed/#", SMS_FAILED_ID); 

sURLMatcher.addURI("sms", "queued", SMS_QUEUED); 

sURLMatcher.addURI("sms", "conversations", SMS_CONVERSATIONS); 

sURLMatcher.addURI("sms", "conversations/*", SMS_CONVERSATIONS_ID); 

sURLMatcher.addURI("sms", "raw", SMS_RAW_MESSAGE); 

sURLMatcher.addURI("sms", "attachments", SMS_ATTACHMENT); 

sURLMatcher.addURI("sms", "attachments/#", SMS_ATTACHMENT_ID); 

sURLMatcher.addURI("sms", "threadID", SMS_NEW_THREAD_ID); 

sURLMatcher.addURI("sms", "threadID/*", SMS_QUERY_THREAD_ID); 

sURLMatcher.addURI("sms", "status/#", SMS_STATUS_ID); 

sURLMatcher.addURI("sms", "sr_pending", SMS_STATUS_PENDING); 

sURLMatcher.addURI("sms", "sim", SMS_ALL_SIM); 

sURLMatcher.addURI("sms", "sim/#", SMS_SIM);*/

//getContentResolver().delete(Uri.parse("content://sms/failed/3"), "_id=?", new String[]{"99"});  

 

getContentResolver().delete(Uri.parse("content://sms/#"),"_id=?", new String[]{"99"});  

}

//添加短信

private void AddSMS()

{

/*

* insert支持的协议:

* 

* SMS_ALL 

* SMS_INBOX 

* SMS_FAILED 

* SMS_QUEUED 

* SMS_SENT 

* SMS_DRAFT

* SMS_OUTBOX 

* SMS_RAW_MESSAGE 

* SMS_STATUS_PENDING 

* SMS_ATTACHMENT

* SMS_NEW_THREAD_ID

* 

* 向sms表插入数据时,type是根据协议来自动设置,

* 如果传入的数据中没有设置date时,自动设置为当前系统时间;非SMS_INBOX协议时,read标志设置为1

* SMS_INBOX协议时,系统会自动查询并设置PERSON threadId为null或者0时,系统也会自动设置

* 

* 一直为造不了"发送失败"的邮件而发愁,现在来做一个: content://sms/failed

*/

String id=null;

ContentValues cv = new ContentValues();   

for(int i = 0;i<5;i++)

{

id.valueOf(i);

cv.put("_id", id);    

cv.put("thread_id", "0");    

cv.put("address", "9999");    

cv.put("person", "888");    

//cv.put("date", "9999"); 

cv.put("protocol", "0"); 

cv.put("read", "1"); 

cv.put("status", "-1"); 

cv.put("body", "@@@@@@@@@"); 

getContentResolver().insert(Uri.parse("content://sms/sent"), cv);

cv.clear();

}

 

 

}

//查询短信内容

private void GetSMS()

{

/*

* content://sms/inbox 收件箱 content://sms/sent 已发送

* content://sms/draft 草稿 content://sms/outbox 发件箱

* content://sms/failed 发送失败 content://sms/queued 待发送列表

* 数据库中sms相关的字段如下: 

*  _id 一个自增字段,从1开始 thread_id 序号,同一发信人的id相同

*            同一个联系人的thread_id是一样的,通过遍历thread id和对比就能知道一个联系人有几条短信;

*            当然了,Threads表里面有个message_out的字段也是显示有条联系人的,但是Threads表还没有找到可以读取的办法;

*            

*  address 发件人手机号码 

*  person 联系人列表里的序号,陌生人为null ;即rawcontact表的contact_id,或者contact表的_id;

*            可通过此id关联联系人;但是person对应结果不稳定,应该使用PhoneLookup.CONTENT_FILTER_URI

*  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 locked

*/

Log.i("SMS","开始获取短信");

String[] projection = new String[] {

          "_id",

          "address",

          "person",

          "date",

          "protocol",

          "read",

          "status",

          "body",

          "service_center",

          "subject"

         };

Cursor cur = getContentResolver().query(Uri.parse("content://sms"), projection, null,null, "date desc");

 

StringBuilder str=new StringBuilder();

        if (cur.moveToFirst()) {

        String name; 

            String phoneNumber;       

            String sms;

            long date;

            String id;

            int idColumn = cur.getColumnIndex("_id");

            int nameColumn = cur.getColumnIndex("person");

            int phoneColumn = cur.getColumnIndex("address");

            int smsColumn = cur.getColumnIndex("body");

            int dateColumn=cur.getColumnIndex("date");

            do {

                // Get the field values

                name = cur.getString(nameColumn);             

                phoneNumber = cur.getString(phoneColumn);

                sms = cur.getString(smsColumn);

                date = cur.getLong(dateColumn);

                String temp = long2String(date);

                id = cur.getString(idColumn);

                //date = String2Long(date);

                str.append("{");

                str.append(name+",");

                str.append(phoneNumber+",");

                str.append(sms+",");

                str.append(temp+",");

                str.append(id);

                str.append("}");

                

                

                if (null==sms)

                sms="";

            } while (cur.moveToNext());

        }

        else

        {

            str.append("no result!");

        }

        Log.i("SMS",str.toString());

        Toast.makeText(context, str.toString(), Toast.LENGTH_SHORT).show();

        System.out.print("短信结果:"+str.toString());

        cur.close();

}

private String long2String(long time) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss:SSSS");

return sdf.format(new Date(time));

}
android 添加手机短信,获取手机短信,删除手机短信和修改手机短信

 

android 添加手机短信,获取手机短信,删除手机短信和修改手机短信,布布扣,bubuko.com

android 添加手机短信,获取手机短信,删除手机短信和修改手机短信

上一篇:Android开发之TextView的下划线添加


下一篇:@RequestMapping 用法详解之地址映射(转)