Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data from BroadcastReceiver to an Activity? [duplicate]

I am having a BroadcastReceiver class which reads incoming sms and extracting a pin from the sms. I want to pass that sms to an activity which is already visible to user. User should enter the pin in an EditText, I am taking the value from the EditText in , Compare the Pin entered by the user with the Pin passed by the BroadcastReceiver. If both match to each other, user can go into the app.But I don't know how to pass that Pin received by the BroadcastReceiver into the Activity. Here is the code

BroadcastReceiver class

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);
}

}

like image 398
Panduka Wedisinghe Avatar asked Mar 11 '23 03:03

Panduka Wedisinghe


2 Answers

You can create your dynamic BroadcastReceiver within the Activity:

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);
}
like image 101
lidox Avatar answered Apr 06 '23 15:04

lidox


You have to make a broadcast receiver and register it in activity. From its onReceive methods you can perform UI changes. Or you can get the data or bundle from the intent if there is one.

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);
}
like image 37
Shashank Yadav Avatar answered Apr 06 '23 17:04

Shashank Yadav