【问题标题】:cancel repeating alarm at specific time在特定时间取消重复警报
【发布时间】:2014-07-12 19:26:45
【问题描述】:

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

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

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;
}

【问题讨论】:

    标签: android alarmmanager repeatingalarm


    【解决方案1】:

    您的代码毫无意义。您必须牢记三个基本事项:

    1. 所有警报都链接到特定应用程序。
    2. 警报功能基于待处理的意图。
    3. 可以通过匹配特定的Intent 来取消。

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

    照常创建基本警报:

    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 放入它:

    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);
        }
    }
    

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

    【讨论】:

    • 史诗你不知道这对你有多大帮助,非常感谢
    • 很好的答案。帮助很大。 :)