joe1806772
joe1806772

Reputation: 596

How to disable location service programmatically?

Is there any way to disable google location service programmatically? I want to write an app to turn it off with a widget.

Thanks for your help!

Upvotes: 1

Views: 8705

Answers (4)

Mapsy
Mapsy

Reputation: 4262

If you're using the UiAutomater, you can turn off Location Services by simulating a click on the Location option in the Quick Settings. The filter logic may be platform dependent. You may analyse the view hierarchy using the uiautomatorviewer.

i.e.

UiDevice.getInstance(getInstrumentation()).openQuickSettings();
UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().descriptionContains("Location").className(ViewGroup.class)).click();

build.gradle

androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'

Upvotes: 1

Ed Holloway-George
Ed Holloway-George

Reputation: 5149

Short answer is, No. But if you really need to turn it off, you can prompt the user to do it:

LocationManager loc = (LocationManager) this.getSystemService( Context.LOCATION_SERVICE );
        if( loc.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ) )
        {
            Toast.makeText( this, "Please turn off GPS", Toast.LENGTH_LONG).show();
            Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS );
            startActivity(myIntent);

        }
        else
        {

        // Do nothing   
        }

This might not help your situation but is the only way I know of changing the settings.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006869

Is there any way to disable google location service programmatically?

No, sorry, you cannot disable GPS, etc. programmatically, except via an app installed on the system partition or signed by the firmware's signing key. IOW, you need a rooted phone.

Upvotes: 5

Robert Estivill
Robert Estivill

Reputation: 12477

As far as i know, the LocationManager is a system service that can not be turned off. Though, you can disable all the location providers that the service uses, such as Wireless location, GPS, etc.

Upvotes: 3

Related Questions