在特定时间取消重复警报 [英] cancel repeating alarm at specific time

查看:100
本文介绍了在特定时间取消重复警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在特定时间取消2条重复的警报,但是应用当前决定在创建警报后立即调用取消。例如,如果您将结束重复的时间设置为13:18,则重复警报应该一直重复到该时间为止。

I'm looking to cancel 2 repeating alarms at a specific time but the app currently decides to call the cancel as soon as you create the alarms. For example if you set the the time to end repeation at 13:18 then the repeating alarm should repeat until it is that time.

这是我拥有的一些代码一直在玩弄

Here is some of the code I have been toying with:

Toast.makeText(this, "Alarm Scheduled for " + midnight, Toast.LENGTH_LONG).show();


    AlarmManager Databackon = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

      Databackon.set(AlarmManager.RTC_WAKEUP,midnight, stoprepeat(V));
    // Databackon.set(AlarmManager.RTC_WAKEUP,midnight, PendingIntent.getBroadcast(this,4,  intent, PendingIntent.FLAG_UPDATE_CURRENT));



     Toast.makeText(this, "THIS " + midnight, Toast.LENGTH_LONG).show();


    /*  
      Button button1=(Button)findViewById(R.id.startupdates);
      button1.setVisibility(View.INVISIBLE);
      Button button2=(Button)findViewById(R.id.stopbutton);
      button2.setVisibility(View.VISIBLE);
    */
}

public PendingIntent stoprepeat(View V)
{
    Intent intent = new Intent(UpdatesActivity.this,AlarmReciever.class);
    PendingIntent pendingIntent =
            PendingIntent.getBroadcast(this,1,  intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.cancel(pendingIntent);

    Intent intent2 = new Intent(UpdatesActivity.this,DataOff.class);
    PendingIntent pendingIntent2 =
            PendingIntent.getBroadcast(this,2, intent2,PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am2 = (AlarmManager) getSystemService(ALARM_SERVICE);
    am2.cancel(pendingIntent2);

        Toast.makeText(this, "Repeating Alarm stopped", Toast.LENGTH_LONG).show();

          Button button1=(Button)findViewById(R.id.startupdates);
          button1.setVisibility(View.VISIBLE);
          Button button2=(Button)findViewById(R.id.stopbutton);
          button2.setVisibility(View.INVISIBLE);

        return pendingIntent;
}


推荐答案

您的代码没有任何意义。您必须牢记三个基本事项:

Your code makes no sense. You have to keep in mind three basic things:


  1. 所有警报都链接到特定的应用程序。

  2. 警报功能基于挂起的意图。

  3. 可以通过匹配特定的意图来取消。

  1. All alarms are linked to the specific application.
  2. Alarms functionality is based on the pending intents.
  3. Cancellation can be done my matching to specific Intent.

考虑到这一点,您可以实施以下解决方案。

Considering that, you can implement the following solution.

像往常一样创建基本警报:

Create basic alarm as usual:

Intent myIntent = new Intent(this, Target.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);

创建另一个警报,该警报将负责取消并放置 pendingIntent

Create another alarm, which will be responsible for cancellation and put pendingIntent from the 1st alarm to it:

Intent cancellationIntent = new Intent(this, CancelAlarmBroadcastReceiver.class);
cancellationIntent.putExtra("key", pendingIntent);
PendingIntent cancellationPendingIntent = PendingIntent.getBroadcast(this, 0, cancellationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, time, cancellationPendingIntent, PendingIntent.FLAG_UPDATE_CURRENT);

其中 CancelAlarmBroadcastReceiver 如下:

public class CancelAlarmBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        PendingIntent pendingIntent = intent.getParcelableExtra("key");
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.cancel(pendingIntent);
    }
}

我没有检查,但我认为应该可以。

I didn't check it, but I think it should work.

这篇关于在特定时间取消重复警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆