报警挂起报警不会取消 [英] Alarm Pending Alarm doesn't cancel
问题描述
我已经经历了很多的问题和答案的StackOverflow阅读,其中许多只强调对 .cancel()
和特殊唯一的ID。不过,现在无论我有多少次尝试,我只是无法取消它。
我的唯一的ID
最终静态INT RQS_1 = 1337;
我setAlarm功能。 pickTime是当前活动,并且timesUp,是另一个服务
类,显示敬酒时,时间到了。
意向意图=新的意图(pickTime.this,timesUp.class);
PendingIntent timesUpIntent = PendingIntent.getService(pickTime.this,RQS_1,意向,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功能
意向意图=新的意图(这一点,pickTime.class);
PendingIntent timesUpIntent = PendingIntent.getBroadcast(这一点,RQS_1,意向,0);
AlarmManager alarmManager =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
如果(timesUpIntent!= NULL){
alarmManager.cancel(timesUpIntent);
timesUpIntent.cancel();
Toast.makeText(getApplicationContext(),报警被取消,
Toast.LENGTH_SHORT).show();
} 其他 {
Toast.makeText(getApplicationContext(),无法停止定时器,
Toast.LENGTH_SHORT).show();
}
我的timesUp 服务
公共类timesUp延伸服务{
@覆盖
公共无效的onCreate(){
// TODO自动生成方法存根
Toast.makeText(这一点,MyAlarmService.onCreate(),Toast.LENGTH_LONG)
。显示();
}
@覆盖
公众的IBinder onBind(意向意图){
// TODO自动生成方法存根
Toast.makeText(这一点,MyAlarmService.onBind(),Toast.LENGTH_LONG)
。显示();
返回null;
}
@覆盖
公共无效的onDestroy(){
// TODO自动生成方法存根
super.onDestroy();
Toast.makeText(这一点,MyAlarmService.onDestroy(),Toast.LENGTH_LONG)
。显示();
}
@覆盖
公共无效ONSTART(意向意图,诠释startId){
// TODO自动生成方法存根
super.onStart(意向,startId);
Toast.makeText(这一点,MyAlarmService.onStart(),Toast.LENGTH_LONG)
。显示();
}
@覆盖
公共布尔onUnbind(意向意图){
// TODO自动生成方法存根
Toast.makeText(这一点,MyAlarmService.onUnbind(),Toast.LENGTH_LONG)
。显示();
返回super.onUnbind(意向);
}
}
那么取消报警,你必须创建你在启动它创造相同的PendingIntent。 你这样做,而开始,
意向意图=新的意图(pickTime.this,timesUp.class);
PendingIntent timesUpIntent = PendingIntent
.getService(pickTime.this,RQS_1,意向,0);
您在做什么,而取消,
意向意图=新的意图(这一点,pickTime.class);
PendingIntent timesUpIntent = PendingIntent
.getBroadcast(这一点,RQS_1,意向,0);
他们是一样的吗?
没有,他们不是一样的。您正在创建 PendingIntent
为服务
启动,并试图取消与不同的 PendingIntent $ C $ç
广播
。
I've read through many questions and answers on Stackoverflow, and many of which just emphasize on the .cancel()
and the special unique ID. However, now matter how many times I tried, I just can't cancel it.
My unique ID
final static int RQS_1 = 1337;
My setAlarm Function. pickTime is current Activity, and timesUp, is another Service
class that shows a toast when the time is up.
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);
My cancelAlarm function
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();
}
My timesUp 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);
}
}
Well for cancelling Alarm you have to create the same PendingIntent that you created while starting it. You are doing while starting,
Intent intent = new Intent(pickTime.this, timesUp.class);
PendingIntent timesUpIntent = PendingIntent
.getService(pickTime.this, RQS_1, intent, 0);
You are doing while cancelling,
Intent intent = new Intent(this, pickTime.class);
PendingIntent timesUpIntent = PendingIntent
.getBroadcast(this, RQS_1, intent, 0);
Are they same?
No, they are not same. You are creating PendingIntent
for Service
to start and trying to cancel with different PendingIntent
of BroadCast
.
这篇关于报警挂起报警不会取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!