【发布时间】:2017-11-10 10:10:49
【问题描述】:
我知道如何在特定时间触发 android 中的后台服务。我已经使用AlarmManager 来实现这一点,但我想在那个时候每天启动该服务。
假设我想在下午 12 点启动它,但在那之后服务应该继续运行。我也用过stopitself()的方法,但是不行。
这是我的代码:
void alram(Context ctx){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.AM_PM,Calendar.AM);
Intent serviceIntent = new Intent(ctx, ServiceClass.class);
PendingIntent servicePendingIntent =
PendingIntent.getService(ctx,Service_Apps.SERVICE_ID,serviceIntent,0);
AlarmManager alarm = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),servicePendingIntent);
alarm.setRepeating(
AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
servicePendingIntent
);
}
【问题讨论】:
-
@Channa 不重复,我们可以在特定时间开始服务,但我正在寻找每天的服务。
-
您可以做的是在第二天执行“旧”时计划新的 .setExact(..)。所以你的意图会做工作+计划。
标签: android background-service