我是android的初学者,只需使用以下代码在mainActivity
中运行闹钟管理器:
Intent intent = new Intent(testSendWithFood.this, AlarmReciever.class);
intent.putExtra("key", "Alert");
//PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 10, 40 * 1000, pendingIntent);
我希望在另一个活动中使用以下代码杀死该警报管理器:
Intent intent = new Intent(this, AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
但警报经理不要杀!为什么?谢谢。
答案 0 :(得分:2)
未决意图必须相同。因此,如果您在创建intent时将intent id更改为1253,然后使用相同的方法取消挂起的intent,那么更改
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),0, intent, 0);
到
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 1253, intent, 0);