Friday, 4 December 2015

How to Enable New Location Dialog from Android Settings API

Enable a system location dialog ,asking user that app needs to find the current location This dialog is from Android Settings API. Check the source code below.


 YodmG


  1.  public class LocationData implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
  2. Context _context;
  3. Location latKnowLocation;
  4. static GoogleApiClient mGoogleApiClient;
  5. LocationInterface loIn;
  6. StartAct startAct;
  7. PendingResult result;
  8. //Location requests
  9. LocationRequest locationRequest = LocationRequest.create()
  10. .setInterval(10 * 60 * 1000) // every 10 minutes
  11. .setExpirationDuration(10 * 1000) // After 10 seconds
  12. .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  13. LocationSettingsRequest.Builder builder;
  14. public LocationData(Context con) {
  15. _context = con;
  16. mGoogleApiClient = new GoogleApiClient.Builder(con.getApplicationContext())
  17. .addConnectionCallbacks(this)
  18. .addOnConnectionFailedListener(this)
  19. .addApi(LocationServices.API)
  20. .build();
  21. mGoogleApiClient.connect();
  22. //initialize the builder and add location request paramenter like HIGH Aurracy
  23. builder = new LocationSettingsRequest.Builder()
  24. .addLocationRequest(locationRequest);
  25. // set builder to always true (Shows the dialog after never operation too)
  26. builder.setAlwaysShow(true);
  27. // Then check whether current location settings are satisfied:
  28. result =
  29. LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
  30. }
  31. // call back for other class
  32. public void callbacksResults()
  33. {
  34. // call backs for lcoation status
  35. result.setResultCallback(new ResultCallback() {
  36. @Override
  37. public void onResult(LocationSettingsResult result) {
  38. final Status status = result.getStatus();
  39. final LocationSettingsStates state = result.getLocationSettingsStates();
  40. switch (status.getStatusCode()) {
  41. case LocationSettingsStatusCodes.SUCCESS:
  42. // All location settings are satisfied. The client can initialize location
  43. // requests here.
  44. if(mGoogleApiClient.isConnected())
  45. {
  46. latKnowLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
  47. }
  48. break;
  49. case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
  50. // Location settings are not satisfied. But could be fixed by showing the user
  51. // a dialog.
  52. try {
  53. status.startResolutionForResult((Activity) _context, Constants.REQUEST_CHECK_SETTINGS);
  54. } catch (IntentSender.SendIntentException e) {
  55. e.printStackTrace();
  56. }
  57. break;
  58. case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
  59. // Location settings are not satisfied. However, we have no way to fix the
  60. // settings so we won't show the dialog.
  61. break;
  62. }
  63. }
  64. });
  65. }
  66. // call back for other class
  67. public void callbacksResultsSplash()
  68. {
  69. // call backs for lcoation status
  70. result.setResultCallback(new ResultCallback() {
  71. @Override
  72. public void onResult(LocationSettingsResult result) {
  73. final Status status = result.getStatus();
  74. final LocationSettingsStates state = result.getLocationSettingsStates();
  75. switch (status.getStatusCode()) {
  76. case LocationSettingsStatusCodes.SUCCESS:
  77. .......
  Read full blog at our highly specific C, Java, PHP, Javascript, iPhone, android developer forum about the topic described above "How to Enable New Location Dialog from Android Settings API". You can also learn much more about different programming technologies and can enhance your tech skills.

No comments:

Post a Comment