我试图让我的警报管理器在时间选择器选择的时间每天重复一次,我还是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);

    }

}

最佳答案

尝试这个:

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


setRepeating中缺少的是您需要设置4个参数。缺少的一个是时间间隔参数(第三个)。 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

07-24 09:26