安卓4.1.2如何通过程序关闭GPS

5
我正在尝试通过代码打开GPS。以下是我使用的代码。
String provider = Settings.Secure.getString(context.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")); 
    context.sendBroadcast(poke);

OnReceive函数中的代码正在执行。我做错了什么?

1
嘿,GIKO,请看看我的答案。你可以使用代码打开和关闭GPS! - Vinoth
5个回答

15

Android指南在4.0以上版本中发生了变化。对于4.0以上版本,您不能通过程序将GPS关闭。但是,您可以通过程序将wifi关闭。


我也在做GPS应用,但是我的脑袋也有点糊涂。我在某个地方读到过,4.0以上的系统不能打开GPS。 - Rahul Gupta
1
你有 developer.android.com 的链接吗?他们确切地在哪里声明了这个。@RahulGupta - Premal Khetani

8
如何编程关闭GPS:
只要以下两个条件成立,GPS无线电就会一直保持开启:
- GPS功能被启用。 - 一个或多个应用程序通过GPS获取用户位置。
您可以让应用程序不再尝试获得用户的位置。但是,GPS是否开启是超出您的控制范围的。
我尝试使用此代码。但没有任何反应。
这很好,因为您正在尝试利用的安全漏洞早已被修复。
应用程序无法编程启用或禁用GPS,除非在经过root处理的设备上可能可以。请允许用户进行操作。

有没有办法实现这个?是否可以按住Ctrl键进入Android内核中的特定函数,并创建一个类似的函数来运行重复函数以绕过原始函数。 - Vinoth
@commonsWare 我注意到谷歌健身应用正在打开我的 GPS。我在使用运行 Android Lollipop 的 Moto G 手机。 - Ankit
1
@Ankit:通过Play Services SDK中的SettingsApi,您可以弹出对话框询问用户是否启用位置。此外,谷歌应用程序往往具有特权,特别是预安装的应用程序,它们可以执行普通SDK应用程序无法执行的操作。 - CommonsWare

3
public void turnGPSOn()
{
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
     intent.putExtra("enabled", true);
     this.ctx.sendBroadcast(intent);

    String provider = Settings.Secure.getString(ctx.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")); 
        this.ctx.sendBroadcast(poke);


    }
}

方法1:

 // automatic turn off the gps
    public void turnGPSOff()
    {
        String provider = Settings.Secure.getString(ctx.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")); 
            this.ctx.sendBroadcast(poke);
        }
    }

如果上述方法无效,请尝试以下方法: 方法二:
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); intent.putExtra("enabled", false);
sendBroadcast(intent);

2

在4.0及以上版本中打开GPS的代码:

@SuppressWarnings("deprecation")
public static void turnGPSOn(Context context)
{
    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
    intent.putExtra("enabled", true);
    context.sendBroadcast(intent);

    String provider = Settings.Secure.getString(context.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")); 
        context.sendBroadcast(poke);
    }
}

@SuppressWarnings("deprecation")
public static void turnGPSOff(Context context)
{
    String provider = Settings.Secure.getString(context.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")); 
        context.sendBroadcast(poke);
    }
}

1

从4.0版本及以上,您无法在程序中自动关闭GPS。由于某些隐私原因,Android指南已经更改。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接