WakefulBroadcastReceiver 不适用于 Nougat 和 Oreo 设备
WakefulBroadcastReceiver is not working on Nougat and Oreo devices
我有一个闹钟应用程序,它使用闹钟管理器设置闹钟。一旦闹钟响起,接收器就会接收到它,该接收器会启动处理闹钟的唤醒服务,并允许用户停止或暂停闹钟。
下面是代码片段
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, final Intent intent) {
Alarm alarm = intent.getParcelableExtra(Constants.ARGS_ALARM);
}
}
我在 Android 的旧版本中得到了正确的值,但在牛轧糖和奥利奥上得到的警报为空。我的应用程序支持 minSdkVersion 17 的牛轧糖。
这段代码可能有什么问题?
Code that used
custom Parcelable
objects with AlarmManager
that might have worked
on older versions of Android will not work on Android N.
Matthias Urhahn
pointed out
[a] workaround: convert the Parcelable
into a byte[]
yourself,
storing that in the Bundle
. Then, the OS process will just treat
it as some random byte[]
. This Stack Overflow answer
shows the technique.
This sample project 也说明了这种技术。
我有一个闹钟应用程序,它使用闹钟管理器设置闹钟。一旦闹钟响起,接收器就会接收到它,该接收器会启动处理闹钟的唤醒服务,并允许用户停止或暂停闹钟。
下面是代码片段
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, final Intent intent) {
Alarm alarm = intent.getParcelableExtra(Constants.ARGS_ALARM);
}
}
我在 Android 的旧版本中得到了正确的值,但在牛轧糖和奥利奥上得到的警报为空。我的应用程序支持 minSdkVersion 17 的牛轧糖。
这段代码可能有什么问题?
Code that used custom
Parcelable
objects withAlarmManager
that might have worked on older versions of Android will not work on Android N.Matthias Urhahn pointed out [a] workaround: convert the
Parcelable
into abyte[]
yourself, storing that in theBundle
. Then, the OS process will just treat it as some randombyte[]
. This Stack Overflow answer shows the technique.
This sample project 也说明了这种技术。