【问题标题】:Android: How to stop repeating alarm without unsetting the alarm itselfAndroid:如何在不取消闹钟本身的情况下停止重复闹钟
【发布时间】:2016-12-29 11:11:26
【问题描述】:

我有一个 Android 应用,我需要每天同时触发提醒。如果忽略,警报必须每 5 分钟重复一次。如果用户通过单击确定按钮声明他已阅读提醒,则警报必须停止重复,直到第二天触发。 所以,我希望警报在用户确认后停止重复,我用 AlarmManager 读到我应该使用 cancel() 方法。但我不想删除未来几天的警报,我只想让它停止重复,直到下一次触发。 换句话说,我不希望 cancel() 方法取消设置未来几天的警报。这是 cancel() 方法的默认行为还是我必须取消警报然后每次都重新设置?

这是我设置闹钟的代码:

public class AlarmSettingManager
    {
     private static Context context;

     // Constructor
     public AlarmSettingManager(Context c)
        {
         context = c;
        }


     private static class PrescriptionAlarmSetter extends AsyncTask<String, Void, Boolean>
        {
         SharedPrefManager sharedPrefManager = SharedPrefManager.getInstance(context);

         @Override
         protected Boolean doInBackground(String... strings)
            {
             // Get the list of prescriptions from SharedPreferences
             if(!sharedPrefManager.getPrescrizioneList().equals(""))
                {
                 try
                    {
                     JSONArray responseJsonArray = new JSONArray(sharedPrefManager.getPrescrizioneList());

                     int currentID = Constants.PRESCRIZIONE_ALARM_ID;

                     for(int j=0; j<responseJsonArray.length(); j++)
                        {
                         JSONObject singlePrescription = responseJsonArray.getJSONObject(j);

                         Prescrizione prescrizione = new Prescrizione
                            (
                             singlePrescription.getInt("id_prescrizione"),
                             singlePrescription.getInt("id_medico"),
                             singlePrescription.getInt("id_farmaco"),
                             singlePrescription.getInt("numero_pasticche"),
                             singlePrescription.getInt("totale_compresse"),
                             singlePrescription.getString("nome"),
                             singlePrescription.getString("ora_assunzione"),
                             singlePrescription.getString("posologia"),
                             singlePrescription.getString("suggerimenti")
                            );

                         // Start setting the alarm for current prescription
                         Intent alarmIntent = new Intent(context, AlarmBroadcastReceiver.class);

                         PendingIntent pendingIntent = PendingIntent.getBroadcast
                            (
                             context.getApplicationContext(),
                             currentID,
                             alarmIntent,
                             PendingIntent.FLAG_CANCEL_CURRENT
                            );

                         // put the RequestCode ID as extra in order to identify which alarm is triggered
                         alarmIntent.putExtra("id", currentID);

                         Calendar calendar = Calendar.getInstance();
                         calendar.setTimeInMillis(System.currentTimeMillis());

                         // Specify the time to trigger the alarm
                         calendar.set(Calendar.HOUR_OF_DAY, prescrizione.getIntHour());
                         calendar.set(Calendar.MINUTE, prescrizione.getIntMinutes());
                         calendar.set(Calendar.SECOND, 0);

                         AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

                         // Set the time interval (in milliseconds) to repeat the alarm if the previous one was ignored
                         alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 300000L, pendingIntent);

                         currentID++;
                        }
                    }
                 catch(JSONException e)
                    {
                     e.printStackTrace();
                    }
                }

             return false;
            }


         @Override
         protected void onPostExecute(Boolean b)
            {
             super.onPostExecute(b);
            }
        }


     public boolean setAlarms()
        {
         AlarmSettingManager.PrescriptionAlarmSetter prescriptionAlarmSetter = new AlarmSettingManager.PrescriptionAlarmSetter();
         prescriptionAlarmSetter.execute();

         return true;
        }
    }

这是我要修改的一段代码,以取消警报重复:

Intent alarmIntent = new Intent(context, AlarmBroadcastReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast
                            (
                             context.getApplicationContext(),
                             currentID,
                             alarmIntent,
                             0
                            );

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

alarmManager.cancel(pendingIntent);

谢谢。

【问题讨论】:

    标签: android alarmmanager


    【解决方案1】:

    在取消闹钟时安排第二天的新闹钟。

    【讨论】:

    • 所以 cancel() 方法的行为是从未来的计划中完全删除警报......好的!
    【解决方案2】:

    “取消”选项的目的是删除警报。

    您的应用程序应该添加一个新警报,就像原来的设置一样。

    您可以在以下链接中找到在 Android 中实现完整警报的一个很好的示例,包括如何在设备重启时重新添加它。

    Repeat Alarm Example In Android Using AlarmManager

    【讨论】:

    • 谢谢,现在更清楚了。所以,有很多类型的提醒,如果我想取消一个特定的,我必须做这样的事情,请求代码识别pendingIntent,对吧? PendingIntent pendingIntent = PendingIntent.getBroadcast ( context.getApplicationContext(), currentID, alarmIntent, 0 ); ... alarmManager.cancel(pendingIntent);
    • @Ciammarica 是的。取消后,您要么完成,要么在再次发生警报的情况下需要为刚刚取消的警报设置替换。
    猜你喜欢
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    相关资源
    最近更新 更多