gpt4 book ai didi

缺少 Android 7 Intent 附加功能

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:12:28 24 4
gpt4 key购买 nike

有谁知道与 Android 6.0 (Lollipop) 相比,Android 7.0 (Nougat) 处理 intent extras 的方式是否有任何变化?

长话短说:我的应用在从 4.1(16) 到 6.0(23) 的所有版本上都按预期运行,但在 android 7.0(24) 上崩溃了!

该应用程序创建一个挂起的 Intent ,该 Intent 具有附加功能的自定义广播接收器。但是,在 android 7 上,广播接收器接收到的 Intent 中没有任何额外内容。

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。请问有人有什么想法吗?

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

最佳答案

将自定义 Parcelable 放在 PendingIntent 中从来都不是特别可靠,并且 it flat-out will not work in an AlarmManager PendingIntent on Android 7.0 .其他进程可能需要将值填充到 Intent 中,这涉及到操作 extras,这不能在任何进程中完成,除了你自己的,因为没有其他进程有你的自定义 Parcelable 类。

This SO answer有一个变通方法,其形式是自己将 Parcelable 转换为 byte[]

关于缺少 Android 7 Intent 附加功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39573849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com