Android sms应用程序,每次收到短信时都会增加

时间:2015-02-19 08:49:13

标签: android smsmanager

我开发了一个可以接收短信的应用程序,但实际上我想要的是从某个数字接收短信,然后每次收到短信时短信的数量增加1

这是我的清单

<receiver android:name = "SmsReceiver">
<intent-filter>
<action android:name = "android.provider.Telephony.SMS RECEIVED"/>
</intent-filter>
</receiver>

这就是我在扩展BroadcastReceiver

的SmsReceiver类中所拥有的
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
    Object[] messages = (Object[])bundle.get("pdus");

    SmsMessage[] sms = new SmsMessage[messages.length];

    for(int n=0; n<messages.length; n++){
        sms[n] = SmsMessage.createFromPdu((byte[])messages[n]);
    }
    for(SmsMessage msg:sms){
        DOH_Home.updateMessageBox("\nFrom: " + msg.getOriginatingAddress() + "\n" + "Message:" + msg.getMessageBody() + "\n");
    }

}

1 个答案:

答案 0 :(得分:0)

    String number = "YOUR_SEARCHED_NUMBER";
    for(SmsMessage msg:sms){
        String sender = msg.getOriginatingAddress() ;
        if (sender != null && sender.equals(number)){
            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            int count = pref.getInt("COUNT", 0);
            count++;
            pref.edit().putInt("COUNT", count).commit();
        }
     }