简体   繁体   English

Android 4.1.2如何以编程方式关闭GPS

[英]Android 4.1.2 How to turn off GPS programmatically

I am trying to switch on GPS by code. 我试图通过代码打开GPS。 The following is the code I've used. 以下是我用过的代码。

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

The code executing in OnReceive function. 在OnReceive函数中执行的代码。 What am I doing wrong? 我究竟做错了什么?

Android Guidelines have changed above version 4.0. Android指南已更改为4.0以上版本。 You cannot change GPS off on programmatically for versions above 4.0. 对于4.0以上的版本,您无法以编程方式更改GPS。 However, you can change wifi off on programmatically 但是,您可以通过编程方式更改wifi

How to turn off GPS programmatically 如何以编程方式关闭GPS

The GPS radio will be on so long as: 只要有以下情况,GPS无线电将开启:

  • It is enabled 它已启用
  • One or more apps are trying to obtain the user's location via GPS 一个或多个应用程序试图通过GPS获取用户的位置

You are welcome to have your app no longer try to obtain the user's location. 欢迎您的应用不再尝试获取用户的位置。 However, whether the GPS is on is outside of your control. 但是,GPS是否打开是您无法控制的。

I try to use this code. 我尝试使用此代码。 But nothing happening. 但没有任何事情发生。

That is good, because the security flaw that you are trying to exploit has long been fixed. 这很好,因为你试图利用的安全漏洞早已得到修复。

Apps cannot enable or disable GPS programmatically, except perhaps on rooted devices. 应用程序无法以编程方式启用或禁用GPS,除非在root设备上。 Please allow the user to do that. 请允许用户这样做。

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


    }
}

Method1: 方法一:

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

try this if above method doesn't work 如果以上方法不起作用,请尝试此操作

Method 2: 方法2:

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); intent.putExtra("enabled", false);
sendBroadcast(intent);

Code to turn on GPS on 4.0 and above: 在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);
    }
}

You cannot change GPS off on programmatically from versions 4.0 & above. 您无法从4.0及更高版本以编程方式更改GPS。 Android guidelines have been changed due to some privacy reasons . 由于某些隐私原因,Android指南已更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM