Casa > H > How Does The Ola App Enable Gps Automatically Without Going To The Device's Setting Page And Retrieve Its Location So Fast For An Android Application?

How does the OLA app enable GPS automatically without going to the device's setting page and retrieve its location so fast for an Android application?

Few Lines of code and this will do things same like OLA or Google maps

You can folloe this androidneha/Automatically-Turn-On-Off-Gps OR

  1. import android.content.Context; 
  2. import android.content.IntentSender; 
  3. import android.location.LocationManager; 
  4. import android.os.Bundle; 
  5. import android.support.v7.app.AppCompatActivity; 
  6. import android.util.Log; 
  7. import android.widget.Button; 
  8. import android.widget.TextView; 
  9. import android.widget.Toast; 
  10.  
  11. import com.google.android.gms.common.ConnectionResult; 
  12. import com.google.android.gms.common.api.GoogleApiClient; 
  13. import com.google.android.gms.common.api.PendingResult; 
  14. import com.google.android.gms.common.api.ResultCallback; 
  15. import com.google.android.gms.common.api.Status; 
  16. import com.google.android.gms.location.LocationRequest; 
  17. import com.google.android.gms.location.LocationServices; 
  18. import com.google.android.gms.location.LocationSettingsRequest; 
  19. import com.google.android.gms.location.LocationSettingsResult; 
  20. import com.google.android.gms.location.LocationSettingsStatusCodes; 
  21.  
  22. import java.util.List; 
  23.  
  24. public class LocationOnOff_Similar_To_Google_Maps extends AppCompatActivity { 
  25.  
  26. protected static final String TAG = "LocationOnOff"; 
  27.  
  28.  
  29. private GoogleApiClient googleApiClient; 
  30. final static int REQUEST_LOCATION = 199; 
  31.  
  32. @Override 
  33. protected void onCreate(Bundle savedInstanceState) { 
  34. super.onCreate(savedInstanceState); 
  35.  
  36. this.setFinishOnTouchOutside(true); 
  37.  
  38. // Todo Location Already on ... start 
  39. final LocationManager manager = (LocationManager) LocationOnOff_Similar_To_Google_Maps.this.getSystemService(Context.LOCATION_SERVICE); 
  40. if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && hasGPSDevice(LocationOnOff_Similar_To_Google_Maps.this)) { 
  41. Toast.makeText(LocationOnOff_Similar_To_Google_Maps.this,"Gps already enabled",Toast.LENGTH_SHORT).show(); 
  42. finish(); 
  43. // Todo Location Already on ... end 
  44.  
  45. if(!hasGPSDevice(LocationOnOff_Similar_To_Google_Maps.this)){ 
  46. Toast.makeText(LocationOnOff_Similar_To_Google_Maps.this,"Gps not Supported",Toast.LENGTH_SHORT).show(); 
  47.  
  48. if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && hasGPSDevice(LocationOnOff_Similar_To_Google_Maps.this)) { 
  49. Log.e("Neha","Gps already enabled"); 
  50. Toast.makeText(LocationOnOff_Similar_To_Google_Maps.this,"Gps not enabled",Toast.LENGTH_SHORT).show(); 
  51. enableLoc(); 
  52. }else{ 
  53. Log.e("Neha","Gps already enabled"); 
  54. Toast.makeText(LocationOnOff_Similar_To_Google_Maps.this,"Gps already enabled",Toast.LENGTH_SHORT).show(); 
  55.  
  56.  
  57. private boolean hasGPSDevice(Context context) { 
  58. final LocationManager mgr = (LocationManager) context 
  59. .getSystemService(Context.LOCATION_SERVICE); 
  60. if (mgr == null) 
  61. return false; 
  62. final List providers = mgr.getAllProviders(); 
  63. if (providers == null) 
  64. return false; 
  65. return providers.contains(LocationManager.GPS_PROVIDER); 
  66.  
  67. private void enableLoc() { 
  68.  
  69. if (googleApiClient == null) { 
  70. googleApiClient = new GoogleApiClient.Builder(LocationOnOff_Similar_To_Google_Maps.this) 
  71. .addApi(LocationServices.API) 
  72. .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { 
  73. @Override 
  74. public void onConnected(Bundle bundle) { 
  75.  
  76.  
  77. @Override 
  78. public void onConnectionSuspended(int i) { 
  79. googleApiClient.connect(); 
  80. }) 
  81. .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() { 
  82. @Override 
  83. public void onConnectionFailed(ConnectionResult connectionResult) { 
  84.  
  85. Log.d("Location error","Location error " + connectionResult.getErrorCode()); 
  86. }).build(); 
  87. googleApiClient.connect(); 
  88.  
  89. LocationRequest locationRequest = LocationRequest.create(); 
  90. locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
  91. locationRequest.setInterval(30 * 1000); 
  92. locationRequest.setFastestInterval(5 * 1000); 
  93. LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
  94. .addLocationRequest(locationRequest); 
  95.  
  96. builder.setAlwaysShow(true); 
  97.  
  98. PendingResult result = 
  99. LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build()); 
  100. result.setResultCallback(new ResultCallback() { 
  101. @Override 
  102. public void onResult(LocationSettingsResult result) { 
  103. final Status status = result.getStatus(); 
  104. switch (status.getStatusCode()) { 
  105. case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
  106. try { 
  107. // Show the dialog by calling startResolutionForResult(), 
  108. // and check the result in onActivityResult(). 
  109. status.startResolutionForResult(LocationOnOff_Similar_To_Google_Maps.this, REQUEST_LOCATION); 
  110.  
  111. finish(); 
  112. } catch (IntentSender.SendIntentException e) { 
  113. // Ignore the error. 
  114. break; 
  115. }); 
  116.  

De Cleve Degen

Que informações os aplicativos coletam? :: Qual é o melhor aplicativo para rastrear todas as despesas do seu veículo?