我如何可以启用或禁用编程在Android上的全球定位系统? [英] How can I enable or disable the GPS programmatically on Android?

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

问题描述

我知道,大约在Android /关闭GPS打开编程的问题有 <一个href="http://stackoverflow.com/questions/2988314/enable-gps-without-forwarding-the-user-to-settings">discussed <一href="http://stackoverflow.com/questions/4689634/android-turn-on-gps-like-gps-on-off-widget">many 和答案总是一样的:

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:

你不能出于安全/隐私的原因,你要转发到所在地preferences屏幕,让用户启用/禁用它。

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

我明白,但是我最近买了塔斯克从市场,其中,你可以用它来完成很多其他的事情,你在进入pre-确定应用程序可以设置规则来自动启用GPS和退出禁用它(请参阅<一href="http://lifehacker.com/5599116/how-to-turn-your-android-phone-into-a-fully+automated-superphone">here关于如何做到这一点,它只是工作!)并且这个程序不能与固件签名密钥签名的,因为它适用于众多Android版本和不同的设备,你甚至都不需要植根教程。

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?

推荐答案

的GPS可以通过的利用一个错误的电源管理部件。看到这 XDA线程的讨论。

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

下面是一些例子code我使用

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

使用以下方法来测试,如果功率控制部件的现有版本是,这将允许你切换GPS。

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上的全球定位系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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