Android Location Manager, Get GPS location ,if no GPS then get to Network Provider location

前端 未结 7 2204
臣服心动
臣服心动 2020-11-29 20:52

I am using this given below code to get locations:

public Location getLocation() {
        try {
            mLocationManager = (LocationManager) context.get         


        
7条回答
  •  無奈伤痛
    2020-11-29 21:15

    The recommended way to do this is to use LocationClient:

    First, define location update interval values. Adjust this to your needs.

    private static final int MILLISECONDS_PER_SECOND = 1000;
    private static final long UPDATE_INTERVAL = MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS;
    private static final int FASTEST_INTERVAL_IN_SECONDS = 1;
    private static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS;
    

    Have your Activity implement GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, and LocationListener.

    public class LocationActivity extends Activity implements 
    GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {}
    

    Then, set up a LocationClientin the onCreate() method of your Activity:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        mLocationClient = new LocationClient(this, this, this);
    
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    }
    

    Add the required methods to your Activity; onConnected() is the method that is called when the LocationClientconnects. onLocationChanged() is where you'll retrieve the most up-to-date location.

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.w(TAG, "Location client connection failed");
    }
    
    @Override
    public void onConnected(Bundle dataBundle) {
        Log.d(TAG, "Location client connected");
        mLocationClient.requestLocationUpdates(mLocationRequest, this); 
    }
    
    @Override
    public void onDisconnected() {
        Log.d(TAG, "Location client disconnected");
    }
    
    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            Log.d(TAG, "Updated Location: " + Double.toString(location.getLatitude()) + "," + Double.toString(location.getLongitude()));
        } else {
            Log.d(TAG, "Updated location NULL");
        } 
    }     
    

    Be sure to connect/disconnect the LocationClient so it's only using extra battery when absolutely necessary and so the GPS doesn't run indefinitely. The LocationClient must be connected in order to get data from it.

    public void onResume() {
        super.onResume();
        mLocationClient.connect();
    }
    
    public void onStop() {
        if (mLocationClient.isConnected()) {
            mLocationClient.removeLocationUpdates(this);
        }
        mLocationClient.disconnect();
        super.onStop();
    }
    

    Get the user's location. First try using the LocationClient; if that fails, fall back to the LocationManager.

    public Location getLocation() {
        if (mLocationClient != null && mLocationClient.isConnected()) {
            return mLocationClient.getLastLocation();
        } else {
            LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            if (locationManager != null) {
                Location lastKnownLocationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (lastKnownLocationGPS != null) {
                    return lastKnownLocationGPS;
                } else {
                    return locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                }
            } else {
                return null;
            }
        }
    }
    

提交回复
热议问题