缺少Android 7意向附加功能 [英] Android 7 intent extras missing

本文介绍了缺少Android 7意向附加功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与Android 6.0(棒棒糖)相比,有人知道Android 7.0(牛轧糖)在处理意向额外内容方面是否有任何变化?

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

长话短说:我的应用程序可以在4.1(16)到6.0(23)的所有版本上正常工作,但是在android 7.0(24)上崩溃!

Long story short: my app works as intended on all versions from 4.1(16) to 6.0(23) but crashes on android 7.0(24)!

该应用程序会创建一个待定意图,该意图的目的是具有额外功能的自定义广播接收器.但是,在android 7上,广播接收器接收到的意图中没有任何额外功能.

The app creates a pending intent with an intent to a custom broadcast receiver which has extras. However, on android 7 none of the extras are present in the intent received by the broadcast receiver.

MainActivity.java

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

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

堆栈跟踪显然使NullPointerException成为崩溃的原因. 如果它在所有版本中崩溃都不会那么奇怪,但是在这种情况下,它仅是最新的android.请问有人有想法吗?

Stack trace obviously gives the NullPointerException as the cause of crash. It would not be so weird if it would crash among all versions, but in this case its the latest android only. Has anyone got any ideas please?

注意:我尝试创建带有(0PendingIntent.FLAG_UPDATE_CURRENTPendingIntent.FLAG_CANCEL_CURRENT)不同标志的待定意图仍然得到完全相同的结果.

Note: I have tried creating pending intents with different flags including (0, PendingIntent.FLAG_UPDATE_CURRENT, PendingIntent.FLAG_CANCEL_CURRENT) still got the exact same result.

推荐答案

PendingIntent中放入自定义Parcelable从未如此可靠,并且

Putting a custom Parcelable in a PendingIntent has never been especially reliable, and it flat-out will not work in an AlarmManager PendingIntent on Android 7.0. Other processes may need to fill in values into the Intent, and that involves manipulating the extras, and that can't be done in any process but your own, since no other process has your custom Parcelable class.

This SO answer has a workaround, in the form of converting the Parcelable yourself to/from a byte[].

这篇关于缺少Android 7意向附加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆