Kyle
Kyle

Reputation: 2379

Starting an activity from BroadcastReceiver getExtras is null

I've been toying with how to start a new intent in my main class from my BroadcastReceiver. Thankfully, after many attempts and some help from SO I successfully triggered the onNewIntent(). However, I need to pass data to the intent so I can do something with it. Below you will see the code in my BroadcastReceiver that starts the intent and the code that handles the intent from my main class. The expected result would be to print out the passed value from the BroadcastReceiver, instead, I get a null exception. I have put a breakpoint and can verify that both the intent and getIntent (which should be the original intent not the intent that was passed in from the BroadcastReceiver) have null extras. Any help would be appreicated.

BroadcastReceiver:

Intent intentMain = new Intent(context.getApplicationContext(), MainActivity.class);
intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("com.xxx.xxx.FROM", "test");
intent.putExtra("com.xxx.xxx.MSG", "test2");
context.startActivity(intentMain);

onNewIntent():

@Override
    protected void onNewIntent(Intent intent) {
        Log.d("SmsReceiver", "entered onNewIntent");
        super.onNewIntent(intent);
        if (intent.getExtras() != null) {
            String from = intent.getExtras().getString("com.xxx.xxx.FROM");
            String msg = intent.getExtras().getString("com.xxx.xxx.MSG");
        }
    }

Upvotes: 0

Views: 153

Answers (1)

starkshang
starkshang

Reputation: 8528

You create intentMain,but put extra with intent.putExtra("com.xxx.xxx.MSG", "test2");it should be intentMain.putExtra("com.xxx.xxx.MSG", "test2");.

Upvotes: 1

Related Questions