gpt4 book ai didi

android - 在我的应用程序中获取更新的位置

转载 作者:行者123 更新时间:2023-11-30 04:38:38 24 4
gpt4 key购买 nike

我需要在整个应用程序中更新位置。我试图通过在我的所有 Activity 中添加下面显示的代码来做到这一点。我的问题是我无法确定用户是否会在一项 Activity 中停留足够长的时间来获取更新。我怎样才能让一些应用程序明智的监听器在每个给定时间(比如 30 分钟)获得更新的位置?

private LocationManager mLocationManager;   

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

// My activity stuff
}

private Location getBestLocation(LocationManager locationManager) {

Location location_gps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location_network = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

// If both are available, get the most recent
if(location_gps!=null && location_network !=null) {
return (location_gps.getTime() > location_network.getTime())?location_gps:location_network;
}
else if(location_gps==null && location_network ==null){
return null;
}
else
return (location_gps==null)?location_network:location_gps;

}

@Override
protected void onResume() {
super.onResume();
// Request location updates at startup
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DIST, this);
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DIST, this);
getBestLocation(mLocationManager)
}

@Override
protected void onPause() {
super.onPause();
// Remove the locationlistener updates when Activity is paused
mLocationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
GlobalVars.lat = (Double) (location.getLatitude());
GlobalVars.lng = (Double) (location.getLongitude());
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// Nothing here

}

@Override
public void onProviderEnabled(String provider) {
Location location = getBestLocation(mLocationManager);
// Initialize the location fields
if (location != null) {
GlobalVars.lat = (Double) (location.getLatitude());
GlobalVars.lng = (Double) (location.getLongitude());
}
}

@Override
public void onProviderDisabled(String provider) {
}

最佳答案

经过进一步调查,我最终使用了一项服务。

public class LocationService extends Service implements LocationListener {

LocationManager locationManager;

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {

locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

Location loc = getBestLocation(locationManager);
GlobalVars.lat = (Double) (loc.getLatitude());
GlobalVars.lng = (Double) (loc.getLongitude());

}

public void onLocationChanged(Location loc) {
GlobalVars.lat = (Double) (loc.getLatitude());
GlobalVars.lng = (Double) (loc.getLongitude());
}

public static Location getBestLocation(LocationManager locationManager) {

Location location_gps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location_network = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

// If both are available, get the most recent
if(location_gps!=null && location_network !=null) {
return (location_gps.getTime() > location_network.getTime())?location_gps:location_network;
}
else if(location_gps==null && location_network ==null){
return null;
}
else
return (location_gps==null)?location_network:location_gps;

}

public void onProviderEnabled(String s){}
public void onProviderDisabled(String s){}
public void onStatusChanged(String s, int i, Bundle b){}

@Override
public void onDestroy() {
locationManager.removeUpdates(this);
}
}

关于android - 在我的应用程序中获取更新的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6395450/

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