2017-10-10 79 views
-1

我正在創建具有多天設置警報的警報應用程序。我已經設置了鬧鐘一天。但不知道如何在選定日期設置相同的鬧鐘。要選擇多天,我使用checkboxes每週爲選定日期設置重複警報

在這裏,我得到了用戶選擇的天數:

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

舉個例子,如果用戶選擇週一,週二和週五,隨即報警應該重複選定的日子。

以下方法設置報警:

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

在這裏calendar.set(Calendar.HOUR_OF_DAY,Cal_hour)和calendar.set(Calendar.MINUTE,Cal_minute)值從時間選擇器獲得。

回答

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