How to disable location service programmatically?

被刻印的时光 ゝ 提交于 2020-01-03 02:05:09

问题


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!


回答1:


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.




回答2:


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:


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.




回答4:


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'


来源:https://stackoverflow.com/questions/13481653/how-to-disable-location-service-programmatically

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