Intent.getAction () throws Null + Android

I wrote BroadcastReceiver for my Alarm app. In the onReceive method, I read Intent.getAction (). It works well in all versions *, except for the SDK version 1.5, where it throws an exception from the null pointer * . I set the action in another action, where I call broadcastReceiver. Please help me with this. Below is a code snippet for receiver classes and activity,

ProfileActivity.java


public static final String STARTALARMACTION = "android.intent.driodaceapps.action.STARTPROFILE"; public static final String STOPALARMACTION = "android.intent.driodaceapps.action.STOPPROFILE"; AlarmManager alarmMan = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent startIntent = new Intent(this,AlarmReceiver.class); startIntent.setAction(STARTALARMACTION); Intent stopIntent = new Intent(this,AlarmReceiver.class); stopIntent.setAction(STOPALARMACTION+intentposition); 

AlarmReceiver.java

 @Override public void onReceive(Context context,Intent intent){ String intentAction = intent.getAction(); Log.d(TAG,"Intent:"+intentAction); **//throwing null** 

After receiving the error, I tried my luck by following the steps in the .manifest file. But to no avail.

Please help me.

Thanks.

+6
android
source share
2 answers

From the docs for BroadcastReceiver.onReceive () ...

Intent filters used in registerReceiver (BroadcastReceiver, IntentFilter) and in the application manifest are not guaranteed to be exclusive .

For this reason, onReceive () implementations should only respond to known actions, ignore any unexpected intentions that they may receive.

I don't fully understand the implications of any of the above statements, but it seems like the second of two is that the onReceive() handler onReceive() to be written to handle the unexpected.

Remember that intentions do not need to have an associated β€œaction” (they can simply be used to transfer data between different objects). Intent.getAction() will return null if Intent has not set an action and attempting Log intentAction will cause an NPE.

Are you sure that the intention is your own and not the other that has found the way to your receiver? I am not familiar with v1.5, but it is possible that everything could work differently - hardly possible, but possible.

I think that just try just checking the intent to see if it is null and ignore it if it is (i.e. exit the onReceive () handler, assuming the intent is not your own). Worth trying.

+5
source share

Following @Squonk's excellent answer, I put the following code in my BroadcastReciever onReceive :

 if (intent != null) { if(intent.getAction() != null){ Log.i(TAG, "Intent: " + intent.getAction()); }else{ Log.i(TAG, "Intent: !null, Action: null"); } }else{ Log.i(TAG, "Intent: null"); } 

My receiver starts up every 60 seconds, but something else calls it:

 12-03 08:42:05.566 Intent: WEEKLY_CHECK 12-03 08:42:32.990 Intent: !null, Action: null 12-03 08:42:33.175 Intent: WEEKLY_CHECK 12-03 08:43:32.990 Intent: WEEKLY_CHECK 12-03 08:43:33.025 Intent: !null, Action: null 12-03 08:43:33.057 Intent: WEEKLY_CHECK 12-03 08:44:32.990 Intent: WEEKLY_CHECK 12-03 08:44:33.069 Intent: !null, Action: null 12-03 08:44:33.102 Intent: WEEKLY_CHECK 

Thus, confirmation of what is indicated in the documents.

+1
source share

All Articles