如何将数据从BroadcastReceiver传递给Activity?

时间:2016-09-15 12:46:17

标签: android android-activity broadcastreceiver

我有一个BroadcastReceiver类,它读取传入的sms并从sms中提取一个引脚。 我想将sms传递给用户已经可见的活动。 用户应在EditText中输入图钉,我将从EditText中取值,将用户输入的图钉与BroadcastReceiver传递的图钉进行比较。如果两者相互匹配,用户可以进入应用程序。但我不知道如何将BroadcastReceiver收到的Pin传递到Activity。 这是代码

BroadcastReceiver类

public class IncomingMessage extends BroadcastReceiver {

final SmsManager sms = SmsManager.getDefault();
private OnSMSReceived onSMSReceived = null;

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

    // Retrieves a map of extended data from the intent.
    final Bundle bundle = intent.getExtras();

    try {

        if (bundle != null) {

            String senderNum = null;
            String message = null;
            final Object[] pdusObj = (Object[]) bundle.get("pdus");

            for (int i = 0; i < pdusObj.length; i++) {

                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                senderNum = phoneNumber;
                message = currentMessage.getDisplayMessageBody();

                Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);

            }

            if (senderNum.equals("ideamart")) {
                Log.d("MessageIdeaMart", message);

            } else if (senderNum.equals("FindDroid")) {
                if (message.startsWith("Welcome")) {
                    String[] splitArray = message.split(" ");
                    String pin = splitArray[7];
                    Log.d("PIN", pin);

                }
            }
        }
    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" + e);

    }
}


public void setOnSmsReceivedListener(Context context) {
    this.onSMSReceived = (OnSMSReceived) context;
}


public interface OnSMSReceived {
    void onSMSReceived(String pin);
}

}

4 个答案:

答案 0 :(得分:1)

您必须制作广播接收器并在活动中注册。 通过其onReceive方法,您可以执行UI更改。 或者,如果有意图,您可以从意图中获取数据或包。

BroadcastReceiver receiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    TextView tv=(TextView)findViewById(R.id.tv);
    tv.setText("Hello");
  }
};

@Override
protected void onResume() {
  super.onResume();
  registerReceiver(receiver, new IntentFilter("my.custom.action"));
}

@Override
protected void onPause() {
  super.onPause();
  unregisterReceiver(receiver);
}

答案 1 :(得分:1)

onSMSReceived

中设置变量IncomingMessage静态

setOnSmsReceivedListener方法设为静态,并将其签名更改为:

public static void setOnSmsReceivedListener(OnSMSReceived onSMSReceived) {
onSMSReceived = onSMSReceived;
}

您的界面:(更改方法名称以避免混淆)

public interface OnSMSReceived {
void smsReceived(String pin);
}

然后当你的senderNum匹配时,执行此操作,(在获得引脚后)

onSMSReceived.smsReceived(pin);

在你的活动中这样做:

IncomingMessage.setOnSmsReceivedListener(new OnSMSReceived(){
        @Override
        public void messageReceived(String pin) {
          // get the pin from EditText which is entered by user
          // match it with the one you are getting here
        }
})

答案 2 :(得分:0)

您可以在Activity中创建动态BroadcastReceiver:

BroadcastReceiver receiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Received Broadcast", Toast.LENGTH_SHORT).show();
  }
};

@Override
protected void onResume() {
  super.onResume();
  // Register the receiver
  String action = "com.SOME_BROADCAST";
  registerReceiver(mReceiver, new IntentFilter(action));
}

@Override
protected void onPause() {
  super.onPause();
  // Unregister the receiver to save unnecessary system overhead
  // Paused activities cannot receive broadcasts anyway
  unregisterReceiver(mReceiver);
}

答案 3 :(得分:0)

您已在BroadcastReceiver类中定义了侦听器。那个听众的目的是什么?

您实际将数据传递给Activity的方式

  • 利用监听器,当您收到短信时,会通知听众。
  • 将BroadcastReceiver作为Activity的内部类。
  • 我认为,您也可以使用Intents将数据传递给Activity。