Android 无法通过 AlarmManager 传递 Intent Extras

编程入门 行业动态 更新时间:2024-10-23 11:16:35
本文介绍了Android 无法通过 AlarmManager 传递 Intent Extras的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图在我的意图中添加一条额外的消息,以传递给 AlarmManager 以便稍后触发.我的 onReceive 正确触发但 extras.getString() 返回 null

I am trying to put an extra message in my intent to pass to AlarmManager to be triggered at a later time. My onReceive triggers correctly but extras.getString() returns null

设置:

public PendingIntent getPendingIntent(int uniqueRequestCode, String extra) { Intent intent = new Intent(this, ActionReceiver.class); intent.putExtra("EXTRA", extra); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueRequestCode, intent, 0); return pendingIntent; } public void setSilentLater(TimeRule timeRule) { boolean[] weekdays = timeRule.getReoccurringWeekdays(); int dayOfWeek = 0; for (boolean day : weekdays) { dayOfWeek++; if (day == true) { Calendar cal = Calendar.getInstance(); AlarmManager alarmManager = (AlarmManager) this .getSystemService(Context.ALARM_SERVICE); cal.set(Calendar.DAY_OF_WEEK, dayOfWeek); cal.set(Calendar.HOUR_OF_DAY, timeRule.getStartTime().get(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, timeRule.getStartTime().get(Calendar.MINUTE)); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 3600000 * 7, getPendingIntent(0, "RINGER_OFF")); } } }

当这个触发时,消息为空:

When this triggers, message is empty:

public class ActionReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); String message = extras.getString("EXTRA"); //empty if(message == "RINGER_OFF") { AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); am.setRingerMode(AudioManager.RINGER_MODE_SILENT); } else if(message == "RINGER_ON") { AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); am.setRingerMode(AudioManager.RINGER_MODE_NORMAL); } } }

推荐答案

我有一些可以帮助其他人的精确度,可以与某个地方的解决方案相关联.如果您将自定义 Parcelable 对象作为 extra 传递,操作系统可能无法处理它们,因此会发生内部异常并且您的 extras 丢失.

I have some precisions that could help others, to be associated with the solution from Someone Somewhere. If you pass custom Parcelable objects as an extra, the operating system may not be able to process them, so an internal exception occurs and your extras are lost.

使用 Android N,即使使用 PendingIntent.FLAG_UPDATE_CURRENT 我也无法检索我的自定义 Pacelable extras.

With Android N, even with PendingIntent.FLAG_UPDATE_CURRENT I cannot retrieve my custom Pacelable extras.

所以我不得不使用系统已知的 Parcelable(如 ParcelUuid)来引用自定义数据库中的一些对象,而不是提供我的整个 Parcelable 对象.

So I had to use system known Parcelable (like ParcelUuid) to reference some objects in a custom database instead of providing my whole Parcelable object.

另一种解决方案是将 Parcelable 转换为系统正确识别的字节数组:如何在 Parcel 的帮助下将 Parcelable 编组和解组为字节数组?

Another solution is to convert Parcelable to a byte array that is correctly recognized by the system: How to marshall and unmarshall a Parcelable to a byte array with help of Parcel?

更多推荐

Android 无法通过 AlarmManager 传递 Intent Extras

本文发布于:2023-11-27 09:25:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637501.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AlarmManager   Android   Extras   Intent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!