本文介绍了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
发布评论