即使传递了相同的意图ID,我也无法检索相同的意图。
我已经检查过广播接收器在创建待定意图时使用了完全相同的上下文。
OnClientClick="return callJavascriptFunction()"
创建PendingIntent。
public class AlarmReceiver extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
public static MediaPlayer mMediaPlayer;
public static NotificationManager notificationManager;
public static Notification notification;
public static int id;
Context ctx;
@Override
public void onReceive(final Context context, Intent intent) {
ctx = context;
Log.d("WTF", ""+intent.getAction());
if (intent.getAction() != null) {
String action = intent.getAction();
switch (action) {
case "SNOOZE":
Log.v("shuffTest", "Pressed Snoozed");
break;
case "STOP_ACTION":
Log.v("shuffTest", "Pressed Stop");
mMediaPlayer.stop();
notificationManager.cancel(id);
stopReceiverServices();
break;
}
} else {
notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
playAlarmSound();
}
// This is the Intent to deliver to our service.
//Intent service = new Intent(context, SimpleWakefulService.class);
// Start the service, keeping the device awake while it is launching.
//startWakefulService(context, service)
}
public void stopReceiverServices(){
Intent notificationIntent = new Intent(ctx.getApplicationContext(), AlarmReceiver.class);
notificationIntent.putExtra(AlarmReceiver.NOTIFICATION_ID, 123);
notificationIntent.putExtra(AlarmReceiver.NOTIFICATION, notification);
Log.d("TAG", "Context A1: "+ctx.getApplicationContext().toString());
Log.d("TAG", "Context A2: "+ctx.toString());
AlarmManager alarmManager = (AlarmManager) ctx.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Log.d("TAG", "AlarmManager Before: " + alarmManager);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx.getApplicationContext(), 987654321, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Log.d("TAG", "PendingIntent Before: " + pendingIntent);
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
Log.d("TAG", "AlarmManager After: " + alarmManager);
Log.d("TAG", "PendingIntent After: " + pendingIntent);
}
/..
}
Logcat:
void setAlarm(Context context) {
mContext = context;
Intent notificationIntent = new Intent(context, AlarmReceiver.class);
notificationIntent.putExtra(AlarmReceiver.NOTIFICATION_ID, 123);
notificationIntent.putExtra(AlarmReceiver.NOTIFICATION, getNotification("Wake Up! Wake Up"));
AlarmManager alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 987654321, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minuteOfHour);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Log.d("TAG", "AlarmManager After setting : " + alarmManager);
Log.d("TAG", "PendingIntent After setting : " + pendingIntent);
Log.d("Ctx setAlarm1 ", context.getApplicationContext().toString());
}
答案 0 :(得分:1)
在alarmManager和PendingIntent中删除getApplicationConext()
void setAlarm(Context context) {
...
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context), 987654321, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
...
}