Android 无法通过 AlarmManager 传递 Intent Extras [英] Android cannot pass intent extras though AlarmManager

查看:22
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆