Relsell
Relsell

Reputation: 771

Using FusedLocation Provider for Android Device

How to get location updates data from Fused location provider when GPS is turned off and wifi is turned on and connected .

I can get location data when GPS is turned on in device.

Upvotes: 0

Views: 84

Answers (2)

Sachin
Sachin

Reputation: 1448

Check out following example, hope it will solve your problem

http://javapapers.com/android/android-location-fused-provider/

Upvotes: 0

Divyesh Boda
Divyesh Boda

Reputation: 258

try this

public class LocationService extends Service {
    private LocationManager locationManager;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                getLocation();
            }
        });
        return Service.START_STICKY;
    }

    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            //your code hear
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
        }
    };

    private void getLocation() {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        if (locationManager != null) {
            String provider = locationManager.getBestProvider(criteria, true);
            if (provider != null) {
                Location location = locationManager.getLastKnownLocation(provider);
                updateWithNewLocation(location);
                locationManager.requestLocationUpdates(provider, 500, 50, locationListener);
            } else {
                if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 50, locationListener);
                } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 50, locationListener);
                } else if (locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)) {
                    locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 500, 50, locationListener);
                }
            }

        }
    }

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

    public void onDestroy() {
        super.onDestroy();
        if (locationManager != null && locationListener != null) {
            locationManager.removeUpdates(locationListener);
        }
    }
}

Upvotes: 1

Related Questions