简体   繁体   English

以编程方式启用GPS(无需导航到位置设置)

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

如何通过点击弹出屏幕上的“开启”选项(无需导航到位置设置),以编程方式启用GPS,因为它确实发生在官方Google地图应用中?

The Google Maps app is using what is now available to us as SettingsApi the Play Services SDK. 谷歌地图应用程序正在使用我们现在可用的SettingsApi作为Play服务SDK。 You can use SettingsApi to inquire as to whether your desired LocationRequest can be fulfilled with whatever location providers are enabled. 您可以使用SettingsApi查询是否可以使用启用的任何位置提供程序来实现所需的LocationRequest 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. 如果无法完成,并且Play服务认为用户可以更改此设置,则可以要求显示弹出地图显示的对话框。

Using SettingsApi is not especially simple. 使用SettingsApi并不是特别简单。 Here a sample app for that. 是一个示例应用程序 Using ACTION_LOCATION_SOURCE_SETTINGS , as suggested in Laurenswuyts' answer, is much simpler to implement. 按照Laurenswuyts的回答中的建议,使用ACTION_LOCATION_SOURCE_SETTINGS实现起来简单得多。

That's because they are using the Settings API in the play services as described in Commonsware's answer, which is a bit difficult. 那是因为他们正在使用Commonsware的答案中描述的播放服务中的Settings API ,这有点困难。 You are better off with the "old" method: 你最好使用“旧”方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM