gpt4 book ai didi

android - 如何取消警报管理器中的重复任务?

转载 作者:太空宇宙 更新时间:2023-11-03 11:15:16 24 4
gpt4 key购买 nike

我想使用警报管理器在我的应用程序启动时执行重复任务,但我在取消任务时遇到了问题。

这是我的代码:

服务:

public class SyncService extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("#####", "It's alive!");
return Service.START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
return null;
}
}

应用:

public class TerminalApplication extends Application {
PendingIntent pintent;
AlarmManager alarm;

@Override
public void onCreate() {
super.onCreate();
Intent intent = new Intent(this, SyncService.class);
pintent = PendingIntent.getService(this, 0, intent, 0);
alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Log.e("#####", "Starting alarm");
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, System.currentTimeMillis(), 5 * 1000, pintent);
}

@Override
public void onTerminate() {
clear();
super.onTerminate();
}

public void clear() {
alarm.cancel(pintent);
Log.e("#####", "Alarm cancelled");
}
}

这就是我在日志中看到的:

07-09 14:53:30.399: ERROR/#####(1743): Starting alarm
07-09 14:53:52.690: ERROR/#####(1590): It's alive!
07-09 14:54:02.690: ERROR/#####(1590): It's alive!
07-09 14:54:12.690: ERROR/#####(1590): It's alive!
07-09 14:54:20.475: ERROR/#####(1743): Alarm cancelled
07-09 14:54:22.695: ERROR/#####(1590): It's alive!
07-09 14:54:32.696: ERROR/#####(1590): It's alive!

我使用相同的 Intent 来取消任务,即使是同一个实例,但它不起作用。你能帮帮我吗?

最佳答案

好奇 pintent 是否仍在 clear() 中保存 SyncService 的详细信息?

关于android - 如何取消警报管理器中的重复任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17551535/

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