在指定日期内每周设置重复警报

时间:2017-10-10 16:44:03

标签: java android android-pendingintent

我正在设置多天警报设置警报应用程序。我已经将警报设置了一天。但是不知道如何在选定日期设置相同的警报。要选择多天,我使用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)值来自时间选择器。

1 个答案:

答案 0 :(得分: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);