Android 7意图附加功能缺失

有没有人知道 Android 7.0(Nougat)与Android 6.0(Lollipop)相比如何处理意图附加功能有什么变化?

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

该应用程序创建一个待定意图,意图是具有附加功能自定义广播接收器.但是,在Android 7上,广播接收器接收的意图中没有任何附加内容.

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

堆栈跟踪显然会将NullPointerException作为崩溃的原因.
如果它会在所有版本之间崩溃,那就不会那么奇怪,但在这种情况下它只是最新的android.有没有人有任何想法吗?

注意:我尝试使用不同的标志创建挂起的意图,包括(0,PendingIntent.FLAG_UPDATE_CURRENT,PendingIntent.FLAG_CANCEL_CURRENT)仍然得到完全相同的结果.

解决方法

自定义Parcelable放在PendingIntent中从来就不是特别可靠,而且 it flat-out will not work in an AlarmManager PendingIntent on Android 7.0.其他进程可能需要在Intent中填入值,这涉及到操作附加内容,并且除了您自己的进程外无法在任何进程中完成没有其他进程具有您的自定义Parcelable类.

This SO answer有一种解决方法,其形式是将Parcelable转换为/从byte []转换.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...