Android 中管理短信(2)

还可以用getContentResolver()获得一个ContentResolver,   
getContentResolver().query()同样返回一个Cursor对象,参数跟managedQuery一致。   
不过用ContentResolver对象去更新、删除和插入一条数据时报SecurityException。看来没有权限,在Manifest.xml中加入权限:
<uses-permission android:name="android.permission.WRITE_SMS"></uses-permission>
然后删除短信:
this.getContentResolver().delete(Uri.parse("content://sms"), "_id=?", new String[]{"3"});
删除成功。


--------------------------------------------------------------------------------

Url中content://sms 替换成content://sms/ 也成功,但是其它url时程序报错,比如content://sms/inbox

看了一下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);


--------------------------------------------------------------------------------

其中,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赋值的。   


--------------------------------------------------------------------------------

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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyggdz.html