我正在尝试制作一个应用程序来计算手机发出的短信数量。目前我有以下代码:
当我发送一条单页短信时,
注意:我尝试了其他变体的
如何唯一区分已发出的短信并准确计算发送的短信数量?
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次。三页短信,则被调用5次;以此类推。注意:我尝试了其他变体的
Uri.parse("content://sms")
,例如"content://sms/sent",但除了"content://sms"之外,没有任何其他方法能够使onChange
被调用。如何唯一区分已发出的短信并准确计算发送的短信数量?