Ожидание тревоги не отменяется
Я прочитал много вопросов и ответов по Stackoverflow, и многие из них просто подчеркивают.cancel()
и специальный уникальный идентификатор. Однако теперь, сколько бы раз я ни пытался, я просто не могу отменить это.
Мой уникальный идентификатор
final static int RQS_1 = 1337;
Моя функция setAlarm. pickTime - текущая активность, а timesUp - другаяService
класс, который показывает тост, когда время истекло.
Intent intent = new Intent(pickTime.this, timesUp.class);
PendingIntent timesUpIntent = PendingIntent.getService(pickTime.this, RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
timesUpIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), timesUpIntent);
Моя функция cancelAlarm
Intent intent = new Intent(this, pickTime.class);
PendingIntent timesUpIntent = PendingIntent.getBroadcast(this, RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (timesUpIntent != null) {
alarmManager.cancel(timesUpIntent);
timesUpIntent.cancel();
Toast.makeText(getApplicationContext(), "Alarm is cancelled",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Unable to stop timer",
Toast.LENGTH_SHORT).show();
}
Мой разService
public class timesUp extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG)
.show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG)
.show();
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG)
.show();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG)
.show();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG)
.show();
return super.onUnbind(intent);
}
}