待定getbroadcast丢失了可打包数据 [英] Pendingintent getbroadcast lost parcelable data
问题描述
这是问题所在.我的程序在Android 6.0中运行完美.将设备更新为android 7.0之后. Pendingintent无法将可打包数据传递给广播广播接收器.这是代码.
Here is the problem. My program is running perfect in Android 6.0. After update the device to android 7.0. Pendingintent can not pass the parcelable data to boradcast reveiver. Here is the code.
发出警报
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是我在通知中需要的实例.
Todo is a Parcelable class while todo is the instance I need in notification.
在Broadcastreceiver中,我无法获取可拆分数据.
In Broadcastreceiver, I cannot getParcelable data.
public void onReceive(Context context, Intent intent) {
Todo todo = intent.getParcelableExtra("KEY_TODO");
}
这是我调试时意图的结果
Here is the result of intent when I debug
我不知道为什么该意图只包含一个我从未放入过的Integer.Parcelable待办事项在哪里. 此代码在android 6.0中没有问题,但不能在7.0中运行
I dont know why the intent only contains a Integer that I never put it in. Where is the Parcelable todo. This code has no problem in android 6.0, but can not run in 7.0
推荐答案
引用
自定义 Custom 使用 One area where this can occur is with 我知道,最有效的解决方法是将自己手动将 The most efficient workaround that I know of is to manually convert the 关键位是 The key bits are the conversions between the 这篇关于待定getbroadcast丢失了可打包数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!Parcelable
类—您的应用独有的内容,而不是一部分
Android框架—曾经有过间歇性的问题
用作Intent
附加项目的年份.基本上,如果是核心操作系统进程
需要修改Intent
附加功能,该过程最终会尝试
重新建立您的Parcelable
对象,作为设置
附加内容Bundle
进行修改.该过程没有您的
类,因此它会获得运行时异常.
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.AlarmManager
可以发生这种情况.使用的代码
具有AlarmManager
的自定义Parcelable
对象,这些对象可能已经起作用
在较旧版本的Android 无法在Android N上运行./p>
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
附加项中,然后根据需要手动将其转换回Parcelable
. 此堆栈溢出答案
显示了该技术,并且此示例项目提供了完整的工作示例Parceable
yourself into a byte[]
and put that in the Intent
extra, manually converting it back into a Parcelable
as needed. This Stack Overflow answer
shows the technique, and this sample project provides a complete working sample.Parcelable
和byte[]
之间的转换:Parcelable
and the 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);
}
}