为什么不杀掉android中的警报管理器?

时间:2015-11-12 07:55:27

标签: android

我是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);


但警报经理不要杀!为什么?谢谢。

1 个答案:

答案 0 :(得分:2)

未决意图必须相同。因此,如果您在创建intent时将intent id更改为1253,然后使用相同的方法取消挂起的intent,那么更改

PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),0, intent, 0);

PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 1253, intent, 0);