如何在android中进行调度?

时间:2013-04-16 10:22:27

标签: android

我正在尝试在android中创建一个调度程序应用程序,我希望在指定的时间每天在后台运行app.Below是代码,但这对我不起作用请建议

  private void setAlarm(String targetCal){
    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]));
    timeOff.set(Calendar.MINUTE,Integer.valueOf(Time[1]));
    timeOff.set(Calendar.SECOND, 00);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeOff.getTimeInMillis(), 24*60*60*1000 , pendingIntent);

}

1 个答案:

答案 0 :(得分:0)

根据您的描述,我无法确定其不起作用的原因,但我的猜测是您未在WakeLock的{​​{1}}方法中获取AlarmReceiver

我前段时间发布了a similar question,而我正试图实现类似的东西。查看接受的答案和CommonsWare的答案。我希望他们在解决你的问题方面有用。

问候!