3

Please help me to turn on GPS in my mobile app like OlaCabs.. I am using below code:

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);
}

But it's not able to turn on GPS..

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ranjit Bisht
  • 157
  • 10

3 Answers3

2

Android Guidelines have changed above version 4.0. You cannot change GPS off on programmatically for versions above 4.0...

Do not waste your time as I did

To use a Dialog check it:

https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi

or that:

https://stackoverflow.com/a/12446361/3626214

Community
  • 1
  • 1
Aspicas
  • 4,498
  • 4
  • 30
  • 53
  • 1
    Then how OlaCabs doing it – Ranjit Bisht Sep 17 '15 at 08:17
  • 1
    I think you need to install OlaCabs and see his functionality @Aspicas – Ranjit Bisht Sep 17 '15 at 08:18
  • No, apps like `OlaCabs`, `Google Maps`....open a Android dialog with permissions required, then you accept that permissions, but you cannot turn ON GPS service programmatically, but you can do a dialog, of course. @RanjitBisht – Aspicas Sep 17 '15 at 08:22
  • 1
    I think you using the old version of OlaCabs @Aspicas, ya i know when we open OlaCabs app we have a dialog whihc ask us to enable the GPS but after press on Yes button GPS turn on automatically.... Please install the old version and see the functionality.... Before OlaCabs look i also thinking like you but not now..... – Ranjit Bisht Sep 17 '15 at 08:28
  • @RanjitBisht impossible, I have Android 5.1.1, I said you that you can do it but only with dialog, you don't specified your question, for this reason I said that, but with dialog... of course, you can do it interacting with the location settings-enabler APIs for example... I have a dialog too on my app, check my update – Aspicas Sep 17 '15 at 08:31
  • there, i take the negative mark away (i didnt give it), stop asking for it – CDrosos Sep 17 '15 at 08:38
  • 1
    I suggest you please, first take an overview on OlaCabs's latest mobile app.. which implemented the required functionality...... @Aspicas – Ranjit Bisht Sep 17 '15 at 09:19
  • 1
    Ok i checked your updated it simply use to redirect the page to the setting page only which can be done by a single line i.e. activity.StartActivity(new Intent(Android.Provider.Settings.ActionLocationSourceSettings)); Which is not the correct solution..... @Aspicas – Ranjit Bisht Sep 17 '15 at 09:39
  • 1
    Sorry @Aspicas not found any solution using first link... I think i need to go through more research on it.... Thanks for help.... – Ranjit Bisht Sep 17 '15 at 11:26
2

For all those still looking for the Answer:

Here is how OLA Cabs and other such apps are doing it.

Add this in your onCreate

if (googleApiClient == null) {
            googleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API).addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(Login.this).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); // 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:
                        // All location settings are satisfied. The client can
                        // initialize location
                        // requests here.
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        // Location settings are not satisfied. But could be
                        // fixed by showing the user
                        // a dialog.
                        try {
                            // Show the dialog by calling
                            // startResolutionForResult(),
                            // and check the result in onActivityResult().
                            status.startResolutionForResult(Login.this, 1000);
                        } catch (IntentSender.SendIntentException e) {
                            // Ignore the error.
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        // Location settings are not satisfied. However, we have
                        // no way to fix the
                        // settings so we won't show the dialog.
                        break;
                    }
                }
            });
        }

These are the implmented methods:

@Override
    public void onConnected(Bundle arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        // TODO Auto-generated method stub

    }
Aishvarya Jaiswal
  • 1,793
  • 6
  • 34
  • 72
  • 2
    I think what they are asking is not having to show even a dialog box, which is not allowed unfortunately. Nevertheless, this is exactly what ola does! – pukingminion Apr 22 '16 at 05:21
0

You can show user a dialog and redirect him/her to settings when one enables location by hand

Max
  • 446
  • 4
  • 16