Turn on GPS Programmatically in Android 4.4 or Higher




Introduction: Turn on GPS Programmatically in Android 4.4 or Higher

About: I am an enthusiastic person who does not follow reality, instead who creates reality. I am sharing some projects with amazing people across the Globe (Of course you people) and I hope you will like them.

Hello friends

As we know Turing on GPS in older Android was very easy but in 4.4 or in higher version we can't turn on GPS directly due to security reasons ,but we can turn on indirectly.

So i am going to teach you how to do that

Let's Start................

Step 1: Step-1

Now open the android studio and click on new project....

Step 2:

Now change name of application whatever you want.

My app name is GPSON

Step 3:

Now select minimum Android version you want to target.

Step 4:

Now click on next button

Step 5:

Now click on finish

Step 6:

Now GUI will open,as show picture....

Step 7:

Now go to

file => project structure => app => Dependencies

now click on add Library dependencies

now add this line com.google.android.ms:play-services:8-4-0 and click ok.

Step 8:

Now just add this code into manifest file as shown in figure

<meta-data android:name="com.google.android.gms.version"

Step 9:

now just add following code in manifest file but outside of application block.....

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Step 10:

Now just copy and paste following code outside oncreate method.....

private GoogleApiClient googleApiClient;

Now just copy and paste following code in oncreate.....

if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(getApplicationContext()) .addApi(LocationServices.API) .build(); googleApiClient.connect();

LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(30 * 1000); locationRequest.setFastestInterval(5 * 1000); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(locationRequest);

// **************************

builder.setAlwaysShow(true); // this is the key ingredient

// **************************

PendingResult result = LocationServices.SettingsApi .checkLocationSettings(googleApiClient, builder.build()); result.setResultCallback(new ResultCallback()



public void onResult(LocationSettingsResult result)


final Status status = result.getStatus();

final LocationSettingsStates state = result .getLocationSettingsStates();

switch (status.getStatusCode())


case LocationSettingsStatusCodes.SUCCESS:


case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:

try {

status.startResolutionForResult(MainActivity.this, 1000);

} catch (IntentSender.SendIntentException e)




case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:





googleApiClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

Step 11:

Now just connect phone to pc and turn on USB debugging mode in mobile and click on run in android studio.

Step 12:

Now you can see that a dialog box will open when you start the app......


Be the First to Share


    • Make it Glow Contest

      Make it Glow Contest
    • First Time Author Contest

      First Time Author Contest
    • PCB Challenge

      PCB Challenge

    6 Discussions


    3 years ago

    you are turning on GPS with runtime permission and show a dialog box to get access.

    i want know is there any way to turn on GPS with out any dialog box and interface. how to turn on GPS in background on marshmallow?


    Reply 2 years ago

    Did you found out way for that how to do that without dialog box??


    4 years ago

    thanks, very nice!! i only had to face adding com.google.android.gms:play-services:9.2.1 (not com.google.android.ms:play-services:8-4-0) and all other gradle stuff. In code there is some problem with setResultCallback: in onResult an argumaent should be with type Result (not LocationSettingsResult), you can cast it afterwards. My only unsolved problem with turning GPS off, how can i do it???


    Reply 3 years ago

    Hello. Did you find a solution for turning off gps programmatically? If yes, Can you please share it with me via email?



    3 years ago

    thank you so much.... it working grate


    4 years ago

    It saved my day and this was what I was looking for. But the only
    problem I'm facing is onActivityResult I'm trying to get location but
    still it returns me 0.0 for both Latitude and Longitude. Can you help me
    with that part?