微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 []转换.

原文地址:https://www.jb51.cc/android/316383.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐