Android 7 no intentions

Does anyone know if there are any changes regarding how Android 7.0 (Nougat) handles intentions compared to Android 6.0 (Lollipop)?

In short: my application works as intended on all versions from 4.1 (16) to 6.0 (23), but it crashes on android 7.0 (24)!

The application creates a pending intent with the intention of setting up a custom broadcast receiver with additional features. However, on Android 7, none of the additional features are present in the intent received by the broadcast receiver.

MainActivity.java

 Intent intent = new Intent(context, PollServerReceiver.class); // TODO: Remove after DEBUGGING is completed! intent.putExtra("TESTING1", "testing1"); intent.putExtra("TESTING2", "testing2"); intent.putExtra("TESTING3", "testing3"); // PendingIntent to be triggered when the alarm goes off. final PendingIntent pIntent = PendingIntent.getBroadcast(context, PollServerReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Setup alarm to schedule our service runs. AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarm.setRepeating(AlarmManager.RTC_WAKEUP, firstRun, freqMilis, pIntent); 

PollServerReceiver.java

 Bundle extras = intent.getExtras(); Log.d(TAG, "onReceive: TESTING1 = " + extras.getString("TESTING1")); // null here // None of the three "TESTING*" keys are there! for (String key : extras.keySet()) { Object value = extras.get(key); Log.d(TAG, String.format("onReceive extra keys: %s %s (%s)", key, value.toString(), value.getClass().getName())); } 

The stack trace obviously gives a NullPointerException as the reason for the failure. It would not be so strange if it crashed among all versions, but in this case its only the latest Android. Anyone have any ideas please?

Note. I tried to create pending intentions with different flags, including ( 0 , PendingIntent.FLAG_UPDATE_CURRENT , PendingIntent.FLAG_CANCEL_CURRENT ), still got the same result.

+7
android android-intent android-7.0-nougat android-broadcastreceiver android-pendingintent
source share
2 answers

Putting a custom Parcelable in a PendingIntent has never been particularly reliable, and it will not work in AlarmManager PendingIntent on Android 7.0 . Other processes may need to fill in the values ​​in Intent , and this includes managing additional functions, and this cannot be done in any process other than your own, since no other process has your custom Parcelable class.

This SO answer has a workaround in the form of Parcelable conversion itself to / from byte[] .

+14
source share

I had a similar problem, but I think I found an easy solution. Put your details in the Bundle and send this Bundle with your intentions. In my case, I wanted to send a serializable object with my intentions.

Alarm Setting:

 AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, AlarmReciever.class); Bundle bundle = new Bundle(); //creating an example object ExampleClass exampleObject = new ExampleClass(); //put the object inside the Bundle bundle.putSerializable("example", exampleObject); //put the Bundle inside the intent intent.putExtra("bundle",bundle); PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); //setup the alarm alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent); 

Get an alarm:

 public class AlarmReciever extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // get the Bundle Bundle bundle = intent.getBundleExtra("bundle"); // get the object ExampleClass exampleObject = (ExampleClass)bundle.getSerializable("example"); } } 

It worked for me. Hope this helps :)

+2
source share

All Articles