Following is the code to exploit this hack -
private void turnGPSOn(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){ //if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
Then, there is few examples which just open the Android Settings page and then user has to toggle GPS settings, following is the code to launch settings page -
Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
Couple of months back, google introduced a standard way of asking user to turn on GPS by showing following dialog to user -
GPS is automatically turned on if user says 'Yes'. No need to open Settings page and then ask user to toggle GPS setting. Majority of standard apps use this method now. Ola, Uber and other gps location based cab booking apps use this method too.
In this blog post I will explain how to use this method and the I will explain how to use Geocoder class to get complete address from location longitude and latitude. Please note that -
- To use GPS, internet is not required i.e. longitude and latitude can be obtained even when device is not connected to internet.
- To get address from longitude and latitude, internet is required.
Following is step by step process of getting Android device location programatically by turning on GPS -
Step 1 - Create a Custom LocationListner which helps in listening to locations changes.
- This can be done by implementing LocationListener class.
- Then Geocoder class can be used to get address info from location. Geocoder class can provide following important properties encapsulated in Address object -
String mSubAdminArea;
String mLocality;
String mSubLocality;
String mThoroughfare;
String mSubThoroughfare;
String mPremises;
String mPostalCode;
String mCountryCode;
String mCountryName;
double mLatitude;
double mLongitude;
- Following is a sample implementation of LocationListener class -
private class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location loc) { String cityName=""; Geocoder gcd = new Geocoder(getBaseContext(),Locale.getDefault()); List<Address> addresses; try { addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); cityName=addresses.get(0).getLocality(); } catch (IOException e) { e.printStackTrace(); } textView.setText("Lat: " +loc.getLatitude() +"\n"+ " Lng: " + loc.getLongitude() +"\nMy Currrent City is: "+cityName); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } }
Step 2 - Check whether GPS is turned ON
- If GPS is turned ON, then add LocationListener created in Step 1.
- If GPS is not turned ON, then show a dialog to user.
public void requestToTurnOnGps() { GoogleApiClient 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); //DO NOT forget this, this is the key ingredient PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(LocationSettingsResult result) { final Status status = result.getStatus(); final LocationSettingsStates state = result.getLocationSettingsStates(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: //this means gps is already turned ON startListeningLocationUpdates(); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: //this means gps is turned off and you need to show the dialog try { status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { // Ignore the error. } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: textView.setText("Looks like GPS is not available in your device."); break; } } }); }
private void startListeningLocationUpdates() { try { textView.setText("Searching for location...\n It might take some time. Please wait ..."); locationListener = new MyLocationListener(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } catch (SecurityException e) { textView.setText("No permission to use GPS."); } }
Following line will launch a dialog to the user with Yes and No options. Result of the dialog i.e. user choose 'Yes' option or 'No' option will be given as onActivityResult with status code passed as second parameter (REQUEST_CHECK_SETTINGS).
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
Step 3 - Handle result of GPS turn ON\OFF dialog shown to user
- If user said Yes, then GPS must be enabled now. React accordingly.
- If user said No, then GPS should still be disabled. Handle this case.
- Following is sample implementation -
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_CHECK_SETTINGS: switch (resultCode) { case Activity.RESULT_OK: //user said YES and GPS is enabled startListeningLocationUpdates(); break; case Activity.RESULT_CANCELED: //user said NO and GPS is still disabled textView.setText("GPS is still disabled."); break; } break; } }
Step 4 - Stop listening to location updates once you have location.
If you want location only once then you can stop listening to location updates once you have location. Call following code whenever you want to stop listening to location updates -
private void stopListeningLocationUpdates() { try { if (locationListener != null) locationManager.removeUpdates(locationListener); } catch (SecurityException e) { //do nothing } }
You can download working Android project from this link.
This project asks request to enable GPS is not already enabled. Then it reads GPS co-ordinates. Once it has co-ordinates, it tries to get current city. Then it stop listening to location updates.
Following are the screenshots of this sample project -
Please feel free to comment below in case you have any doubts.
Thanks for your efforts in sharing this post with us. This was really awesome. kindly keep continuing the great work.
ReplyDeleteAndroid Training Institute in Chennai | Android Training Institute in anna nagar | Android Training Institute in omr | Android Training Institute in porur | Android Training Institute in tambaram | Android Training Institute in velachery