0

PendinIntentAlarmManager을 사용하여 일정 기간 후에 다른 활동을 시작하는 서비스가 있습니다. 서비스가 나는 또한 활동을 원치 않는 사용자 넣다 서비스를 취소 할 수있는 버튼이있는 알림을 설정 시작되면AlarmManager가 관련 서비스를 취소 한 후에도 여전히 알람을 발생시키고 PendingIntent

 Calendar cal = Calendar.getInstance(); //Create a calendar 
     cal.add(Calendar.MINUTE, 1);   //Add the set minutes to the alarm 

     Intent dialogIntent = new Intent(this, alarmRingLayout.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 1234, dialogIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

     AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE); 
     am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent); 

: 여기

는 서비스의 관련 코드 쏘다. "취소"버튼을 클릭에 는 stopService()가 호출됩니다 :

stopService(new Intent(StopPowerNapAlarmService.this, PowerNapAlarmService.class));

onDestroy()는 통지를 취소하고 취소 stopSelf() 그것은 또한 시도 호출을 통해 다음과 같은 코드를 가지고 PendingIntentAlarmManager.

문제는 onDestroy이 호출 된 후에도 활동이 열리는 문제입니다. PendingIntent 및/또는 AlarmManager이 취소되지 않는 것으로 보입니다.

@Override 
    public void onDestroy() { 
     Toast.makeText(this, "Alarm Finished", Toast.LENGTH_SHORT).show(); 
     CancelNotification(this, 0); 

     //Cancel the pending intent and AlarmManager 
     Intent myntent = new Intent(PowerNapAlarmService.this, PowerNapAlarmService.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 
       1234, myntent, PendingIntent.FLAG_UPDATE_CURRENT); 
     pendingIntent.cancel(); 
     AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
     am.cancel(pendingIntent); 

     stopSelf(); 
    } 

잘못 이상 무슨 것입니다 : 여기

onDestroy() 코드입니다?

답변

2

여기에 무슨 문제가 있습니까?

Intent 개체가 다르므로 PendingIntent 개체가 다릅니다. 이것은 차례로 다른 경보로 작업하고 있음을 의미합니다. 첫 번째 IntentalarmRingLayout.class 활동을 가리 킵니다. 두 번째 IntentPowerNapAlarmService이라는 이상한 이름의 BroadcastReceiver을 가리 킵니다. 당신이 alarmRingLayout 알람을 cancel()하려면, alarmRingLayout위한 활동 PendingIntent을 만들고, cancel()와 그 사용

.

또한 stopSelf()onDestroy()으로 제거하십시오. 필요하지 않으므로 문제가 발생할 수 있습니다.

관련 문제