How to disable location service programmatically?

2019-06-08 14:28发布

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!

4条回答
SAY GOODBYE
2楼-- · 2019-06-08 14:54

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.

查看更多
戒情不戒烟
3楼-- · 2019-06-08 14:55

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'
查看更多
何必那么认真
4楼-- · 2019-06-08 14:57

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.

查看更多
放荡不羁爱自由
5楼-- · 2019-06-08 15:03

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.

查看更多
登录 后发表回答