我的要求是
Android 应用程序必须每隔一小时(可配置)向服务器发送用户位置详细信息(纬度和经度)。
我遵循的方法是使用警报管理器,我以配置的时间间隔调用我的服务,无论应用程序是否正在运行,该服务都会将位置详细信息发送到服务器。
这是一个好方法吗?
我更喜欢ScheduledExecutorService,因为它对于后台任务来说更容易。
报警管理器:
The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. This guarantees that the phone will not sleep until you have finished handling the broadcast. Once onReceive() returns, the Alarm Manager releases this wake lock.
调度线程池执行器:
You can use java.util.Timer or ScheduledThreadPoolExecutor (preferred) to schedule an action to occur at regular intervals on a background thread.
您可以在这里看到完整的答案=>Android 中的 ScheduledExecutorService 和 AlarmManager 哪个更好? https://stackoverflow.com/a/23904741/3884250 And Here https://stackoverflow.com/a/14377875/3884250
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// Hit WebService
}
}, 0, 1, TimeUnit.HOURS);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)