如何在Android中停止重复警报服务?
我有totalTime = 1小时和intervalTime = 5min,两者都以毫秒为单位,
我想每个间隔重复我的GPSSetting服务,并且在totaltime完成后,这种重复应该停止。
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent1 = new Intent(C2DMReceiver.this, GPSSetting.class);
PendingIntent pendingIntent = PendingIntent.getService(
C2DMReceiver.this, 0, intent1, 0);
am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), intervalTime,
pendingIntent);
那么我该如何停止这个重复的警报呢?
问候, 吉里什
I have totalTime = 1 hr and and intervalTime = 5min, both are in millisecond,
I want to repeat my GPSSetting services every interval and after completion of totaltime this repeating shoud be stop.
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent1 = new Intent(C2DMReceiver.this, GPSSetting.class);
PendingIntent pendingIntent = PendingIntent.getService(
C2DMReceiver.this, 0, intent1, 0);
am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), intervalTime,
pendingIntent);
So how to i can stop this repeating alarm ?
Regards,
Girish
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建与注册警报时相同的
PendingIntent
并使用AlaramManager.cancel()
取消它。Create the same
PendingIntent
as when you registered the alarm and useAlaramManager.cancel()
to cancel it.如果您设置了重复时间间隔的闹钟服务并且您想取消服务,那么这个解决方案将帮助您,这里首先我每 30 秒后创建重复服务,但是当我按下停止按钮时它将取消我生成的所有服务。
现在我将使用相同的“pendingintent”和“alarmManager”停止/取消此服务,要取消此服务,代码将是
If you have set alarm service for repeating interval of time and you want to cancel your services, then this solution will help you, here first of all i am creating repeating services after every 30 sec, but when i'll press the stop button then it will cancel all the services which i have generated.
Now i'll stop/cancel this services using the same "pendingintent" and "alarmManager", to cancel this the code will be
您可以将意图额外值传递为当前系统时间 1 小时(或任意时间)。然后您可以从服务的 onBind() 事件内部检查该值并进行比较。以下是一个示例:
然后在 onBind() 方法内部:
不确定它是否适用于服务,但它适用于 BroadcastReceiver。
You can pass an intent extra value as 1 hours (or any) from current system time. Then you can check that value from inside the onBind() event of the service and compare it. Following is an example:
Then inside the onBind() methode:
Not sure if it will work for services but it worked for BroadcastReceiver.
您可以通过传递您在警报管理器的取消方法中设置时使用的相同挂起意图来取消警报:
You can cancel the alarm by passing the the same pending intent you used while setting up in the cancel method of alarm manager: