Android无法通过额外的意图虽然AlarmManager

编程入门 行业动态 更新时间:2024-10-24 01:48:09
本文介绍了Android无法通过额外的意图虽然AlarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图把一个额外的消息,在我的意图传递给AlarmManager在以后的时间被触发。我的onReceive正确触发,但extras.getString()返回null

设置:

公共PendingIntent getPendingIntent(INT uniqueRequest code,字符串额外的){     意向意图=新的意图(这一点,ActionReceiver.class);     intent.putExtra(额外的,另计);     PendingIntent pendingIntent = PendingIntent.getBroadcast(这一点,uniqueRequest code,             意图,0);     返回pendingIntent; } 公共无效setSilentLater(TimeRule timeRule){     布尔[]平日= timeRule.getReoccurringWeekdays();     INT DAYOFWEEK = 0;     对于(布尔日:周一至周五){         一周中的某天++;         如果(天==真){             日历CAL = Calendar.getInstance();             AlarmManager alarmManager =(AlarmManager)本                     .getSystemService(Context.ALARM_SERVICE);             cal.set(Calendar.DAY_OF_WEEK,星期几);             cal.set(Calendar.HOUR_OF_DAY,                     。timeRule.getStartTime()得到(Calendar.HOUR_OF_DAY));             cal.set(Calendar.MINUTE,                     。timeRule.getStartTime()得到(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));   }  } }

在此触发,消息是空的:

公共类ActionReceiver扩展的BroadcastReceiver {     @覆盖     公共无效的onReceive(上下文的背景下,意图意图){          捆绑额外= intent.getExtras();          字符串消息= extras.getString(额外); //空          如果(消息==RINGER_OFF)          {              AudioManager上午=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);              am.setRingerMode(AudioManager.RINGER_MODE_SILENT);          }          否则,如果(消息==RINGER_ON)          {              AudioManager上午=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);              am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);          }     } }

解决方案

//前更换code     捆绑额外= intent.getExtras();     字符串消息= extras.getString(额外); //后更换code      字符串消息= intent.getStringExtra(额外);                      要么 //前替换code    意向意图=新的意图(这一点,ActionReceiver.class);    intent.putExtra(额外的,另计); //后更换code    意向意图=新的意图(这一点,ActionReceiver.class);    束束=新包();    bundle.putString(额外的,另计);    intent.putExtras(包);

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

Setup:

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

解决方案

// before replace code Bundle extras = intent.getExtras(); String message = extras.getString("EXTRA"); // after replace code String message = intent.getStringExtra("EXTRA"); OR // before replace code Intent intent = new Intent(this, ActionReceiver.class); intent.putExtra("EXTRA", extra); // after replace code Intent intent = new Intent(this, ActionReceiver.class); Bundle bundle = new Bundle(); bundle.putString("EXTRA", extra); intent.putExtras(bundle);

更多推荐

Android无法通过额外的意图虽然AlarmManager

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

发布评论

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

>www.elefans.com

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