创建一个从webservice获取数据的应用程序。我已经while(true)
并在特定的毫秒内休眠循环..我想让服务操作(从webservice获取数据)始终在特定时间开始...而不是始终在on并暂停{ {1}} ...谢谢
这就是使用
Thread.sleep(milli)
答案 0 :(得分:2)
使用Alarm API.这比睡眠更安全民意调查服务。
为什么?
因为当资源变低时,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
答案 1 :(得分:0)
尝试使用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.