Android:检测短信发送,计数不正确

发布于 2024-11-28 18:46:13 字数 1505 浏览 2 评论 0原文

我正在尝试制作一个应用程序来计算手机发出的短信的数量。目前我有以下代码:

private void listenForSMS(){
    Log.v("SMSTEST", "STARTED LISTENING FOR SMS OUTGOING");
    Handler handle = new Handler(){};

    SMSObserver myObserver = new SMSObserver(handle);

    ContentResolver contentResolver = getContentResolver();
    contentResolver.registerContentObserver(Uri.parse("content://sms"),true, myObserver);
}

private class SMSObserver extends ContentObserver{

    int count = 0;
    String lastMessage = null;

    public SMSObserver(Handler handler) {
        super(handler);
    }

    public void onChange(boolean selfChange) {
        super.onChange(selfChange);

        Log.v("SMSTEST", "HIT ON CHANGE");

        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){
            count++;
            Log.v("SMSTEST", "SMS SENT: count: " + count);
            String content = cur.getString(cur.getColumnIndex("body"));

        }else{
            Log.v("SMSTEST", "SMS RECIEVED");
        }
    }
}

当我发送一页短信时,onChange 被调用3次。当我发送两页 SMS 消息时,onChange 被调用 4 次。 3页留言,呼叫5次;等等。

注意:我尝试过 Uri.parse("content://sms") 的其他变体,例如“content://sms/sent”,除了“content://sms”之外什么都没有" 已调用 onChange

如何唯一区分一条外发短信并准确统计发送的短信条数?

I am trying to make an application to count the number of outgoing SMS messages from a phone. Presently I have the following code:

private void listenForSMS(){
    Log.v("SMSTEST", "STARTED LISTENING FOR SMS OUTGOING");
    Handler handle = new Handler(){};

    SMSObserver myObserver = new SMSObserver(handle);

    ContentResolver contentResolver = getContentResolver();
    contentResolver.registerContentObserver(Uri.parse("content://sms"),true, myObserver);
}

private class SMSObserver extends ContentObserver{

    int count = 0;
    String lastMessage = null;

    public SMSObserver(Handler handler) {
        super(handler);
    }

    public void onChange(boolean selfChange) {
        super.onChange(selfChange);

        Log.v("SMSTEST", "HIT ON CHANGE");

        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){
            count++;
            Log.v("SMSTEST", "SMS SENT: count: " + count);
            String content = cur.getString(cur.getColumnIndex("body"));

        }else{
            Log.v("SMSTEST", "SMS RECIEVED");
        }
    }
}

When I send a one page SMS message, onChange is called 3 times. When I send a two page SMS message, onChange is called 4 times. 3 page message, called 5 times; And so on.

NOTE: I have tried other variations of Uri.parse("content://sms"), such as "content://sms/sent", and nothing other than "content://sms" has gotten onChange to be called.

How can I uniquely distinguish an outgoing SMS message and get an accurate count of the number of SMS messages sent?

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

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

发布评论

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

评论(3

怼怹恏 2024-12-05 18:46:13

请注意,当您发送短信时,它会保存短信的状态。要解决这个问题,请检查该记录的类型 int type = c.getInt(c.getColumnIndex("type"));
3次onchange():
第一:类型 = 6
第二:类型 = 4
第三:类型 = 2(成功)
那么你可以增加你的计数器。
此案例仅适用于 1 位收件人。
如果收件人数量多于 1 个,则需要重新查询“thread_id”和“seen”来获取类型。您可以查看有多少条消息发送成功。
祝你好运!

Note that, when you send a SMS message, it will save the status of message. To solve it, you check the type of that records int type = c.getInt(c.getColumnIndex("type"));
3 times onchange():
1st : type = 6
2nd : type = 4
3rd : type = 2 (success)
then you can increase your counter.
This case is applied for 1 recipient only.
If there are much more 1 recipient, need to requery "thread_id" and "seen" to get the type. You can check how many message are sent success.
Good luck!

李不 2024-12-05 18:46:13

我找到了解决问题的方法。 SMS 和 MMS 可以通过其 _id 值进行唯一区分。

Uri uriSMSURI = Uri.parse("content://sms");
    Cursor cur = getContentResolver().query(uriSMSURI, null, null,
                 null, null);
    cur.moveToNext();
String id = cur.getString(cur.getColumnIndex("_id"));

I found a solution to my problem. SMSs and MMSs can be uniquely distinguished by their _id value.

Uri uriSMSURI = Uri.parse("content://sms");
    Cursor cur = getContentResolver().query(uriSMSURI, null, null,
                 null, null);
    cur.moveToNext();
String id = cur.getString(cur.getColumnIndex("_id"));
对你再特殊 2024-12-05 18:46:13

如何唯一区分一条外发短信并准确统计发送的短信数量?

你不能。

此内容提供程序不是 Android SDK 的一部分。它的使用没有记录且不受支持。此外,任何给定的消息传递客户端(例如,AOSP Messenger 应用程序)如何使用它本身没有文档记录且不受支持。一条发送的 SMS 可能会导致内容提供商发生 1,337 次更改,而您却无从得知。此外,甚至不需要在该内容提供商或任何内容提供商中记录传出的SMS消息。并且,如果最近的行为是任何迹象表明,谷歌未来有可能关闭第三方对该内容提供商的访问。

How can I uniquely distinguish an outgoing SMS message and get an accurate count of the number of SMS messages sent?

You can't.

This content provider is not part of the Android SDK. Its use is undocumented and unsupported. Moreover, how any given messaging client (e.g., the AOSP Messenger app) might use it is itself undocumented and unsupported. A single sent SMS could result in 1,337 changes to the content provider, and you have no way to know. Furthermore, there is no requirement for outgoing SMS messages to even be recorded in this or any content provider. And, if recent behavior is any indication, it is possible that Google will shut down third-party access to this content provider in the future.

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