如何编写监控收入短信并在 Toast 中显示的广播接收器

how to write broadcast receiver that monitor income sms and show in Toast

我想开发一个应用程序,在其中监控特定收入短信的名称和正文显示在 toast 中。 以及如何防止将此类 SMS 插入收件箱,因为我们不需要将 SMS 发送到收件箱。

收件人代码:

    public class mReciver extends BroadcastReceiver {

     SmsManager sms = SmsManager.getDefault();

     public void onReceive(Context context, Intent intent) {

      // there I need code
     }

}

实际上数据在意图中,您可以轻松地从意图中获取接收号码和文本 // 像这样写代码

public class mReciver extends BroadcastReceiver {
String IncomeNumber,Incometext ;
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();

            if (bundle != null) {
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
                for (int i = 0; i < pdusObj.length; i++) {
                    SmsMessage inComeText = SmsMessage
                            .createFromPdu((byte[]) pdusObj[i]);
                    IncomeNumber = inComeText
                            .getDisplayOriginatingAddress();
                    Incometext = inComeText.getDisplayMessageBody();
                }
            }

        }
    }      

对于特定的号码,您只需将号码与商店号码进行比较 就这样。

if(IncomeNumber.equals("+123456788")){
    Toast.makeText(context,"Text: "+Incometext+"\nfrom: "+IncomeNumber, 0).show();
    abortBroadcast();
}

abortBroadcast(); 用于防止表单存储收件箱

尝试类似的东西,

 public class SmsReceiver extends BroadcastReceiver {
 // vars here

 @Override
  public void onReceive(Context context, Intent intent) {

    // Get SMS map from Intent
    Bundle extras = intent.getExtras();

    if (extras != null) {
        // Get received SMS array
        Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME);

        String address = "";
        String body = "";

        for (int i = 0; i < smsExtra.length; ++i) {
            SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
            body += sms.getMessageBody().toString();
            address = sms.getOriginatingAddress();
        }
      Toast.makeText(context, body,1000).show();
    }