Android 如何在特定时间启动服务
我正在创建一个从webservice获取数据的应用程序。我一直在Android 如何在特定时间启动服务,android,multithreading,service,timing,Android,Multithreading,Service,Timing,我正在创建一个从webservice获取数据的应用程序。我一直在while(true),并在特定的毫秒内休眠循环。。我希望服务操作(从webservice获取数据)总是在特定的时间启动。。相反,总是打开并通过线程暂停。睡眠(毫秒)。。。谢谢 这就是我们一直在使用的 while(true) { ///pullDataFromWebservice(); Thread.sleep(600000); } 使用It比使用睡眠和投票服务更安全 为什么? 因为当资源变少时,Android很有可能回收这么长时
while(true)
,并在特定的毫秒内休眠循环。。我希望服务操作(从webservice获取数据)总是在特定的时间启动。。相反,总是打开并通过线程暂停。睡眠(毫秒)
。。。谢谢
这就是我们一直在使用的
while(true)
{
///pullDataFromWebservice();
Thread.sleep(600000);
}
使用It比使用睡眠和投票服务更安全
为什么?
因为当资源变少时,Android很有可能回收这么长时间运行的服务,从而导致
在你的延迟逻辑中,永远不会被调用
从现在起1小时内激活回调的示例:
private PendingIntent pIntent;
AlarmManager manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyDelayedReceiver.class); <------- the receiver to be activated
pIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
60*60*1000, pIntent); <---------- activation interval
私人吊挂帐篷;
AlarmManager=(AlarmManager)context.getSystemService(context.ALARM\u服务);
意向意向=新意向(上下文,MyDelayedReceiver.class) 尝试使用AlarmManager-虽然有点耗电
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, YourClass.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar cal= Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 19);
cal.set(Calendar.MINUTE, 20);
cal.set(Calendar.SECOND, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pIntent);
//RTC_WAKEUP to enable this alarm even in switched off mode.
这个链接可能会帮助你:非常有用。真的节省了我的时间。“PendingEvent.getBroadcast(context,0,intent,0)”是否会发送广播?PendingIntent包装了报警管理器将用于在适当时间唤醒接收器的意图。我的意思是,其他上下文必须接收报警管理器发送的广播??我也可以使用“PrimePosith.GETService(这个,0,意图,0)”来启动服务,而不是发送一个广播??我已经成功地安排了,但是考虑一个情况,如果应用程序被任务管理器清除了,日程是否仍然有效?