gpt4 book ai didi

android - 警报未决警报不会取消

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:57:58 26 4
gpt4 key购买 nike

我在 Stackoverflow 上通读了很多问题和答案,其中很多只是强调 .cancel() 和特殊的唯一 ID。但是,现在无论尝试多少次,我都无法取消它。


我的唯一ID

final static int RQS_1 = 1337;


我的 setAlarm 函数。 pickTime 是当前 Activity,而 timesUp 是另一个 Service 类,它在时间到了时显示 toast 。

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();
}

我的 timesUp 服务

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

}

}

最佳答案

要取消 Alarm,您必须创建与启动它时相同的 PendingIntent。你在做的同时开始,

Intent intent = new Intent(pickTime.this, timesUp.class);
PendingIntent timesUpIntent = PendingIntent
.getService(pickTime.this, RQS_1, intent, 0);

你正在做的同时取消,

Intent intent = new Intent(this, pickTime.class);
PendingIntent timesUpIntent = PendingIntent
.getBroadcast(this, RQS_1, intent, 0);

Are they same?

不,它们不一样。您正在为 Service 创建 PendingIntent 以启动并尝试使用 BroadCast 的不同 PendingIntent 取消。

关于android - 警报未决警报不会取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12058581/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com