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
- import android.content.Context;
- import android.content.IntentSender;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.common.api.PendingResult;
- import com.google.android.gms.common.api.ResultCallback;
- import com.google.android.gms.common.api.Status;
- import com.google.android.gms.location.LocationRequest;
- import com.google.android.gms.location.LocationServices;
- import com.google.android.gms.location.LocationSettingsRequest;
- import com.google.android.gms.location.LocationSettingsResult;
- import com.google.android.gms.location.LocationSettingsStatusCodes;
- import java.util.List;
- public class LocationOnOff_Similar_To_Google_Maps extends AppCompatActivity {
- protected static final String TAG = "LocationOnOff";
- private GoogleApiClient googleApiClient;
- final static int REQUEST_LOCATION = 199;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.setFinishOnTouchOutside(true);
- // Todo Location Already on ... start
- final LocationManager manager = (LocationManager) LocationOnOff_Similar_To_Google_Maps.this.getSystemService(Context.LOCATION_SERVICE);
- if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && hasGPSDevice(LocationOnOff_Similar_To_Google_Maps.this)) {
- Toast.makeText(LocationOnOff_Similar_To_Google_Maps.this,"Gps already enabled",Toast.LENGTH_SHORT).show();
- finish();
- }
- // Todo Location Already on ... end
- if(!hasGPSDevice(LocationOnOff_Similar_To_Google_Maps.this)){
- Toast.makeText(LocationOnOff_Similar_To_Google_Maps.this,"Gps not Supported",Toast.LENGTH_SHORT).show();
- }
- if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && hasGPSDevice(LocationOnOff_Similar_To_Google_Maps.this)) {
- Log.e("Neha","Gps already enabled");
- Toast.makeText(LocationOnOff_Similar_To_Google_Maps.this,"Gps not enabled",Toast.LENGTH_SHORT).show();
- enableLoc();
- }else{
- Log.e("Neha","Gps already enabled");
- Toast.makeText(LocationOnOff_Similar_To_Google_Maps.this,"Gps already enabled",Toast.LENGTH_SHORT).show();
- }
- }
- private boolean hasGPSDevice(Context context) {
- final LocationManager mgr = (LocationManager) context
- .getSystemService(Context.LOCATION_SERVICE);
- if (mgr == null)
- return false;
- final List providers = mgr.getAllProviders();
- if (providers == null)
- return false;
- return providers.contains(LocationManager.GPS_PROVIDER);
- }
- private void enableLoc() {
- if (googleApiClient == null) {
- googleApiClient = new GoogleApiClient.Builder(LocationOnOff_Similar_To_Google_Maps.this)
- .addApi(LocationServices.API)
- .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
- @Override
- public void onConnected(Bundle bundle) {
- }
- @Override
- public void onConnectionSuspended(int i) {
- googleApiClient.connect();
- }
- })
- .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
- @Override
- public void onConnectionFailed(ConnectionResult connectionResult) {
- Log.d("Location error","Location error " + connectionResult.getErrorCode());
- }
- }).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);
- PendingResult result =
- LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
- result.setResultCallback(new ResultCallback() {
- @Override
- public void onResult(LocationSettingsResult result) {
- final Status status = result.getStatus();
- switch (status.getStatusCode()) {
- case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
- try {
- // Show the dialog by calling startResolutionForResult(),
- // and check the result in onActivityResult().
- status.startResolutionForResult(LocationOnOff_Similar_To_Google_Maps.this, REQUEST_LOCATION);
- finish();
- } catch (IntentSender.SendIntentException e) {
- // Ignore the error.
- }
- break;
- }
- }
- });
- }
- }
- }
Articoli simili
- Do you think Apple's occupying the third place behind Huawei and Samsung in terms of its smartphone sale is due to its overrated price?
- Perché rimuovono i tasti dedicati Home, End, Page Up, Page Down dalla tastiera di un laptop?
- Cosa sono l'ottimizzazione on-page e off-page? Come può essere utile?
- How to enable transfer files from my iPad and Mac to iCloud