获取整个应用程序中的最新位置

| 我需要在整个应用中更新位置。我正在尝试通过在我的所有活动中添加以下代码来做到这一点。我的问题是,我不能确定用户将在一项活动中停留足够长的时间来获取更新。如何在给定的时间(例如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应用程序中都有一个应用程序(或子类),您可以使用默认值来提供自己的应用程序范围的功能。只需将android.app.Application子类化即可添加其他功能,然后在清单中声明您的Application。在清单的节点中,提供一个android:name属性,该属性等于覆盖的全名。 一旦做到这一点,给它一个静态的访问方法集就很简单了。     

要回复问题请先登录注册