使用时间选择器使警报管理器每天重复

时间:2014-08-13 17:22:20

标签: java android eclipse alarmmanager timepicker

我试图让我的闹钟管理器每天在时间选择器选择的时间重复,我仍然是java编码和东西的新手,我不知道在setRepeating()和一个报警管理器中写什么例如我找到了这段代码

alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

我尝试更改它但我收到错误 描述资源路径位置类型 AlarmManager类型中的方法setRepeating(int,long,long,PendingIntent)不适用于参数(int,long,PendingIntent)

帮助?

public class AndroidTimeActivity extends Activity {

    TimePicker myTimePicker;
    Button buttonstartSetDialog;
    Button buttonCancelAlarm;
    TextView textAlarmPrompt;

    TimePicker timePicker;
    CheckBox optRepeat;

    final static int RQS_1 = 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        timePicker = (TimePicker)findViewById(R.id.picker);
        optRepeat = (CheckBox)findViewById(R.id.optrepeat);
        textAlarmPrompt = (TextView)findViewById(R.id.alarmprompt);

        buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
        buttonstartSetDialog.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                Calendar calNow = Calendar.getInstance();
                Calendar calSet = (Calendar) calNow.clone();

                calSet.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
                calSet.set(Calendar.MINUTE, timePicker.getCurrentMinute());
                calSet.set(Calendar.SECOND, 0);
                calSet.set(Calendar.MILLISECOND, 0);

                if(calSet.compareTo(calNow) <= 0){
                    //Today Set time passed, count to tomorrow
                    calSet.add(Calendar.DATE, 1);
                }

                setAlarm(calSet, optRepeat.isChecked());

            }});

        buttonCancelAlarm = (Button)findViewById(R.id.cancel);
        buttonCancelAlarm.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                cancelAlarm();
            }});

    }

    private void setAlarm(Calendar targetCal, boolean repeat){

        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

        if(repeat){
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
                    targetCal.getTimeInMillis(), 
                    AlarmManager.INTERVAL_DAY,
                    pendingIntent);

            textAlarmPrompt.setText(
                    "\n\n***\n"
                    + "Alarm is set@ " + targetCal.getTime() + "\n"
                    + "Repeat\n"
                    + "***\n");
        }else{
            alarmManager.set(AlarmManager.RTC_WAKEUP, 
                    targetCal.getTimeInMillis(),  
                    pendingIntent);

            textAlarmPrompt.setText(
                    "\n\n***\n"
                    + "Alarm is set@ " + targetCal.getTime() + "\n"
                    + "One shot\n"
                    + "***\n");
        }

    }

    private void cancelAlarm(){

        textAlarmPrompt.setText(
                "\n\n***\n"
                + "Alarm Cancelled! \n"
                + "***\n");

        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);

    }

}

1 个答案:

答案 0 :(得分:1)

试试这个:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

setRepeating中缺少的是您需要设置的4个参数。缺少的是间隔参数(第3个)。 AlarmManager具有为每天,半天等间隔声明的常量。

如果您想重复此操作,请确保不要使用alarmManager.set()' and use alarmManager.setRepeating`。

此外,如果您想让用户选择是否重复,您可以进行简单的布尔检查。如果重复,请使用.setRepeating,否则请使用.set

编辑:我自己没有使用.setRepeating(),我不确定它是否按照.INTERVAL_DAY常量的方式运行。您可能必须使用.setInexactRepeating()。参数看起来完全一样:

alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

AlarmManager | Android