我正在尝试创建一个应用程序来计算来自手机的外发短信的数量.目前我有以下代码:
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); 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次;等等.
注意:我已经尝试过Uri.parse的其他变体(“content:// sms”),例如“content:// sms / sent”,除了“content:// sms”之外没有任何变化可以调用.
如何唯一地区分外发短信并准确计算发送的短信数量?