android–Pendingintent getbroadcast丢失了可分配的数据

android–Pendingintent getbroadcast丢失了可分配的数据,第1张

概述这是问题所在.我的程序在Android6.0中运行完美.将设备更新到android7.0后.Pendingintent无法将可分配数据传递给boradcastreveiver.这是代码.发出警报publicstaticvoidsetAlarm(@NonNullContextcontext,@NonNullTodotodo){AlarmManageralarmManager=(Ala

这是问题所在.我的程序在Android 6.0中运行完美.将设备更新到androID 7.0后. Pendingintent无法将可分配的数据传递给boradcast reveiver.这是代码.

发出警报

public static voID setAlarm(@NonNull Context context, @NonNull Todo todo) {    AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);    Intent intent = new Intent(context, AlarmReceiver.class);    intent.putExtra("KEY_Todo", todo);    PendingIntent alarmIntent = PendingIntent.getbroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);    alarmManager.set(AlarmManager.RTC_WAKEUP, todo.remindDate.getTime(), alarmIntent);}

Todo是一个Parcelable类,而todo是我在通知中需要的实例.

在broadcastreceiver中,我无法获取可用的数据.

public voID onReceive(Context context, Intent intent) {    Todo todo = intent.getParcelableExtra("KEY_Todo");}

这是我调试时的意图结果
enter image description here

我不知道为什么意图只包含一个我从未放入的Integer.Carcelable todo在哪里.
此代码在AndroID 6.0中没有问题,但无法在7.0中运行

解决方法:

引用myself:

Custom Parcelable classes — ones unique to your app, not a part
of the AndroID framework — have had intermittent problems over
the years when used as Intent extras. Basically, if a core OS process
needs to modify the Intent extras, that process winds up trying
to recreate your Parcelable objects as part of setting up the
extras Bundle for modification. That process does not have your
class and so it gets a runtime exception.

One area where this can occur is with AlarmManager. Code that used
custom Parcelable objects with AlarmManager that might have worked
on older versions of AndroID 07001.

我所知道的最有效的解决方法是手动将Parceable转换为byte []并将其放入Intent extra中,根据需要手动将其转换回Parcelable. This Stack Overflow answer
显示了该技术,this sample project提供了完整的工作样本.

关键位是Parcelable和byte []之间的转换:

/*** copyright (c) 2016 CommonsWare, LLC licensed under the Apache license, Version 2.0 (the "license"); you may not use this file except in compliance with the license. You may obtain a copy of the license at http://www.apache.org/licenses/liCENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implIEd. See the license for the specific language governing permissions and limitations under the license. From _The Busy Coder's GuIDe to AndroID Development_ https://commonsware.com/AndroID */package com.commonsware.androID.parcelable.marshall;import androID.os.Parcel;import androID.os.Parcelable;// inspired by https://stackoverflow.com/a/18000094/115145public class Parcelables {  public static byte[] toByteArray(Parcelable parcelable) {    Parcel parcel=Parcel.obtain();    parcelable.writetoParcel(parcel, 0);    byte[] result=parcel.marshall();    parcel.recycle();    return(result);  }  public static <T> T toparcelable(byte[] bytes,                                   Parcelable.Creator<T> creator) {    Parcel parcel=Parcel.obtain();    parcel.unmarshall(bytes, 0, bytes.length);    parcel.setDataposition(0);    T result=creator.createFromParcel(parcel);    parcel.recycle();    return(result);  }}
总结

以上是内存溢出为你收集整理的android – Pendingintent getbroadcast丢失了可分配的数据全部内容,希望文章能够帮你解决android – Pendingintent getbroadcast丢失了可分配的数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1120089.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存