【发布时间】:2017-04-05 06:26:24
【问题描述】:
实际上,当他们开始会议时,我在我的应用程序中使用了我的分销商和零售商 GPS 位置,并且 GPS 在他们没有结束会议之前不会关闭,因为我必须获取他们的结束会议位置。但这会导致他们手机的电池出现问题。有什么方法可以在他们的开始会议和结束会议之间自动关闭 GPS?
【问题讨论】:
标签: android gps java-threads android-gps
实际上,当他们开始会议时,我在我的应用程序中使用了我的分销商和零售商 GPS 位置,并且 GPS 在他们没有结束会议之前不会关闭,因为我必须获取他们的结束会议位置。但这会导致他们手机的电池出现问题。有什么方法可以在他们的开始会议和结束会议之间自动关闭 GPS?
【问题讨论】:
标签: android gps java-threads android-gps
我认为现在无法以编程方式打开/关闭 GPS。此功能在早期的 android 版本中可用,但我认为现在谷歌已经停止了它。 相反,您可以将用户引导至 GPS 设置页面,以便用户自己选择是否要公开其位置。 你可以通过调用来做到这一点:
startActivity(上下文,新 意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
现在很多人都对分享他们的位置持怀疑态度,这就是为什么发送 GPS 位置必须征得用户同意的原因。
【讨论】:
你可以用这个
private void turnGPSOn(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){ //if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}}private void turnGPSOff(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")){ //if gps is enabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}}
当您想禁用 gps 或在需要时启用它时调用此函数,您可以从 How can I enable or disable the GPS programmatically on Android? 找到更多信息
【讨论】: