gpt4 book ai didi

Android 位置更新作为后台服务

转载 作者:行者123 更新时间:2023-12-02 15:23:25 24 4
gpt4 key购买 nike

我是 Android 新手,正在开发一个应用程序,以实现即使应用程序退出也能获取位置更新的功能。即,无论我的应用程序的状态如何,它都应该不断更新我在服务器中的当前位置终止。

你们能建议我应该采取什么方法吗?非常感谢您的帮助...

最佳答案

下面是使用处理程序每​​ X 分钟或每 X 米获取用户位置的代码。

Location gpslocation = null;

private static final int GPS_TIME_INTERVAL = 60000; // get gps location every 1 min
private static final int GPS_DISTANCE= 1000; // set the distance value in meter

/*
for frequently getting current position then above object value set to 0 for both you will get continues location but it drown the battery
*/

private void obtainLocation(){
if(locMan==null)
locMan = (LocationManager) getSystemService(LOCATION_SERVICE);

if(locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){
gpslocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(isLocationListener){
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,
GPS_TIME_INTERVAL, GPS_DISTANCE, GPSListener);
}
}
}
}

处理程序

    private static final int HANDLER_DELAY = 1000*60*5;

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
myLocation = obtainLocation();
handler.postDelayed(this, HANDLER_DELAY);
}
}, START_HANDLER_DELAY);

GPS位置监听器

private LocationListener GPSListener = new LocationListener(){
public void onLocationChanged(Location location) {
// update your location

}

public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

public void onStatusChanged(String provider, int status, Bundle extras) {
}
};

关于Android 位置更新作为后台服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36545298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com