0

In my Application I have a Broadcast receiver (registered in manifest) class from which I want to send an Intent to MainActivity. Therefore I have another broadcast receiver (dynamically registered) in MainActivity and an Intent Filter. But I don't receive the Intent in Main Activity. This is the code:

public class SmsReceiver extends BroadcastReceiver {

    public static final String SMS_BUNDLE = "pdus";

    public SmsReceiver(){

    }
        String TAG = SmsReceiver.class.getSimpleName();
        @Override
        public void onReceive(Context context, Intent intent){
            Bundle bundle = intent.getExtras();

            if (bundle != null) {
                Object[] sms = (Object[]) bundle.get(SMS_BUNDLE);
                String str = "";

                for (int i=0; i < sms.length; i++) {
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
                    String smsbody = smsMessage.getMessageBody().toString();
                    str += smsbody;
                }
                Intent bcIntent = new Intent();
                bcIntent.setAction("SMS_RECEIVED_ACTION");
                bcIntent.addCategory(Intent.CATEGORY_DEFAULT);
                bcIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
                bcIntent.putExtra("message", str);
                context.sendBroadcast(bcIntent);
            }
        }
    }

and in the MainActivity:

public class MainActivity extends AppCompatActivity
{
public boolean receivedSMS;
public String displaySMS;

  private BroadcastReceiver iReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    String qqq = intent.getExtras().getString("message");
    info2(intent.getExtras().getString("message"));
    evalMsg(qqq);
  }
};

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    graphInit();
    timerInit();
    smnInit();

  }

  @Override
  protected void onResume(){

  super.onResume();
  IntentFilter iFilter = new IntentFilter();
    iFilter.addAction("SMS_RECEIVED_ACTION");

    registerReceiver(iReceiver, iFilter);

  }

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

Does anyone know why the broadcast receiver in the main activity doesnt receive the intent? There should be a text displayed in the TextView but it is never shown. thanks for any idea

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
pluto
  • 1

1 Answers1

0

When receiving the intent via SmsReceiver you can't know if the Activity is up and in what state. What you should consider is using startActivity(android.content.Intent) from within your SmsReceiver.onReceive(). This way you'll have the same intent object available within Activity.onCreate (just call the getIntent() method)

But if you want to stick to your original design of double broadcasts, then here is your answer.

Arseny Levin
  • 664
  • 4
  • 10