Enabling Location mode High Accuracy or Battery saving, programmatically, without user needing to visit...












18















Why i ask this:(also the reason for trying it in an app)



It happens when we use Google Maps in Lollipop. Even if the Location is disabled, it is turned on, in high accuracy mode after user's input from the Maps app, without having to visit Settings.



A similar functionality can be achieved for enabling Bluetooth, where the action is initiated in my app; user needs to make a choice but user is not redirected to Settings, using:



startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));



which could be found on BluetoothAdapter, now we know there is no LocationAdapter, so i looked around gms->LocationServices, basically almost everything under Location API references, android.location.LocationManager but doesn't seem anything like ACTION_REQUEST_ENABLE is available as yet.



Hope there is some other method for the same, and more people have tried it.



Please note:
context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); does not work like that.










share|improve this question





























    18















    Why i ask this:(also the reason for trying it in an app)



    It happens when we use Google Maps in Lollipop. Even if the Location is disabled, it is turned on, in high accuracy mode after user's input from the Maps app, without having to visit Settings.



    A similar functionality can be achieved for enabling Bluetooth, where the action is initiated in my app; user needs to make a choice but user is not redirected to Settings, using:



    startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));



    which could be found on BluetoothAdapter, now we know there is no LocationAdapter, so i looked around gms->LocationServices, basically almost everything under Location API references, android.location.LocationManager but doesn't seem anything like ACTION_REQUEST_ENABLE is available as yet.



    Hope there is some other method for the same, and more people have tried it.



    Please note:
    context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); does not work like that.










    share|improve this question



























      18












      18








      18


      19






      Why i ask this:(also the reason for trying it in an app)



      It happens when we use Google Maps in Lollipop. Even if the Location is disabled, it is turned on, in high accuracy mode after user's input from the Maps app, without having to visit Settings.



      A similar functionality can be achieved for enabling Bluetooth, where the action is initiated in my app; user needs to make a choice but user is not redirected to Settings, using:



      startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));



      which could be found on BluetoothAdapter, now we know there is no LocationAdapter, so i looked around gms->LocationServices, basically almost everything under Location API references, android.location.LocationManager but doesn't seem anything like ACTION_REQUEST_ENABLE is available as yet.



      Hope there is some other method for the same, and more people have tried it.



      Please note:
      context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); does not work like that.










      share|improve this question
















      Why i ask this:(also the reason for trying it in an app)



      It happens when we use Google Maps in Lollipop. Even if the Location is disabled, it is turned on, in high accuracy mode after user's input from the Maps app, without having to visit Settings.



      A similar functionality can be achieved for enabling Bluetooth, where the action is initiated in my app; user needs to make a choice but user is not redirected to Settings, using:



      startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));



      which could be found on BluetoothAdapter, now we know there is no LocationAdapter, so i looked around gms->LocationServices, basically almost everything under Location API references, android.location.LocationManager but doesn't seem anything like ACTION_REQUEST_ENABLE is available as yet.



      Hope there is some other method for the same, and more people have tried it.



      Please note:
      context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); does not work like that.







      android google-play-services android-5.0-lollipop android-location android-settings






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 7 '17 at 6:34







      Pararth

















      asked Feb 27 '15 at 7:11









      PararthPararth

      7,31242549




      7,31242549
























          3 Answers
          3






          active

          oldest

          votes


















          22














          UPDATE 3:



          Prompt the user to change location settings
          as @patrickandroid, mentioned in comments, the link from second update is broken



          GoogleSamples; android Location and options - for code reference.



          UPDATE 2:



          GoogleSamples; - for code reference.




          This sample builds on the LocationUpdates sample included in this
          repo, and allows the user to update the device's location settings
          using a location dialog.



          Uses the SettingsApi to ensure that the device's system settings are
          properly configured for the app's location needs.






          UPDATE 1:



          Google Play services, Version 7.0 (March 2015) released...




          Location settings - While the FusedLocationProviderApi combines multiple sensors to give you the optimal location, the accuracy of the
          location your app receives still depends greatly on the settings
          enabled on the device (GPS, wifi, airplane mode, and others). Using
          the new SettingsApi class, you can bring up a Location Settings dialog
          which displays a one-touch control for users to change their settings
          without leaving your app.




          Using the public interface SettingsApi





          • Determine if the relevant system settings are enabled on the device to carry out the desired location request.

          • Optionally, invoke a dialog that allows the user to enable the necessary location settings with a single tap.








          Leaving the previous part for reference:
          Update/Answer

          For everybody looking for this answer, Google Play Services 7.0

          It Adds APIs For Detecting Places And Connecting To Nearby Devices, Improves On Mobile Ads, Fitness Data, Location Settings, And More




          In Google Play services 7.0, we’re introducing a standard mechanism to
          check that the necessary location settings are enabled for a given
          LocationRequest to succeed. If there are possible improvements, you
          can display a one touch control for the user to change their settings
          without leaving your app.



          exactly this



          This API provides a great opportunity to make for a much better user
          experience, particularly if location information is critical to the
          user experience of your app such as was the case with Google Maps when
          they integrated the Location Settings dialog and saw a dramatic
          increase in the number of users in a good location state.




          Source: Android developers blog: Google Play services 7.0 - Places Everyone!




          SDK Coming Soon!

          We will be rolling out Google Play services 7.0 over
          the next few days. Expect an update to this blog post, published
          documentation, and the availability of the SDK once the rollout is
          completed.




          will update the programmatic l-o-c after implementation






          share|improve this answer


























          • i need new SDK download can you add link ??

            – Ando Masahashi
            Mar 25 '15 at 9:22











          • basically your SDK manager -> Extras -> Google Play Services , update that and it will be included in your library project

            – Pararth
            Mar 25 '15 at 9:25











          • yes but it not provide me to update

            – Ando Masahashi
            Mar 25 '15 at 10:13











          • then you would have it already

            – Pararth
            Apr 7 '15 at 6:22











          • The first link above is broken.

            – patrickandroid
            Jan 25 at 17:26





















          20














          Based on @user2450263 notes above, here are some snippets,



          /**
          * Prompt user to enable GPS and Location Services
          * @param mGoogleApiClient
          * @param activity
          */
          public static void locationChecker(GoogleApiClient mGoogleApiClient, final Activity activity) {
          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<LocationSettingsResult> result =
          LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, 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(
          activity, 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;
          }
          }
          });
          }


          And use it like,



          mGoogleApiClient = new GoogleApiClient
          .Builder(this)
          .enableAutoManage(this, 34992, this)
          .addApi(LocationServices.API)
          .addConnectionCallbacks(this)
          .addOnConnectionFailedListener(this)
          .build();

          locationChecker(mGoogleApiClient, MyActivity.this);


          Basically your user will get a prompt like this, where they can enable Location in High Accuracy mode without going in the settings
          Turn on GPS






          share|improve this answer































            0














            I have solution, tested on Android 6.0.1 Marshmallow, on button event OnClick call the void:



            private void openGPSSettings() {
            //Get GPS now state (open or closed)
            boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER);

            if (gpsEnabled) {
            Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE, 0);
            } else {
            Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE, 3);
            }
            }


            Android needs to have USB debugging enabled or be rooted. Execute the command below in adb shell (from a root shell on the device or from a computer connected by USB if the device is not rooted):



            pm grant com.example.application.test android.permission.WRITE_SECURE_SETTINGS


            Add these permissions in app manifest:



            <uses-permission android:name="android.permission.WRITE_SETTINGS" />
            <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />





            share|improve this answer





















            • 4





              Not all phones are rooted. This is not a feasible solution.

              – PsyGik
              Feb 29 '16 at 8:26






            • 1





              Awesome! works for me :)

              – Rosenpin
              Jul 22 '16 at 6:29











            • This does not work with systemless rooted android 6 devices, does it? The app needs to be a system app as well, correct? I'm talking about android 6 and above only...

              – prom85
              Jul 25 '16 at 19:45








            • 1





              why is it not feasible? Works for me. I need for testing to switch between location modes, so I installed my app on UNrooted Galaxy S6, run "adb shell pm grant <my package> android.permission.WRITE_SECURE_SETTINGS", it does the job (phone pops up dialog for the first time but then, mode consistently gets switched to "High accuracy" mode, ).

              – waggledans
              Sep 15 '16 at 18:47











            • It's not a viable solution for most Android apps though.

              – patrickandroid
              Jan 11 at 19:14











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28759454%2fenabling-location-mode-high-accuracy-or-battery-saving-programmatically-withou%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            22














            UPDATE 3:



            Prompt the user to change location settings
            as @patrickandroid, mentioned in comments, the link from second update is broken



            GoogleSamples; android Location and options - for code reference.



            UPDATE 2:



            GoogleSamples; - for code reference.




            This sample builds on the LocationUpdates sample included in this
            repo, and allows the user to update the device's location settings
            using a location dialog.



            Uses the SettingsApi to ensure that the device's system settings are
            properly configured for the app's location needs.






            UPDATE 1:



            Google Play services, Version 7.0 (March 2015) released...




            Location settings - While the FusedLocationProviderApi combines multiple sensors to give you the optimal location, the accuracy of the
            location your app receives still depends greatly on the settings
            enabled on the device (GPS, wifi, airplane mode, and others). Using
            the new SettingsApi class, you can bring up a Location Settings dialog
            which displays a one-touch control for users to change their settings
            without leaving your app.




            Using the public interface SettingsApi





            • Determine if the relevant system settings are enabled on the device to carry out the desired location request.

            • Optionally, invoke a dialog that allows the user to enable the necessary location settings with a single tap.








            Leaving the previous part for reference:
            Update/Answer

            For everybody looking for this answer, Google Play Services 7.0

            It Adds APIs For Detecting Places And Connecting To Nearby Devices, Improves On Mobile Ads, Fitness Data, Location Settings, And More




            In Google Play services 7.0, we’re introducing a standard mechanism to
            check that the necessary location settings are enabled for a given
            LocationRequest to succeed. If there are possible improvements, you
            can display a one touch control for the user to change their settings
            without leaving your app.



            exactly this



            This API provides a great opportunity to make for a much better user
            experience, particularly if location information is critical to the
            user experience of your app such as was the case with Google Maps when
            they integrated the Location Settings dialog and saw a dramatic
            increase in the number of users in a good location state.




            Source: Android developers blog: Google Play services 7.0 - Places Everyone!




            SDK Coming Soon!

            We will be rolling out Google Play services 7.0 over
            the next few days. Expect an update to this blog post, published
            documentation, and the availability of the SDK once the rollout is
            completed.




            will update the programmatic l-o-c after implementation






            share|improve this answer


























            • i need new SDK download can you add link ??

              – Ando Masahashi
              Mar 25 '15 at 9:22











            • basically your SDK manager -> Extras -> Google Play Services , update that and it will be included in your library project

              – Pararth
              Mar 25 '15 at 9:25











            • yes but it not provide me to update

              – Ando Masahashi
              Mar 25 '15 at 10:13











            • then you would have it already

              – Pararth
              Apr 7 '15 at 6:22











            • The first link above is broken.

              – patrickandroid
              Jan 25 at 17:26


















            22














            UPDATE 3:



            Prompt the user to change location settings
            as @patrickandroid, mentioned in comments, the link from second update is broken



            GoogleSamples; android Location and options - for code reference.



            UPDATE 2:



            GoogleSamples; - for code reference.




            This sample builds on the LocationUpdates sample included in this
            repo, and allows the user to update the device's location settings
            using a location dialog.



            Uses the SettingsApi to ensure that the device's system settings are
            properly configured for the app's location needs.






            UPDATE 1:



            Google Play services, Version 7.0 (March 2015) released...




            Location settings - While the FusedLocationProviderApi combines multiple sensors to give you the optimal location, the accuracy of the
            location your app receives still depends greatly on the settings
            enabled on the device (GPS, wifi, airplane mode, and others). Using
            the new SettingsApi class, you can bring up a Location Settings dialog
            which displays a one-touch control for users to change their settings
            without leaving your app.




            Using the public interface SettingsApi





            • Determine if the relevant system settings are enabled on the device to carry out the desired location request.

            • Optionally, invoke a dialog that allows the user to enable the necessary location settings with a single tap.








            Leaving the previous part for reference:
            Update/Answer

            For everybody looking for this answer, Google Play Services 7.0

            It Adds APIs For Detecting Places And Connecting To Nearby Devices, Improves On Mobile Ads, Fitness Data, Location Settings, And More




            In Google Play services 7.0, we’re introducing a standard mechanism to
            check that the necessary location settings are enabled for a given
            LocationRequest to succeed. If there are possible improvements, you
            can display a one touch control for the user to change their settings
            without leaving your app.



            exactly this



            This API provides a great opportunity to make for a much better user
            experience, particularly if location information is critical to the
            user experience of your app such as was the case with Google Maps when
            they integrated the Location Settings dialog and saw a dramatic
            increase in the number of users in a good location state.




            Source: Android developers blog: Google Play services 7.0 - Places Everyone!




            SDK Coming Soon!

            We will be rolling out Google Play services 7.0 over
            the next few days. Expect an update to this blog post, published
            documentation, and the availability of the SDK once the rollout is
            completed.




            will update the programmatic l-o-c after implementation






            share|improve this answer


























            • i need new SDK download can you add link ??

              – Ando Masahashi
              Mar 25 '15 at 9:22











            • basically your SDK manager -> Extras -> Google Play Services , update that and it will be included in your library project

              – Pararth
              Mar 25 '15 at 9:25











            • yes but it not provide me to update

              – Ando Masahashi
              Mar 25 '15 at 10:13











            • then you would have it already

              – Pararth
              Apr 7 '15 at 6:22











            • The first link above is broken.

              – patrickandroid
              Jan 25 at 17:26
















            22












            22








            22







            UPDATE 3:



            Prompt the user to change location settings
            as @patrickandroid, mentioned in comments, the link from second update is broken



            GoogleSamples; android Location and options - for code reference.



            UPDATE 2:



            GoogleSamples; - for code reference.




            This sample builds on the LocationUpdates sample included in this
            repo, and allows the user to update the device's location settings
            using a location dialog.



            Uses the SettingsApi to ensure that the device's system settings are
            properly configured for the app's location needs.






            UPDATE 1:



            Google Play services, Version 7.0 (March 2015) released...




            Location settings - While the FusedLocationProviderApi combines multiple sensors to give you the optimal location, the accuracy of the
            location your app receives still depends greatly on the settings
            enabled on the device (GPS, wifi, airplane mode, and others). Using
            the new SettingsApi class, you can bring up a Location Settings dialog
            which displays a one-touch control for users to change their settings
            without leaving your app.




            Using the public interface SettingsApi





            • Determine if the relevant system settings are enabled on the device to carry out the desired location request.

            • Optionally, invoke a dialog that allows the user to enable the necessary location settings with a single tap.








            Leaving the previous part for reference:
            Update/Answer

            For everybody looking for this answer, Google Play Services 7.0

            It Adds APIs For Detecting Places And Connecting To Nearby Devices, Improves On Mobile Ads, Fitness Data, Location Settings, And More




            In Google Play services 7.0, we’re introducing a standard mechanism to
            check that the necessary location settings are enabled for a given
            LocationRequest to succeed. If there are possible improvements, you
            can display a one touch control for the user to change their settings
            without leaving your app.



            exactly this



            This API provides a great opportunity to make for a much better user
            experience, particularly if location information is critical to the
            user experience of your app such as was the case with Google Maps when
            they integrated the Location Settings dialog and saw a dramatic
            increase in the number of users in a good location state.




            Source: Android developers blog: Google Play services 7.0 - Places Everyone!




            SDK Coming Soon!

            We will be rolling out Google Play services 7.0 over
            the next few days. Expect an update to this blog post, published
            documentation, and the availability of the SDK once the rollout is
            completed.




            will update the programmatic l-o-c after implementation






            share|improve this answer















            UPDATE 3:



            Prompt the user to change location settings
            as @patrickandroid, mentioned in comments, the link from second update is broken



            GoogleSamples; android Location and options - for code reference.



            UPDATE 2:



            GoogleSamples; - for code reference.




            This sample builds on the LocationUpdates sample included in this
            repo, and allows the user to update the device's location settings
            using a location dialog.



            Uses the SettingsApi to ensure that the device's system settings are
            properly configured for the app's location needs.






            UPDATE 1:



            Google Play services, Version 7.0 (March 2015) released...




            Location settings - While the FusedLocationProviderApi combines multiple sensors to give you the optimal location, the accuracy of the
            location your app receives still depends greatly on the settings
            enabled on the device (GPS, wifi, airplane mode, and others). Using
            the new SettingsApi class, you can bring up a Location Settings dialog
            which displays a one-touch control for users to change their settings
            without leaving your app.




            Using the public interface SettingsApi





            • Determine if the relevant system settings are enabled on the device to carry out the desired location request.

            • Optionally, invoke a dialog that allows the user to enable the necessary location settings with a single tap.








            Leaving the previous part for reference:
            Update/Answer

            For everybody looking for this answer, Google Play Services 7.0

            It Adds APIs For Detecting Places And Connecting To Nearby Devices, Improves On Mobile Ads, Fitness Data, Location Settings, And More




            In Google Play services 7.0, we’re introducing a standard mechanism to
            check that the necessary location settings are enabled for a given
            LocationRequest to succeed. If there are possible improvements, you
            can display a one touch control for the user to change their settings
            without leaving your app.



            exactly this



            This API provides a great opportunity to make for a much better user
            experience, particularly if location information is critical to the
            user experience of your app such as was the case with Google Maps when
            they integrated the Location Settings dialog and saw a dramatic
            increase in the number of users in a good location state.




            Source: Android developers blog: Google Play services 7.0 - Places Everyone!




            SDK Coming Soon!

            We will be rolling out Google Play services 7.0 over
            the next few days. Expect an update to this blog post, published
            documentation, and the availability of the SDK once the rollout is
            completed.




            will update the programmatic l-o-c after implementation







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 29 at 4:22

























            answered Mar 12 '15 at 5:53









            PararthPararth

            7,31242549




            7,31242549













            • i need new SDK download can you add link ??

              – Ando Masahashi
              Mar 25 '15 at 9:22











            • basically your SDK manager -> Extras -> Google Play Services , update that and it will be included in your library project

              – Pararth
              Mar 25 '15 at 9:25











            • yes but it not provide me to update

              – Ando Masahashi
              Mar 25 '15 at 10:13











            • then you would have it already

              – Pararth
              Apr 7 '15 at 6:22











            • The first link above is broken.

              – patrickandroid
              Jan 25 at 17:26





















            • i need new SDK download can you add link ??

              – Ando Masahashi
              Mar 25 '15 at 9:22











            • basically your SDK manager -> Extras -> Google Play Services , update that and it will be included in your library project

              – Pararth
              Mar 25 '15 at 9:25











            • yes but it not provide me to update

              – Ando Masahashi
              Mar 25 '15 at 10:13











            • then you would have it already

              – Pararth
              Apr 7 '15 at 6:22











            • The first link above is broken.

              – patrickandroid
              Jan 25 at 17:26



















            i need new SDK download can you add link ??

            – Ando Masahashi
            Mar 25 '15 at 9:22





            i need new SDK download can you add link ??

            – Ando Masahashi
            Mar 25 '15 at 9:22













            basically your SDK manager -> Extras -> Google Play Services , update that and it will be included in your library project

            – Pararth
            Mar 25 '15 at 9:25





            basically your SDK manager -> Extras -> Google Play Services , update that and it will be included in your library project

            – Pararth
            Mar 25 '15 at 9:25













            yes but it not provide me to update

            – Ando Masahashi
            Mar 25 '15 at 10:13





            yes but it not provide me to update

            – Ando Masahashi
            Mar 25 '15 at 10:13













            then you would have it already

            – Pararth
            Apr 7 '15 at 6:22





            then you would have it already

            – Pararth
            Apr 7 '15 at 6:22













            The first link above is broken.

            – patrickandroid
            Jan 25 at 17:26







            The first link above is broken.

            – patrickandroid
            Jan 25 at 17:26















            20














            Based on @user2450263 notes above, here are some snippets,



            /**
            * Prompt user to enable GPS and Location Services
            * @param mGoogleApiClient
            * @param activity
            */
            public static void locationChecker(GoogleApiClient mGoogleApiClient, final Activity activity) {
            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<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, 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(
            activity, 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;
            }
            }
            });
            }


            And use it like,



            mGoogleApiClient = new GoogleApiClient
            .Builder(this)
            .enableAutoManage(this, 34992, this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

            locationChecker(mGoogleApiClient, MyActivity.this);


            Basically your user will get a prompt like this, where they can enable Location in High Accuracy mode without going in the settings
            Turn on GPS






            share|improve this answer




























              20














              Based on @user2450263 notes above, here are some snippets,



              /**
              * Prompt user to enable GPS and Location Services
              * @param mGoogleApiClient
              * @param activity
              */
              public static void locationChecker(GoogleApiClient mGoogleApiClient, final Activity activity) {
              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<LocationSettingsResult> result =
              LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, 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(
              activity, 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;
              }
              }
              });
              }


              And use it like,



              mGoogleApiClient = new GoogleApiClient
              .Builder(this)
              .enableAutoManage(this, 34992, this)
              .addApi(LocationServices.API)
              .addConnectionCallbacks(this)
              .addOnConnectionFailedListener(this)
              .build();

              locationChecker(mGoogleApiClient, MyActivity.this);


              Basically your user will get a prompt like this, where they can enable Location in High Accuracy mode without going in the settings
              Turn on GPS






              share|improve this answer


























                20












                20








                20







                Based on @user2450263 notes above, here are some snippets,



                /**
                * Prompt user to enable GPS and Location Services
                * @param mGoogleApiClient
                * @param activity
                */
                public static void locationChecker(GoogleApiClient mGoogleApiClient, final Activity activity) {
                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<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, 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(
                activity, 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;
                }
                }
                });
                }


                And use it like,



                mGoogleApiClient = new GoogleApiClient
                .Builder(this)
                .enableAutoManage(this, 34992, this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

                locationChecker(mGoogleApiClient, MyActivity.this);


                Basically your user will get a prompt like this, where they can enable Location in High Accuracy mode without going in the settings
                Turn on GPS






                share|improve this answer













                Based on @user2450263 notes above, here are some snippets,



                /**
                * Prompt user to enable GPS and Location Services
                * @param mGoogleApiClient
                * @param activity
                */
                public static void locationChecker(GoogleApiClient mGoogleApiClient, final Activity activity) {
                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<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, 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(
                activity, 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;
                }
                }
                });
                }


                And use it like,



                mGoogleApiClient = new GoogleApiClient
                .Builder(this)
                .enableAutoManage(this, 34992, this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

                locationChecker(mGoogleApiClient, MyActivity.this);


                Basically your user will get a prompt like this, where they can enable Location in High Accuracy mode without going in the settings
                Turn on GPS







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 29 '15 at 8:03









                PsyGikPsyGik

                1,3721432




                1,3721432























                    0














                    I have solution, tested on Android 6.0.1 Marshmallow, on button event OnClick call the void:



                    private void openGPSSettings() {
                    //Get GPS now state (open or closed)
                    boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER);

                    if (gpsEnabled) {
                    Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE, 0);
                    } else {
                    Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE, 3);
                    }
                    }


                    Android needs to have USB debugging enabled or be rooted. Execute the command below in adb shell (from a root shell on the device or from a computer connected by USB if the device is not rooted):



                    pm grant com.example.application.test android.permission.WRITE_SECURE_SETTINGS


                    Add these permissions in app manifest:



                    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
                    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />





                    share|improve this answer





















                    • 4





                      Not all phones are rooted. This is not a feasible solution.

                      – PsyGik
                      Feb 29 '16 at 8:26






                    • 1





                      Awesome! works for me :)

                      – Rosenpin
                      Jul 22 '16 at 6:29











                    • This does not work with systemless rooted android 6 devices, does it? The app needs to be a system app as well, correct? I'm talking about android 6 and above only...

                      – prom85
                      Jul 25 '16 at 19:45








                    • 1





                      why is it not feasible? Works for me. I need for testing to switch between location modes, so I installed my app on UNrooted Galaxy S6, run "adb shell pm grant <my package> android.permission.WRITE_SECURE_SETTINGS", it does the job (phone pops up dialog for the first time but then, mode consistently gets switched to "High accuracy" mode, ).

                      – waggledans
                      Sep 15 '16 at 18:47











                    • It's not a viable solution for most Android apps though.

                      – patrickandroid
                      Jan 11 at 19:14
















                    0














                    I have solution, tested on Android 6.0.1 Marshmallow, on button event OnClick call the void:



                    private void openGPSSettings() {
                    //Get GPS now state (open or closed)
                    boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER);

                    if (gpsEnabled) {
                    Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE, 0);
                    } else {
                    Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE, 3);
                    }
                    }


                    Android needs to have USB debugging enabled or be rooted. Execute the command below in adb shell (from a root shell on the device or from a computer connected by USB if the device is not rooted):



                    pm grant com.example.application.test android.permission.WRITE_SECURE_SETTINGS


                    Add these permissions in app manifest:



                    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
                    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />





                    share|improve this answer





















                    • 4





                      Not all phones are rooted. This is not a feasible solution.

                      – PsyGik
                      Feb 29 '16 at 8:26






                    • 1





                      Awesome! works for me :)

                      – Rosenpin
                      Jul 22 '16 at 6:29











                    • This does not work with systemless rooted android 6 devices, does it? The app needs to be a system app as well, correct? I'm talking about android 6 and above only...

                      – prom85
                      Jul 25 '16 at 19:45








                    • 1





                      why is it not feasible? Works for me. I need for testing to switch between location modes, so I installed my app on UNrooted Galaxy S6, run "adb shell pm grant <my package> android.permission.WRITE_SECURE_SETTINGS", it does the job (phone pops up dialog for the first time but then, mode consistently gets switched to "High accuracy" mode, ).

                      – waggledans
                      Sep 15 '16 at 18:47











                    • It's not a viable solution for most Android apps though.

                      – patrickandroid
                      Jan 11 at 19:14














                    0












                    0








                    0







                    I have solution, tested on Android 6.0.1 Marshmallow, on button event OnClick call the void:



                    private void openGPSSettings() {
                    //Get GPS now state (open or closed)
                    boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER);

                    if (gpsEnabled) {
                    Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE, 0);
                    } else {
                    Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE, 3);
                    }
                    }


                    Android needs to have USB debugging enabled or be rooted. Execute the command below in adb shell (from a root shell on the device or from a computer connected by USB if the device is not rooted):



                    pm grant com.example.application.test android.permission.WRITE_SECURE_SETTINGS


                    Add these permissions in app manifest:



                    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
                    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />





                    share|improve this answer















                    I have solution, tested on Android 6.0.1 Marshmallow, on button event OnClick call the void:



                    private void openGPSSettings() {
                    //Get GPS now state (open or closed)
                    boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER);

                    if (gpsEnabled) {
                    Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE, 0);
                    } else {
                    Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE, 3);
                    }
                    }


                    Android needs to have USB debugging enabled or be rooted. Execute the command below in adb shell (from a root shell on the device or from a computer connected by USB if the device is not rooted):



                    pm grant com.example.application.test android.permission.WRITE_SECURE_SETTINGS


                    Add these permissions in app manifest:



                    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
                    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 2 '17 at 0:48









                    davidgro

                    30125




                    30125










                    answered Feb 11 '16 at 2:18









                    Henrique José dos Santos NetoHenrique José dos Santos Neto

                    452




                    452








                    • 4





                      Not all phones are rooted. This is not a feasible solution.

                      – PsyGik
                      Feb 29 '16 at 8:26






                    • 1





                      Awesome! works for me :)

                      – Rosenpin
                      Jul 22 '16 at 6:29











                    • This does not work with systemless rooted android 6 devices, does it? The app needs to be a system app as well, correct? I'm talking about android 6 and above only...

                      – prom85
                      Jul 25 '16 at 19:45








                    • 1





                      why is it not feasible? Works for me. I need for testing to switch between location modes, so I installed my app on UNrooted Galaxy S6, run "adb shell pm grant <my package> android.permission.WRITE_SECURE_SETTINGS", it does the job (phone pops up dialog for the first time but then, mode consistently gets switched to "High accuracy" mode, ).

                      – waggledans
                      Sep 15 '16 at 18:47











                    • It's not a viable solution for most Android apps though.

                      – patrickandroid
                      Jan 11 at 19:14














                    • 4





                      Not all phones are rooted. This is not a feasible solution.

                      – PsyGik
                      Feb 29 '16 at 8:26






                    • 1





                      Awesome! works for me :)

                      – Rosenpin
                      Jul 22 '16 at 6:29











                    • This does not work with systemless rooted android 6 devices, does it? The app needs to be a system app as well, correct? I'm talking about android 6 and above only...

                      – prom85
                      Jul 25 '16 at 19:45








                    • 1





                      why is it not feasible? Works for me. I need for testing to switch between location modes, so I installed my app on UNrooted Galaxy S6, run "adb shell pm grant <my package> android.permission.WRITE_SECURE_SETTINGS", it does the job (phone pops up dialog for the first time but then, mode consistently gets switched to "High accuracy" mode, ).

                      – waggledans
                      Sep 15 '16 at 18:47











                    • It's not a viable solution for most Android apps though.

                      – patrickandroid
                      Jan 11 at 19:14








                    4




                    4





                    Not all phones are rooted. This is not a feasible solution.

                    – PsyGik
                    Feb 29 '16 at 8:26





                    Not all phones are rooted. This is not a feasible solution.

                    – PsyGik
                    Feb 29 '16 at 8:26




                    1




                    1





                    Awesome! works for me :)

                    – Rosenpin
                    Jul 22 '16 at 6:29





                    Awesome! works for me :)

                    – Rosenpin
                    Jul 22 '16 at 6:29













                    This does not work with systemless rooted android 6 devices, does it? The app needs to be a system app as well, correct? I'm talking about android 6 and above only...

                    – prom85
                    Jul 25 '16 at 19:45







                    This does not work with systemless rooted android 6 devices, does it? The app needs to be a system app as well, correct? I'm talking about android 6 and above only...

                    – prom85
                    Jul 25 '16 at 19:45






                    1




                    1





                    why is it not feasible? Works for me. I need for testing to switch between location modes, so I installed my app on UNrooted Galaxy S6, run "adb shell pm grant <my package> android.permission.WRITE_SECURE_SETTINGS", it does the job (phone pops up dialog for the first time but then, mode consistently gets switched to "High accuracy" mode, ).

                    – waggledans
                    Sep 15 '16 at 18:47





                    why is it not feasible? Works for me. I need for testing to switch between location modes, so I installed my app on UNrooted Galaxy S6, run "adb shell pm grant <my package> android.permission.WRITE_SECURE_SETTINGS", it does the job (phone pops up dialog for the first time but then, mode consistently gets switched to "High accuracy" mode, ).

                    – waggledans
                    Sep 15 '16 at 18:47













                    It's not a viable solution for most Android apps though.

                    – patrickandroid
                    Jan 11 at 19:14





                    It's not a viable solution for most Android apps though.

                    – patrickandroid
                    Jan 11 at 19:14


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28759454%2fenabling-location-mode-high-accuracy-or-battery-saving-programmatically-withou%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas