Get current Location of Android device

Overview

Getting current location of Android device is common task of developers. However there are a lot of solution for this issue. But some of them are very complex. Therefor, I will introduce my solution to solve this problem easily.

How to implement

Config:

public class LocationConfig {
    public static final int INTERVAL = 600000;
    public static final int MIN_DISTANCE = 50;
}

Write LocationService file:

public class LocationService implements LocationListener {
    private static final String TAG = LocationService.class.getSimpleName();
    private static LocationService instance;

    private LocationManager locationManager;
    private boolean isGPSEnabled, isNetworkEnabled;
    private Context context;

    public static LocationService getInstance(Context context) {
        if (instance == null) {
            instance = new LocationService(context);
        }
        return instance;
    }

    private LocationService(Context context) {
        this.context = context;
    }

    @TargetApi(23)
    public void initLocationService() {
        if (Build.VERSION.SDK_INT >= 23 &&
            ContextCompat.checkSelfPermission(this.context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ContextCompat.checkSelfPermission(this.context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Log.e(TAG, "Can not get location because don't have permission!");
            return;
        }

        try {
            this.locationManager = (LocationManager) this.context.getSystemService(
                Context.LOCATION_SERVICE);

            // Get GPS and network status
            this.isGPSEnabled = this.locationManager.isProviderEnabled(
                LocationManager.GPS_PROVIDER);
            this.isNetworkEnabled = this.locationManager.isProviderEnabled(
                LocationManager.NETWORK_PROVIDER);

            if (!this.isNetworkEnabled && !this.isGPSEnabled) {
                Log.e(TAG, "Can not get location because network and GPS are disable!");
            } else {
                if (this.isNetworkEnabled) {
                    Log.i(TAG, "Get location by network");
                    this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                        LocationConfig.INTERVAL, LocationConfig.MIN_DISTANCE, this);
                    if (this.locationManager != null) {
                        Location location = this.locationManager.getLastKnownLocation(
                            LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            this.updateLocation(location);
                            return;
                        }
                    }
                }
                if (this.isGPSEnabled) {
                    Log.i(TAG, "Get location by GPS");
                    this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                        LocationConfig.INTERVAL, LocationConfig.MIN_DISTANCE, this);
                    if (this.locationManager != null) {
                        Location location = this.locationManager.getLastKnownLocation(
                            LocationManager.GPS_PROVIDER);
                        this.updateLocation(location);
                        return;
                    }
                }
            }
        } catch (Exception ex) {
            Log.e(TAG, "initLocationService", ex);
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.i(TAG, "Location: " + location);
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {
        Log.i(TAG, "onStatusChanged: " + s);
    }

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

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

    private void updateLocation(Location location) {
        // write your code here
    }
}

Use LocationService to get current location:

LocationService service = LocationService.getInstance(context);
if (service != null) {
    service.initLocationService();
}

Conclusion

You can use my solution to get current location. However I recommend that you should to combine Receiver and my solution. This is the best way for getting current location in background task.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s