如何在android编码中启用GPS [英] How to enable GPS in android coding

查看:15
本文介绍了如何在android编码中启用GPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
像Tasker一样以编程方式启用GPS

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

Ok, I know there is same question asked before but all answer is "NO". It is proven that enable GPS can be done in coding. Don't believe? try out an application called "LOOKOUT". I believe there is some workaround in order to achieve this.

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

I had read through lookout AndroidManifest.xml, what i found out they only use

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

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

Anyone have idea how they managed to do this? what is the secret behind? I personally managed to do this only if the application is install as system app and also require two permission

<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);

谢谢.

推荐答案

根据这个帖子 在 Lookout 支持论坛上,他们无法以编程方式启用 GPS.

According to this post on the Lookout support forums, they are unable to programmatically enable GPS.

如果 Lookout 可以远程启用 GPS 接收器,然后使用该服务,然后将其关闭 - 我们会这样做,但不幸的是,没有为 Lookout 以外的所有设备禁用 GPS",如果您禁用了 GPS 接收器,则没有应用程序可以利用它.启用 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 中,他们现在最有可能利用安全漏洞来实现这一点.不能保证继续工作,但 根据一个开放的 Android 错误2010 年 4 月的报告,类似于以下的代码将成功以编程方式启用/禁用 GPS(至少在修复缺陷之前):

It looks like in the new version of Lookout they are most likely now exploiting a security flaw to accomplish this. It is not guaranteed to continue to work, but according to an open Android bug report from April 2010, code similar to the following will successfully programmatically enable/disable GPS (at least until the flaw is fixed):

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 线程 以进一步讨论人们如何使用这种方法.

Also, see the question "Enable GPS programmatically like Tasker" and the XDA thread mentioned there for more discussion on how people have used this method.

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

If you do use this method, you should make it clear to potential users of your application, since this is a HUGE potential privacy concern.

这篇关于如何在android编码中启用GPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆