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

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

这是问题所在.我的程序在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/115145

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

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

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

相关推荐