在我的应用中获取更新的位置

时间:2011-06-18 10:48:16

标签: android location listener gsp

我需要在整个应用中拥有更新的位置。我试图通过在我的所有活动中添加下面显示的代码来实现。我的问题是,我无法确定用户是否会在一个活动中停留足够长的时间来获取更新。如何让每个给定时间(例如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) {        
}

2 个答案:

答案 0 :(得分:1)

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

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);
    }
}

答案 1 :(得分:0)

每个Android应用程序中都有一个Application(或子类),您可以使用默认值来提供自己的应用程序范围的功能。只需将android.app.Application子类化即可添加您的附加功能,并在清单中声明您的应用程序。在清单的< application>中node,提供一个android:name属性,等于覆盖的全名。

一旦你有了它,给它一套静态的访问方法是微不足道的。