检测手机短信传出

检测手机短信传出

本文介绍了安卓:检测手机短信传出,正确Count的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让应用程序从一个电话算传出短信的数量。 presently我有以下的code:

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

当我把一页纸的短信,的onChange 被称为3倍。当我把两页的短信,的onChange 被称为4倍。 3寻呼消息,称为5倍;等等。

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.

注:我曾尝试的其他变化Uri.parse(内容:// SMS),如内容://短信/发送,和没有什么比其他的。内容://短信已经得到了的onChange 被称为

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.

如何可以唯一区分传出SMS消息,并获得SMS消息的数量的准确计数发出?

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

推荐答案

我找到了解决我的问题。短信和彩信可以通过它们的_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"));

这篇关于安卓:检测手机短信传出,正确Count的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:35