Turn on GPS Programmatically in Android 4.4 or Higher

by prayag nao in Circuits > Mobile

40894 Views, 18 Favorites, 0 Comments

Turn on GPS Programmatically in Android 4.4 or Higher

gps-compass-19128415.jpg

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

Screen Shot 2016-05-28 at 10.40.45 am.png

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

Screen Shot 2016-05-28 at 10.41.12 am.png

Now change name of application whatever you want.

My app name is GPSON

Screen Shot 2016-05-28 at 10.41.23 am.png

Now select minimum Android version you want to target.

Screen Shot 2016-05-28 at 10.41.31 am.png

Now click on next button

Screen Shot 2016-05-28 at 10.41.44 am.png

Now click on finish

Screen Shot 2016-05-28 at 10.46.35 am.png

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

Screen Shot 2016-05-28 at 10.46.50 am.png
Screen Shot 2016-05-28 at 10.53.30 am.png

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.

Screen Shot 2016-05-28 at 10.54.09 am.png

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

<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>

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"/>

Screen Shot 2016-05-28 at 10.54.51 am.png

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()

{

@Override

public void onResult(LocationSettingsResult result)

{

final Status status = result.getStatus();

final LocationSettingsStates state = result .getLocationSettingsStates();

switch (status.getStatusCode())

{

case LocationSettingsStatusCodes.SUCCESS:

break;

case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:


try {

status.startResolutionForResult(MainActivity.this, 1000);

} catch (IntentSender.SendIntentException e)

{

}

break;

case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:

break;

}

});

}

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

Screen Shot 2016-05-28 at 11.02.26 am.png

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

Screenshot_2016-05-28-11-08-11_com.google.android.gms.png

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

Enjoy.