Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 日历,每24小时执行一次代码_Android_Calendar_Scheduler - Fatal编程技术网

Android 日历,每24小时执行一次代码

Android 日历,每24小时执行一次代码,android,calendar,scheduler,Android,Calendar,Scheduler,我想每24小时执行一段代码,但我不知道如何执行。 我有一些代码设置我希望循环开始的时间,但不确定如何执行结束时间 int startDay = 00; // 12am int end = 24; // 12 pm int hours = (end - startDay) % 24; //difference will be 24 hours Calendar calInstanceOne = Calendar.getInstance(); // set

我想每24小时执行一段代码,但我不知道如何执行。 我有一些代码设置我希望循环开始的时间,但不确定如何执行结束时间

    int startDay = 00; // 12am
    int end = 24; // 12 pm
    int hours = (end - startDay) % 24; //difference will be 24 hours

    Calendar calInstanceOne = Calendar.getInstance();

    // set calendar to 12 am
    calInstanceOne.set(Calendar.HOUR_OF_DAY, startDay);
    calInstanceOne.set(Calendar.MINUTE, 0);
    calInstanceOne.set(Calendar.SECOND, 0);
    calInstanceOne.set(Calendar.MILLISECOND, 0);

我是否创建另一个日历实例,设置为12pm?比较两者?非常感谢您能深入了解这一点。

您可以使用AlarmManager定期执行操作:

Intent intent = new Intent(this, MyStartServiceReceiver.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, <24h in msecs>, pendingIntent);
Intent Intent=newintent(这是MyStartServiceReceiver.class).addFlags(Intent.FLAG\u活动\u新任务);
PendingEvent=PendingEvent.getBroadcast(this,0,intent,PendingEvent.FLAG_UPDATE_CURRENT);
alarmManager=(alarmManager)getSystemService(Context.ALARM\u服务);
alarmManager.setInexactRepeating(alarmManager.RTC_唤醒,System.currentTimeMillis()+5000,PendingEvent);

然后,您应该在清单中注册您的
BroadcastReceiver
,并调用要从此接收器执行的方法。

您可以使用AlarmManager定期执行操作:

Intent intent = new Intent(this, MyStartServiceReceiver.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, <24h in msecs>, pendingIntent);
Intent Intent=newintent(这是MyStartServiceReceiver.class).addFlags(Intent.FLAG\u活动\u新任务);
PendingEvent=PendingEvent.getBroadcast(this,0,intent,PendingEvent.FLAG_UPDATE_CURRENT);
alarmManager=(alarmManager)getSystemService(Context.ALARM\u服务);
alarmManager.setInexactRepeating(alarmManager.RTC_唤醒,System.currentTimeMillis()+5000,PendingEvent);
然后,您应该在清单中注册您的
BroadcastReceiver
,并从该接收器调用要执行的方法

我想每24小时执行一段代码

,连同或。理想情况下,在
AlarmManager
上运行
INTERVAL\u DAY
,以允许Android滑动实际时间,从而最好地为用户节省电池

我想每24小时执行一段代码

,连同或。理想情况下,在
AlarmManager
上运行
INTERVAL\u DAY
,以允许Android滑动实际时间,从而最好地为用户节省电池

首先存储当前时间,然后每当应用程序打开时,将当前时间与以前的存储时间(如果大于或等于24小时)进行比较 执行你的代码

首先存储当前时间,然后每当应用程序打开时,将当前时间与以前的存储时间(如果大于或等于24小时)进行比较 执行你的代码


你可能有几个选择,让我来概括一下最简单的一个。策略是简单地使用系统时间在24小时后执行:

package com.test;
import java.util.Calendar;

public class ExecuteCheck {
//Class fields
/* Number of milliseconds in a day
 * 
 */
private static final long C_DAY=24*60*60*1000;
//Object fields
/* Time last executed (or beginning of cycle), in milliseconds;
 * 
 */
private long lastExecuted = System.currentTimeMillis();

public ExecuteCheck() {
}

/** Set the current execution cycle time to now
 * 
 */
public void setExecutionTimeToNow() {
    lastExecuted = System.currentTimeMillis();
}
/** Set the execution cycle time to be the value in the calendar argument.
 * @param cal
 */
public void setExecutionTime(Calendar cal) {
    lastExecuted = cal.getTimeInMillis();   
}

/** Is it more than twenty-four hours since the last execution time?
 * @return
 */
public boolean isTimeToExecute() {
    return (System.currentTimeMillis() - lastExecuted) > C_DAY;
}

}

您可能有几种选择,让我概括一下最简单的一种。策略是简单地使用系统时间在24小时后执行:

package com.test;
import java.util.Calendar;

public class ExecuteCheck {
//Class fields
/* Number of milliseconds in a day
 * 
 */
private static final long C_DAY=24*60*60*1000;
//Object fields
/* Time last executed (or beginning of cycle), in milliseconds;
 * 
 */
private long lastExecuted = System.currentTimeMillis();

public ExecuteCheck() {
}

/** Set the current execution cycle time to now
 * 
 */
public void setExecutionTimeToNow() {
    lastExecuted = System.currentTimeMillis();
}
/** Set the execution cycle time to be the value in the calendar argument.
 * @param cal
 */
public void setExecutionTime(Calendar cal) {
    lastExecuted = cal.getTimeInMillis();   
}

/** Is it more than twenty-four hours since the last execution time?
 * @return
 */
public boolean isTimeToExecute() {
    return (System.currentTimeMillis() - lastExecuted) > C_DAY;
}

}

谢谢,这也是一个帮助我的答案。非常感谢谢谢你,这也是一个帮助我的答案。非常感谢