通过Android以编程方式启用GPS(无需导航至位置设置)
如何通过编程方式启用GPS(就像在正式的Google Maps应用中一样),只需点击弹出屏幕上的打开"选项即可(无需导航至位置设置)?
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)?
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.
使用SettingsApi
并不是特别简单.在此一个示例应用.正如Laurenswuyts的答案所建议的那样,使用ACTION_LOCATION_SOURCE_SETTINGS
更容易实现.
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.