如何在Android中停止重复警报服务?

发布于 2024-12-16 10:34:38 字数 591 浏览 2 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聊慰 2024-12-23 10:34:38

创建与注册警报时相同的 PendingIntent 并使用 AlaramManager.cancel() 取消它。

Create the same PendingIntent as when you registered the alarm and use AlaramManager.cancel() to cancel it.

梦在深巷 2024-12-23 10:34:38

如果您设置了重复时间间隔的闹钟服务并且您想取消服务,那么这个解决方案将帮助您,这里首先我每 30 秒后创建重复服务,但是当我按下停止按钮时它将取消我生成的所有服务。

  public void AlarmService_afte30sec()
   {

       Toast.makeText(this, "Starting  Alarm Services", Toast.LENGTH_LONG).show();
       Intent myIntent = new Intent(this, StartAlarmServices.class);
       pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

               alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                int sec=30;
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sec*1000, pendingIntent);
     }

现在我将使用相同的“pendingintent”和“alarmManager”停止/取消此服务,要取消此服务,代码将是

  public void stopservice()
 {
        alarmManager.cancel(pendingIntent);      
   }

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.

  public void AlarmService_afte30sec()
   {

       Toast.makeText(this, "Starting  Alarm Services", Toast.LENGTH_LONG).show();
       Intent myIntent = new Intent(this, StartAlarmServices.class);
       pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

               alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                int sec=30;
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sec*1000, pendingIntent);
     }

Now i'll stop/cancel this services using the same "pendingintent" and "alarmManager", to cancel this the code will be

  public void stopservice()
 {
        alarmManager.cancel(pendingIntent);      
   }
韶华倾负 2024-12-23 10:34:38

您可以将意图额外值传递为当前系统时间 1 小时(或任意时间)。然后您可以从服务的 onBind() 事件内部检查该值并进行比较。以下是一个示例:

 //your code:
 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            Intent intent1 = new Intent(C2DMReceiver.this, GPSSetting.class);

 //my code:
 intent1.putExtra("end", System.currentTimeMillis() + 3600000); //current time plus 1 hour

 //your code:
            PendingIntent pendingIntent = PendingIntent.getService(
                    C2DMReceiver.this, 0, intent1, 0); //You might wanna give PendingIntent.FLAG_UPDATE_CURRENT as flag.
            am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), intervalTime,
                    pendingIntent);

然后在 onBind() 方法内部:

public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
 long end = intent.getLongExtra("end", 0);
 if(end <= System.currentTimeMillis()){
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
      PendingIntent pendingIntent = PendingIntent.getService(
                    this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      am.cancel(pendingIntent);
 }
    return null;
}

不确定它是否适用于服务,但它适用于 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:

 //your code:
 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            Intent intent1 = new Intent(C2DMReceiver.this, GPSSetting.class);

 //my code:
 intent1.putExtra("end", System.currentTimeMillis() + 3600000); //current time plus 1 hour

 //your code:
            PendingIntent pendingIntent = PendingIntent.getService(
                    C2DMReceiver.this, 0, intent1, 0); //You might wanna give PendingIntent.FLAG_UPDATE_CURRENT as flag.
            am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), intervalTime,
                    pendingIntent);

Then inside the onBind() methode:

public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
 long end = intent.getLongExtra("end", 0);
 if(end <= System.currentTimeMillis()){
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
      PendingIntent pendingIntent = PendingIntent.getService(
                    this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      am.cancel(pendingIntent);
 }
    return null;
}

Not sure if it will work for services but it worked for BroadcastReceiver.

烟─花易冷 2024-12-23 10:34:38

您可以通过传递您在警报管理器的取消方法中设置时使用的相同挂起意图来取消警报:

am.cancel(pendingIntent);

You can cancel the alarm by passing the the same pending intent you used while setting up in the cancel method of alarm manager:

am.cancel(pendingIntent);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文