Implementing Location Permission in Android 5.0 or below

1 min read

Dear Friend

For accessing GPS Location Services GPS is to be turned on. Since turning on GPS impacts device battery’s life so System has given a privilege to user so he or she can turn on or off GPS as required from Setting or Quick Settings.

So if GPS functionality is critical to application we application developers should ask user to Turn on GPS if it not on. So application can use Location Services . You must have seen popular Location based Apps like Uber App or OLA app


Reference image



By the way these apps have made life easy. 🙂



please have a look at below function

public void isLocationEnabled(Context mContext) {
       LocationManager  lm = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(MainActivity.this, "Gps on", Toast.LENGTH_SHORT).show();

            // If location service is enabled then get the current location
        } else {
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setMessage("Please turn on Location").setCancelable(false);
            dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivityForResult(myIntent, 001);
            dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
            Toast.makeText(MainActivity.this, "Gps off, kindly turn it on", Toast.LENGTH_SHORT).show();

So above code is self explanatory we check the GPS is turned on or off. if it is off we ask user to turn it on. and if it is on we do other cooler stuff.


You need to remember doing implementation this way on Android 5.0 or below device provide the GPS permission to application directly if GPS is turned on. Now in Android Marshmallow a new permission model is introduced called Runtime permission which has changed the way how application request location permission from user. For that discussion please have a look at my another blog post.

Happy coding.