Android:如何在不取消设置闹钟的情况下停止重复闹钟

时间:2016-12-29 11:11:27

标签: android alarmmanager

我有一个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);

感谢。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

&#34;取消&#34;的意图选项是删除警报。

您的应用程序应该像原始设置一样添加新警报。

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

Repeat Alarm Example In Android Using AlarmManager