我有一个服务,它在每天特定的时间间隔后使用设置的重复闹钟来触发。现在,我希望仅在办公时间内触发该服务。即上午8时至下午6时对于这个时间间隔,我希望服务在用户设置的特定时间间隔触发。下午6点以后,它应该停止,并在第二天上午8点重新启动我尝试了internet.Please上的几乎所有可用的东西,给我一些示例代码。
发布于 2015-07-24 02:38:54
您可以每2小时触发一次告警事件。在接收器中,检查时间。如果时间在用户输入的时间范围(UserEnteredTimeRange)内,请不要运行service.If,不要执行任何操作。
我不知道你需要什么样的服务。如果这只是一个快速的后台工作,你可以启动一个IntentService。
Intent alarmIntent = new Intent(ctx, MyAlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(ctx, 0, alarmIntent, 0);
manager = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), SCHEDULEDING_TIME_IN_MS, pendingIntent);在MyAlarmReceiver中
@Override
public void onReceive(Context ctx, Intent arg1) {
Log.e(TAG,"Alarm received");
acquire(ctx);
try {
if(time in userEnteredTimeRange){
ctx.startService(new Intent(ctx, YourService.class));
}
}catch(Exception e){
Log.e(TAG,"error in starting service");
}
}https://stackoverflow.com/questions/31595290
复制相似问题