4

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

}

Nikhil
  • 3,711
  • 8
  • 32
  • 43
  • Hey, just create a Broadcast receiver class inside your required activity. Register and unregister it inside onResume and onPause respectively. Now when you are sending broadcast at that time use same action as you have given to broadcast when you created inside activity. – Rahul Sharma Sep 15 '16 at 13:08

4 Answers4

3

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);
}
lidox
  • 1,901
  • 3
  • 21
  • 40
2

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

Make variable onSMSReceived static in IncomingMessage

Make your setOnSmsReceivedListener method static and change it's signature as:

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

Your interface: (changed method name to avoid confusion)

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

then when your senderNum matches then do this, (after you get the pin)

onSMSReceived.smsReceived(pin);

And In your activity do this:

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

You already have a listener defined in your BroadcastReceiver class. What is the purpose of that listener?

The way you can actually pass the data to Activity

  • Make use of listener and when you receive SMS notify the listener about it.
  • Have BroadcastReceiver as inner class of Activity.
  • I think, you can also pass the data using Intents to Activity.
android_dev
  • 128
  • 7