2017-10-10 5 views
-1

Je crée une application d'alarme avec réglage de l'alarme de plusieurs jours. J'ai déjà réglé l'alarme pour un jour. Mais je ne sais pas comment régler la même alarme pour les jours sélectionnés. Pour sélectionner plusieurs jours, j'utilise checkboxes.Régler l'alarme de répétition chaque semaine pour les jours sélectionnés

Ici, je reçois l'utilisateur jours sélectionnés:

//set alarm repeat days method 
public void showDialogAlarmdays() { 

    setRepeatTxt.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        selections=""; 
        ad.show(); 
       } 
      } 
    ); 

    final String[] items=getResources().getStringArray(R.array.my_date_choose); 

    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this); 
    // Set the dialog title 
    builder.setTitle("Choose your days"); 
    // Specify the list array, the items to be selected by default (null for none), 
    // and the listener through which to receive callbacks when items are selected 
    builder.setMultiChoiceItems(R.array.my_date_choose, null, 
      new DialogInterface.OnMultiChoiceClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which, 
            boolean isChecked) { 
        if (isChecked) { 
         // If the user checked the item, add it to the selected items 
         mSelectedItems.add(items[which]); 
        } else if (mSelectedItems.contains(items[which])) { 
         // Else, if the item is already in the array, remove it 
         mSelectedItems.remove(items[which]); 
        } 
       } 
      }); 
    // Set the action buttons 
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int id) { 
      selections=""; 
      for (String ms:mSelectedItems) { 
       if(selections==""){ 
        selections=ms; 
       }else{ 
        selections=selections+","+ms; 
       } 

      } 
      // Toast.makeText(addTimeSlot.this,selections, Toast.LENGTH_LONG).show(); 
      if(selections.equals("")){ 
       showRepeatTxt.setText("Choose your days"); 
      }else{ 
       showRepeatTxt.setText(selections); 
      } 
       //Toast.makeText(setAlarm.this,selections, Toast.LENGTH_LONG).show(); 
     } 
    }); 
    builder .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int id) { 

     } 
    }); 

    ad = builder.create(); 
} 

À titre d'exemple, si l'utilisateur sélectionner lundi, mardi et vendredi, alors alarme doit répéter que les jours sélectionnés.

Ici méthode pour régler l'alarme: les valeurs

alarm_start.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //Toast.makeText(setAlarm.this,hour+" : "+min, Toast.LENGTH_SHORT).show(); 

      //celender set time 
      calendar.set(Calendar.HOUR_OF_DAY,Cal_hour); 
      calendar.set(Calendar.MINUTE,Cal_minute); 

      //put extra string into Alarm_intent 
      //tells the clock that you pressed the 'OK' button 
      Alarm_intent.putExtra("extra","on"); 

      //put extra int into Alarm_intent 
      //tells the clock that you want to certain value from spinner 
      Alarm_intent.putExtra("ringtoneChoice",choose_ringtone); 

      Log.e("Ringtone id : ", String.valueOf(choose_ringtone)); 

      //create a pending intent that delay the intent until the specified calendar time 
      pending_intent = PendingIntent.getBroadcast(setAlarm.this.getApplicationContext(),0, 
        Alarm_intent,pending_intent.FLAG_UPDATE_CURRENT); 

      //set the alarm manager 
      alarm_Manager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), 
        pending_intent); 


      Toast.makeText(setAlarm.this,"Alarm is set...!", Toast.LENGTH_SHORT).show(); 
      finish(); 
     } 
    }); 

Ici calendar.set (Calendar.HOUR_OF_DAY, Cal_hour) et calendar.set (Calendar.MINUTE, Cal_minute) obtenir à partir d'un sélecteur de temps.

Répondre

0
Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class); 
    pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0); 

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 

      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      calendar.add(Calendar.SECOND, 10); 
      //alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, pendingIntent);