Android Development : Aksing user to turn on the GPS
Dialog box : Turn on the GPS
Recently, in one of my android project work, I needed GPS location of the user device. I wanted it to look like the dialog box which appears in the Google Maps when we turn in on.
This is how Google asks user permission for GPS access:
So, after referring the Google's official documentation and some more modifications following code worked like a charm.
I had integrated the map and the code somewhat goes like this:
First you have to add manifest permissions:
I hope this code helps you well. If you have any doubts, please feel free to comment down below.
This is how Google asks user permission for GPS access:
So, after referring the Google's official documentation and some more modifications following code worked like a charm.
I had integrated the map and the code somewhat goes like this:
First you have to add manifest permissions:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Then edit your MainActivity like this :public class MainActivity extends AppCompatActivity implements
ConnectionCallbacks, OnConnectionFailedListener, LocationListener{
GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
enable_loc(); //This will call the actual methods to implement the dialog box
}
}
//ask to turn on the gps
private void enableLoc() {
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);
PendingResult result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
Log.d("status.getStatusCode()",String.valueOf(status.getStatusCode()));
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(
MainActivity.this, REQUEST_LOCATION);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_LOCATION:
switch (resultCode) {
case Activity.RESULT_CANCELED: {
// The user was asked to change settings, but chose not to
finish();
break;
}
case Activity.RESULT_OK:
//write here what you want to do next
break;
default: {
break;
}
}
break;
}
}
I hope this code helps you well. If you have any doubts, please feel free to comment down below.
Comments
Post a Comment