android - 如何在android编码中启用GPS

标签 android gps

<分区>

Possible Duplicate:
Enable GPS programatically like Tasker

好的,我知道之前有人问过同样的问题,但所有答案都是“否”。事实证明,启用GPS可以在编码中完成。不信?试用名为“LOOKOUT”的应用程序。我相信有一些解决方法可以实现这一目标。

我已经通读了 lookout AndroidManifest.xml,我发现他们只使用

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

有人知道他们是如何做到这一点的吗?背后的 secret 是什么?只有当应用程序作为系统应用程序安装并且还需要两个权限时,我个人才设法做到这一点

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

我的代码:

Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);

谢谢。

最佳答案

根据 this post在 Lookout 支持论坛上,他们无法以编程方式启用 GPS。

If Lookout could enable the GPS receiver remotely, then use the service, then turn it off – we would, but unfortunately there is no “disable GPS for everything other than Lookout” and if you have disabled the GPS receiver, no application can make use of it. Enabling the GPS receiver requires a user to do it manually.

编辑:看起来在新版本的 Lookout 中,他们现在很可能正在利用安全漏洞来完成此操作。不保证继续工作,但是according to an open Android bug report from April 2010 ,类似于以下的代码将成功地以编程方式启用/禁用 GPS(至少在修复缺陷之前):

private void toggleGPS(boolean enable) {
    String provider = Settings.Secure.getString(getContentResolver(), 
        Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

    if(provider.contains("gps") == enable) {
        return; // the GPS is already in the requested state
    }

    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"));
    context.sendBroadcast(poke);
}

另请参阅问题 "Enable GPS programmatically like Tasker"XDA thread mentioned there有关人们如何使用此方法的更多讨论。

如果您确实使用这种方法,您应该向您的应用程序的潜在用户明确说明,因为这是一个巨大潜在的隐私问题。

关于android - 如何在android编码中启用GPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5481695/

相关文章:

ios - LocationManager不一致

google-maps - 使用 Google 地球/ map 绘制随时间变化的轨迹(电影)

Android 自定义视频捕获文件路径不起作用

android - 构建房间期间出现 StackOverflowError

android - 如何计算当前位置和多个标记之间的距离并返回最近的标记

android - 如何防止其他用户更改 gps 位置和每隔一小段时间调用一次位置更新(比如 3 秒)

android - CWAC 摄像头是否捕获用户的 GPS 位置?

Android 客户端和 PHP 服务器 : common practice in communication

android - ListFragment 中的 fragment

android startActivityForResult 正在终止父 Activity 中的线程