2

我正在android中创建一个调度程序应用程序,它每天在用户指定的时间运行这里是代码,请说明为什么它不工作

  private void setAlarm(String targetCal){
    Toast.makeText(MainActivity.this,"Alarm Set", Toast.LENGTH_LONG).show();
    String[] Time=targetCal.split(":");
    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Calendar timeOff = Calendar.getInstance();
    //int days = Calendar.SUNDAY + (7 - timeOff.get(Calendar.DAY_OF_WEEK)); // how many days until Sunday
    timeOff.set(Calendar.HOUR,Integer.valueOf(Time[0].trim()));
    timeOff.set(Calendar.MINUTE,Integer.valueOf(Time[1].trim()));
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeOff.getTimeInMillis(), alarmManager.INTERVAL_DAY , pendingIntent);

}
4

1 回答 1

1

setRepeating行替换为以下内容:

尝试:-

 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeOff.getTimeInMillis(), 1000 * 60 * 60 * 60 * 24, pendingIntent);

反而:-

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

注意:我刚刚实现了这种方式,它对我来说效果很好。

于 2013-04-16T13:06:14.187 回答