androidgpslocationlistener

Is it possible to get gps co ordinates on network only


I am creating app which updates the location in background using service. Now it is done by gps and network provider both. But I want to get last location using network provider only because gps consumes lot of battery. I got code online which I modified is as below

public class MyLocationServiceOne extends Service {
    private static final String TAG = "MyLocationService";
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 0;//MIN_TIME_BW_UPDATES 1 Minute
    private static final float LOCATION_DISTANCE = 0f;//MIN_DISTANCE_CHANGE_FOR_UPDATES

    private class LocationListener implements android.location.LocationListener
    {
        Location mLastLocation;
        public LocationListener(String provider)
        {
            Log.e(TAG, "LocationListener " + provider);
            mLastLocation = new Location(provider);
        }
        @Override
        public void onLocationChanged(Location location)
        {
            Log.e(TAG, "onLocationChanged: " + location);
            Log.e(TAG, "Location : " + location.getLatitude()+" , "+
                    location.getLongitude());
            mLastLocation.set(location);
            double lati=location.getLatitude();
            double longi=location.getLongitude();
            if(String.valueOf(lati)!=null&&String.valueOf(longi)!=null) {
                Toast.makeText(MyLocationServiceOne.this, String.valueOf(lati)+String.valueOf(longi), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onProviderDisabled(String provider)
        {
            Log.e(TAG, "onProviderDisabled: " + provider);
        }

        @Override
        public void onProviderEnabled(String provider)
        {
            Log.e(TAG, "onProviderEnabled: " + provider);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras)
        {
            Log.e(TAG, "onStatusChanged: " + provider);
        }
    }

    LocationListener[] mLocationListeners = new LocationListener[] {
            //new LocationListener(LocationManager.GPS_PROVIDER),
            new LocationListener(LocationManager.NETWORK_PROVIDER)
    };

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.e(TAG, "onStartCommand");
        super.onStartCommand(intent, flags, startId);

        final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
        if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            //buildAlertMessageNoGps();
        }
        return START_STICKY;
    }

    @Override
    public void onCreate()
    {
        Log.e(TAG, "onCreate");
        initializeLocationManager();
        try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                    mLocationListeners[0]);

        } catch (java.lang.SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "network provider does not exist, " + ex.getMessage());
        }
        /*try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                    mLocationListeners[0]);
        } catch (java.lang.SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "gps provider does not exist " + ex.getMessage());
        }*/

    }

    @Override
    public void onDestroy()
    {
        Log.e(TAG, "onDestroy");
        super.onDestroy();
        if (mLocationManager != null) {
            for (int i = 0; i < mLocationListeners.length; i++) {
                try {
                    mLocationManager.removeUpdates(mLocationListeners[i]);
                } catch (Exception ex) {
                    Log.i(TAG, "fail to remove location listners, ignore", ex);
                }
            }
        }
    }

    private void initializeLocationManager() {
        Log.e(TAG, "initializeLocationManager");
        if (mLocationManager == null) {
            mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        }
    }
}

But it gives gps coordinates only when gps is on. Thanks for any help


Solution

  • Getting location through gps is painful to user since it consumes a lot of battery as you indicated above. You can select your provider too, that is what is source of getting location, is it network or it is gps. These all depends on your location needs and some other logics of app. well to cut short it, I must say you need to learn location strategies. This will give you better understanding. Please look this here.

    I will recommend you to use google's fused location API. This give you better control on locations, and the best part of this API is that it is battery efficient and it is really fast and reliable. here is a link if you want to learn about it. and for some more code snippts and helping material see this, this and this one is so advance also if you are using kotlin it has its demo code.

    Ask if there is any confusion.