Android - 查询 SMS ContentProvider?

发布于 2024-08-27 10:38:53 字数 1953 浏览 6 评论 0原文

我目前在以下 URI“content://sms/”上注册了一个内容观察器,以侦听正在发送的传入和传出消息。

这似乎工作正常,我也尝试从 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 内容提供商获取收件人和消息文本,谁能告诉我该怎么做?

以及如何删除一条消息而不是整个线程?

I currently register a content observer on the following URI "content://sms/" to listen out for incoming and outgoing messages being sent.

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/"

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?

And also how to delete one message instead of an entire thread?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖公主 2024-09-03 10:38:53

这已经讨论过了。

要从内容提供商读取 SMS,请检查:
- android-1-5-reading-sms-messages

检查此线程:

关于您的评论,称您要删除整个线程而不是单个短信:
您是否尝试过此代码

This was already discussed.

To read SMS from the Content provider check:
- android-1-5-reading-sms-messages

Check this threads:

About your comment saying that you are deleting a whole thread instead of a single sms:
Have you tried out this code?

黄昏下泛黄的笔记 2024-09-03 10:38:53

地址栏包含短信发送者的电话号码。

使用cursor.getString(cursor.getColumnIndex("address")) 将电话号码提取为字符串。将光标弹出到 content://sms 上并按日期降序对其进行排序以获取最新消息。您必须等待新消息进入表,否则您将从错误的消息中提取信息。在传入SMS 广播接收器中,在线程中使用while 循环,轮询cursor.getCount() 的变化。然后,在 while 循环之后,cursor.moveToFirst 将成为新消息。

例如:

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);
}

然后获取短信发送者的地址:

cur = getContentResolver().query(Uri.parseUri(content://sms), null, null, null, "date DESC");

cur.MoveToFirst();
String telephoneNumber = cur.getString(cur.getColumnIndex("address");

这个 while 循环将暂停线程,直到新消息到达。您还可以使用 contentObserver,但是这个 while 循环很简单,不需要注册、注销和单独的类。

坦率地说,我认为直接从传入意图的 pdu 中提取地址和消息正文会更快。这样你就不必等待消息进入表中来获取地址和正文。 Android 类 SmsMessage 有多种有用的方法。

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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文