且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android的AlarmManager和服务问题

更新时间:2023-02-26 21:26:54

不,你不能这样做,直接从监听器。您可以禁用报警是这样的:

No you can't do that directly from listener. You can disable the alarm this way:

Intent intent = new Intent(maincode.this, AlarmReceiver.class);
PendingIntent pendingIntent =  PendingIntent.getBroadcast(bInsulinReminder.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingItem.cancel();
alarmManager.cancel(pendingItem);

或者,如果(我想)AlarmReceiver是执行的BroadcastReceiver,并从的onReceive方法,你开始你的MyService这是实现服务类。

Or if (I suppose) AlarmReceiver is implementation of BroadcastReceiver and from onReceive method you start your MyService which is implementation of Service class.

所以,如果你想从你的主要code.java监听器内停止此报警你可以通过重新PendingIntent你AlarmReceiver使用和执行stopService方法停止为MyService。

So, if you want to stop this alarm from inside of your maincode.java listener you can just stop MyService by recreating PendingIntent you used in AlarmReceiver and executing stopService method.

希望有所帮助。