2016-12-29 22 views
1

毎日リマインダーを同時に起動する必要のあるAndroidアプリがあります。無視された場合、アラームは5分ごとに繰り返さなければなりません。ユーザーがOKボタンをクリックしてリマインダーを読んだと宣言した場合、アラームは翌日にトリガーされるまで繰り返されなくなります。 したがって、ユーザーの確認後にアラームが繰り返されるのを止め、AlarmManagerを使用してアラームを読み取るには、cancel()メソッドを使用する必要があります。しかし、私は将来のアラームを削除したくない、私はちょうどそれが次のトリガまで繰り返すのをやめておきたい。 つまり、私はcancel()メソッドで将来のアラームを解除したくありません。それはcancel()メソッドのデフォルトの動作ですか、それとも毎回アラームをキャンセルしてから再設定する必要がありますか?Android:アラームを解除しないでアラームを繰り返すのを止める方法

これは、アラームを設定するための私のコードです:

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

感謝を。

答えて

0

アラームスケジュールのキャンセル時に、翌日の新しいアラームがスケジュールされます。

+0

ので、キャンセル()メソッドの動作はOK ...完全に将来のスケジュールからアラームを削除することです! – Ciammarica

0

"キャンセル"オプションの目的は、アラームを削除することです。

アプリケーションは、元の設定と同じように新しいアラームを追加する必要があります。

完全なアラームを実装する良い例は、デバイスの再起動時に再追加する方法を含め、Androidの次のリンクから見つけることができます。

Repeat Alarm Example In Android Using AlarmManager

+0

ありがとう、それは今より明らかです。だから、多くの種類のリマインダーを持っていて、特定のものを取り消したいのであれば、pendingIntentを識別するリクエストコードでこれを行う必要があります。 'PendingIntent pendingIntent = PendingIntent.getBroadcast ( context.getApplicationContext()、 currentID、 alarmIntent、 0)。 ... alarmManager.cancel(pendingIntent); ' – Ciammarica

+0

@Ciammaricaはい。あなたがキャンセルされたら、あなたはどちらかが完了しているか、または再発アラームの場合、直前に取り消されたアラームの代わりを設定する必要があります。 –

+0

これを明確にしてくれてありがとう! – Ciammarica

関連する問題