gpt4 book ai didi

android - Pendingintent getbroadcast lost parcelable data

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:47 25 4
gpt4 key购买 nike

问题来了。我的程序在 Android 6.0 上运行完美。将设备更新到 android 7.0. Pendingintent 无法将 parcelable 数据传递给 boradcast receiver。这是代码。

拉响警报

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 中,我无法获取 Parcelable 数据。

public void onReceive(Context context, Intent intent) {

Todo todo = intent.getParcelableExtra("KEY_TODO");

}

这是我调试时的intent结果

enter image description here

我不知道为什么 Intent 只包含一个我从未放入过的整数。Parcelable 待办事项在哪里。这段代码在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 will not work on Android N.

据我所知,最有效的解决方法是自己手动将 Parceable 转换为 byte[] 并将其放入 Intent额外的,根据需要手动将其转换回 ParcelableThis Stack Overflow answer展示了技术,this sample project提供完整的工作示例。

关键位是Parcelablebyte[]之间的转换:

/***
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);
}
}

关于android - Pendingintent getbroadcast lost parcelable data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39478422/

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