Android - 查询 SMS ContentProvider?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2584058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Querying the SMS ContentProvider?
提问by Donal Rafferty
I currently register a content observer on the following URI "content://sms/" to listen out for incoming and outgoing messages being sent.
我目前在以下 URI“content://sms/”上注册了一个内容观察者,以侦听正在发送的传入和传出消息。
This seems to work ok and I have also tried deleting from the sms database but I can only delete an entire thread from the following URI "content://sms/conversations/"
这似乎工作正常,我也尝试从 sms 数据库中删除,但我只能从以下 URI“content://sms/conversations/”中删除整个线程
Here is the code I use for that
这是我使用的代码
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);
}
}
}
However I want to be able to get the recipricant and the message text from the SMS Content Provider, can anyone tell me how to do this?
但是,我希望能够从 SMS 内容提供商处获取收件人和消息文本,谁能告诉我如何执行此操作?
And also how to delete one message instead of an entire thread?
以及如何删除一条消息而不是整个线程?
采纳答案by Macarse
This was already discussed.
这已经讨论过了。
To read SMS from the Content provider check: - android-1-5-reading-sms-messages
要从内容提供商检查读取短信: - android-1-5-reading-sms-messages
Check this threads:
检查这个线程:
- 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
About your comment saying that you are deleting a whole thread instead of a single sms: Have you tried out this code?
关于您的评论说您正在删除整个线程而不是单个短信:您是否尝试过此代码?
回答by Noah Seidman
The address column contains the telephone number of the sms sender.
地址栏包含短信发送者的电话号码。
Use cursor.getString(cursor.getColumnIndex("address")) to pull the telephone number as a String. Pop a cursor on content://sms and sort it in date descending order to get the most recent message. You will have to wait for the new message to enter the table otherwise you will pull information from the wrong message. In an incomingSMS broadcastReceiver use a while loop, in a thread, polling for the cursor.getCount() to change. Then after the while loop cursor.moveToFirst will be the new message.
使用 cursor.getString(cursor.getColumnIndex("address")) 将电话号码作为字符串提取。在 content://sms 上弹出光标并按日期降序对其进行排序以获取最新消息。您将不得不等待新消息进入表格,否则您将从错误消息中提取信息。在incomingSMS broadcastReceiver 中使用while 循环,在一个线程中,轮询cursor.getCount() 来改变。然后在 while 循环之后 cursor.moveToFirst 将是新消息。
For example:
例如:
Cursor cur = getContentResolver().query(Uri.parseUri(content://sms), null, null, null, null);
int count = cur.getCount();
while (cur.getCount() == count)
{
Thread.sleep(1000);
cur = getContentResolver().query(Uri.parseUri(content://sms), null, null, null, null);
}
Then get the address of the sms sender:
然后获取短信发送者的地址:
cur = getContentResolver().query(Uri.parseUri(content://sms), null, null, null, "date DESC");
cur.MoveToFirst();
String telephoneNumber = cur.getString(cur.getColumnIndex("address");
This while loop will pause the thread until the new message arrives. You can also use a contentObserver, but this while loop is simple and does not require registration, unregistration, and a separate class.
这个 while 循环将暂停线程直到新消息到达。您也可以使用 contentObserver,但这个 while 循环很简单,不需要注册、注销和单独的类。
Frankly I think its faster to pull the address and the body of the message directly from the pdu of the incoming intent. This way you dont have to wait for the message to enter the table to get the address and the body. The Android class SmsMessage has a variety of useful methods.
坦率地说,我认为直接从传入意图的 pdu 中提取地址和消息正文会更快。这样你就不用等消息进入表来获取地址和正文了。Android 类 SmsMessage 有多种有用的方法。