Enable GPS programmatically Android (without navigating to the location settings)

僤鯓⒐⒋嵵緔 提交于 2020-01-27 06:14:25

问题


How to enable GPS programmatically, as it does happen in the official Google Maps app by just clicking on the 'turn on' option on the pop up screen (without navigating to location settings)?


回答1:


The Google Maps app is using what is now available to us as SettingsApi the Play Services SDK. You can use SettingsApi to inquire as to whether your desired LocationRequest can be fulfilled with whatever location providers are enabled. If it cannot be fulfilled, and Play Services thinks that the user can change this, you can ask for the dialog that you see Maps display pop up.

Using SettingsApi is not especially simple. Here a sample app for that. Using ACTION_LOCATION_SOURCE_SETTINGS, as suggested in Laurenswuyts' answer, is much simpler to implement.




回答2:


That's because they are using the Settings API in the play services as described in Commonsware's answer, which is a bit difficult. You are better off with the "old" method:

Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);


来源:https://stackoverflow.com/questions/29697725/enable-gps-programmatically-android-without-navigating-to-the-location-settings

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