How to disable location service programmatically?

心不动则不痛 提交于 2019-12-06 16:32:18

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.

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.

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.

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'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!