多个SMS广播接收器编辑文本

时间:2016-03-21 07:41:44

标签: android android-edittext broadcastreceiver sms

通过BroadcastReceiver收到消息后,我会自动更新一个包含在SMS中的6个数字的edittext框。 我可以第一次成功地做到这一点,但我希望第二次重复使用该代码,但无法找到方法。

public class SMSReceiver extends BroadcastReceiver {
private static EditText editTextInputNumber;
@Override
public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(
                "android.provider.Telephony.SMS_RECEIVED")) {
            StringBuilder sms = new StringBuilder();    
            Bundle bundle = intent.getExtras();        

            if (bundle != null) {
                Object[] pdusObj = (Object[])  bundle.get("pdus");
                SmsMessage[] messages = new SmsMessage[pdusObj.length];
                for (int i = 0; i < pdusObj.length; i++) {
                    messages[i] =
                            SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                }

                for (SmsMessage smsMessage : messages) {
                    sms.append(smsMessage.getMessageBody());
                }

                sms.toString(); 
                String smsBody = sms.toString();
                Pattern pattern = Pattern.compile("\\d{6}");
                Matcher matcher = pattern.matcher(smsBody);
                String authNumber = null;

                if (matcher.find()) {
                    authNumber = matcher.group(0);
                }

                if (authNumber != null) {
                Activity_create.inputAuthNumber(authNumber);
  

我想使用上面的内容,并在打开其他一些活动时更改另一个edittext.Response_findaccount.inputAuthNumber(authNumber);

            }
        }
    }
}

INSIDE Activity_Create我在做:

...
sendBroadcast(new Intent("android.provider.Telephony.SMS_RECEIVED"));
...
public static void inputAuthNumber(String authNumber) {
    if (authNumber != null) {
        et_dialog_access.setText(authNumber);
    }
}

INSIDE Activity_findaccount我也想用不同的edittext做同样的事情:

...
sendBroadcast(new Intent("android.provider.Telephony.SMS_RECEIVED"));
...
public static void inputAuthNumber(String authNumber) {
    if (authNumber != null) {
        et_find_access.setText(authNumber);
    }
}
  

更新:答案:请参阅MIKEM以下的评论。和检查的答案

为什么取消注册很重要=防止泄漏。

何时注册/取消注册请点击此链接: When to unregister BroadcastReceiver? In onPause(), onDestroy(), or onStop()?

  

我的最终编码使用此。而不是getActivity。如果你在课堂活动

public class Fragment_findaccount1 extends Fragment{

BroadcastReceiver smsReceiver;
...
@Override
public void onPause(){
    super.onPause();
    getActivity().unregisterReceiver(smsReceiver);
}
...
private void SMS_Receive() {
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
    smsReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            StringBuilder sms = new StringBuilder();
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Object[] pdusObj = (Object[]) bundle.get("pdus");
                SmsMessage[] messages = new SmsMessage[pdusObj.length];
                for (int i = 0; i < pdusObj.length; i++) {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                }
                for (SmsMessage smsMessage : messages) {
                    sms.append(smsMessage.getMessageBody());
                }
                sms.toString();
                String smsBody = sms.toString();
                Pattern pattern = Pattern.compile("\\d{6}");
                Matcher matcher = pattern.matcher(smsBody);
                String authNumber = null;
                if (matcher.find()) {
                    authNumber = matcher.group(0);
                }
                if (authNumber != null) {
                    inputAuthNumber(authNumber);
                }
            }
        }
    };
    getActivity().registerReceiver(smsReceiver, intentFilter);
}

private void inputAuthNumber(String authNumber) {
    if (authNumber != null) {
        et_cert.setText(authNumber);
        progressbar.setVisibility(View.INVISIBLE);
    }
}

1 个答案:

答案 0 :(得分:1)

如果要在不同的活动中使用广播接收器,则需要在活动中声明广播接收器。 `

private void registerOTPReceiver() {
    intentFilter = new IntentFilter();
    intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
    mOtpReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            // Your Code goes here
            updateOtpText(matcher.group(0));
        }
    }
};
this.registerReceiver(mOtpReceiver, intentFilter);

private void updateOtpText(String otp){
    // Set the message text to wherever you want
    mOtpEditText.setText(otp);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    this.unregisterReceiver(mOtpReceiver);
}

`