如何在 Android 上以编程方式启用或禁用 GPS? [英] How can I enable or disable the GPS programmatically on Android?

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

问题描述

我知道关于在 android 上以编程方式打开/关闭 GPS 的问题 去过 讨论 许多,以及答案总是一样的:

<块引用>

出于安全/隐私原因,您不能这样做,您必须转发到位置首选项屏幕并让用户启用/禁用它."

我知道,但是我最近从市场上购买了 Tasker完成它,您可以设置规则以在进入预先确定的应用程序时自动启用 GPS,并在退出时禁用它(参见 此处 获取有关如何操作的教程,它可以正常工作!)并且此应用程序无法使用固件进行签名签名密钥,因为它适用于许多 android 版本和不同的设备,您甚至不需要 root.

我想在我的应用程序中执行此操作.当然,我不想破坏用户的隐私,所以我会首先询问用户是否要使用典型的记住我的决定"复选框自动打开它,如果他回答是,则启用它.

有人对塔斯克如何实现这一目标有任何想法或线索吗?

解决方案

GPS 可以通过 利用电源管理器小部件中的错误.请参阅此 xda thread 进行讨论.

这是我使用的一些示例代码

private void turnGPSOn(){String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);if(!provider.contains("gps")){//如果gps被禁用最终意图戳=新意图();poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");poke.addCategory(Intent.CATEGORY_ALTERNATIVE);poke.setData(Uri.parse("3"));发送广播(戳);}}私人无效turnGPSOff(){String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);if(provider.contains("gps")){//如果gps开启最终意图戳=新意图();poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");poke.addCategory(Intent.CATEGORY_ALTERNATIVE);poke.setData(Uri.parse("3"));发送广播(戳);}}

使用以下内容测试电源控制小部件的现有版本是否允许您切换 gps.

private boolean canToggleGPS() {PackageManager pacman = getPackageManager();PackageInfo pacInfo = null;尝试 {pacInfo = pacman.getPackageInfo("com.android.settings", PackageManager.GET_RECEIVERS);} catch (NameNotFoundException e) {返回假;//找不到包}if(pacInfo != null){for(ActivityInfo actInfo : pacInfo.receivers){//测试recevier是否导出.如果是这样,我们可以切换 GPS.if(actInfo.name.equals("com.android.settings.widget.SettingsAppWidgetProvider") && actInfo.exported){返回真;}}}返回假;//默认}

I know that the question about turning on/off GPS programatically on android has been discussed many times, and the answer is always the same:

"You can't for security/privacy reasons, you have to forward to location preferences screen and let the user enable/disable it."

I understand that, however I recently bought Tasker from the market and, among many other things that you can accomplish with it, you can set rules to auto-enable GPS on entering pre-determined applications and disable it on exit (see here for the tutorial on how to do it, and it just works!) and this app can't be signed with the firmware signing key as it works on many android versions and different devices and you don't even need to be rooted.

I would like to do this in my app. Of course, I don't want to blow up the users privacy, so I would first ask the user if he wants to turn it on automatically with the typical "remember my decision" checkbox and if he answers yes, enable it.

Does anybody have any idea or clue on how Tasker achieves this?

解决方案

the GPS can be toggled by exploiting a bug in the power manager widget. see this xda thread for discussion.

here's some example code i use

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

use the following to test if the existing version of the power control widget is one which will allow you to toggle the gps.

private boolean canToggleGPS() {
    PackageManager pacman = getPackageManager();
    PackageInfo pacInfo = null;

    try {
        pacInfo = pacman.getPackageInfo("com.android.settings", PackageManager.GET_RECEIVERS);
    } catch (NameNotFoundException e) {
        return false; //package not found
    }

    if(pacInfo != null){
        for(ActivityInfo actInfo : pacInfo.receivers){
            //test if recevier is exported. if so, we can toggle GPS.
            if(actInfo.name.equals("com.android.settings.widget.SettingsAppWidgetProvider") && actInfo.exported){
                return true;
            }
        }
    }

    return false; //default
}

这篇关于如何在 Android 上以编程方式启用或禁用 GPS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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