我目前在以下 URI“content://sms/”上注册了一个内容观察器,以监听正在发送的传入和传出消息。
这似乎工作正常,我也尝试从短信数据库中删除,但我只能从以下 URI“content://sms/conversations/”中删除整个线程
这是我使用的代码
String url = "content://sms/";
Uri uri = Uri.parse(url);
getContentResolver().registerContentObserver(uri, true, new MyContentObserver(handler));
}
class MyContentObserver extends ContentObserver {
public MyContentObserver(Handler handler) {
super(handler);
}
@Override public boolean deliverSelfNotifications() {
return false;
}
@Override public void onChange(boolean arg0) {
super.onChange(arg0);
Log.v("SMS", "Notification on SMS observer");
Message msg = new Message();
msg.obj = "xxxxxxxxxx";
handler.sendMessage(msg);
Uri uriSMSURI = Uri.parse("content://sms/");
Cursor cur = getContentResolver().query(uriSMSURI, null, null,
null, null);
cur.moveToNext();
String protocol = cur.getString(cur.getColumnIndex("protocol"));
if(protocol == null){
Log.d("SMS", "SMS SEND");
int threadId = cur.getInt(cur.getColumnIndex("thread_id"));
Log.d("SMS", "SMS SEND ID = " + threadId);
Cursor c = getContentResolver().query(Uri.parse("content://sms/outbox/" + threadId), null, null,
null, null);
c.moveToNext();
int p = cur.getInt(cur.getColumnIndex("person"));
Log.d("SMS", "SMS SEND person= " + p);
//getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadId), null, null);
}
else{
Log.d("SMS", "SMS RECIEVE");
int threadIdIn = cur.getInt(cur.getColumnIndex("thread_id"));
getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadIdIn), null, null);
}
}
}
但是我希望能够从 SMS Content Provider 获取接收者和消息文本,谁能告诉我该怎么做?
还有如何删除一条消息而不是整个线程?
最佳答案
这已经讨论过了。
要从内容提供者检查读取短信: - android-1-5-reading-sms-messages
检查这个线程:
- delete-sms-in-android-1-5
- how-to-delete-sms-from-inbox-in-android-programmatically
- can-we-delete-an-sms-in-android-before-it-reaches-the-inbox
关于您说您要删除整个线程而不是单个短信的评论: 你试过了吗this code ?
关于Android - 查询 SMS ContentProvider?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2584058/