安卓:发送用户位置服务器每15分钟只有位置已经改变? [英] Android: Send user location to the server every 15 minutes only if the location has been changed?
本文介绍了安卓:发送用户位置服务器每15分钟只有位置已经改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序为用户提供位置相关的通知,这意味着我需要知道我的用户作为实时越好,不要把太多的压力对battery.I地点已经研究了一下,创建选项下面的列表
My app provides users location relevant notifications which means I need to be aware of my users' locations as real time as possible and not putting too much stress on battery.I have researched a bit and create the following list of option
-
部分激活锁定
:它可以让屏幕超时而CPU保持执行的任务。但我只是想我的背景code至每n秒调用,检查是否有位置更新,如果该位置被改变然后将其发送给服务器。 -
AlarmManager
:我可以用它来设计的重复任务,但不知道这是否会继续在后台运行无限期可以在用户故意杀害
partial Wakelock
: which lets the screen to timeout but CPU keeps executing task. But I just want my background code to be invoked every n seconds,check for location update,if the location is changed then send it to server.AlarmManager
: I can use this to design recurring tasks but not sure if this will keep running in background indefinitely and can it be killed by users deliberately.
我想我的背景code每n秒,只要安装该应用程序用户的手机上调用。我要寻找答案,理论不是实际的code,因为我需要了解我在做什么。
I want my background code to be invoked every n seconds as long as the app is installed on user's phone. I am looking for theoretical answers not the actual code as I need to understand what I am doing.
推荐答案
哟可以使用服务类有:
public class StartService extends Service {
Timer timer = new Timer();
private final int TIME_INTERVAL = 10000;
GPSTracker GPSTracker;
Double latitude ;
Double longitude ;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
GPSTracker = new GPSTracker(StartService.this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
doTimerThings();
return super.onStartCommand(intent, flags, startId);
}
private void doTimerThings()
{
timer.scheduleAtFixedRate(new TimerTask() {
@SuppressLint("DefaultLocale")
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void run() {
latitude = GPSTracker.getLatitude();
longitude = GPSTracker.getLongitude();
// you get the lat and lng , do your server stuff here-----
System.out.println("lat------ "+latitude);
System.out.println("lng-------- "+longitude);
}
}, 0, TIME_INTERVAL);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
这篇关于安卓:发送用户位置服务器每15分钟只有位置已经改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文