我试图在我的意图中添加一条额外的消息以传递给 AlarmManager 以便稍后触发。我的 onReceive 触发正确,但 extras.getString() 返回 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"));
}
}
}
当此触发时,消息为空:
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);
}
}
}