簡體   English   中英

Android 4.1.2如何以編程方式關閉GPS

[英]Android 4.1.2 How to turn off GPS programmatically

我試圖通過代碼打開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函數中執行的代碼。 我究竟做錯了什么?

Android指南已更改為4.0以上版本。 對於4.0以上的版本,您無法以編程方式更改GPS。 但是,您可以通過編程方式更改wifi

如何以編程方式關閉GPS

只要有以下情況,GPS無線電將開啟:

  • 它已啟用
  • 一個或多個應用程序試圖通過GPS獲取用戶的位置

歡迎您的應用不再嘗試獲取用戶的位置。 但是,GPS是否打開是您無法控制的。

我嘗試使用此代碼。 但沒有任何事情發生。

這很好,因為你試圖利用的安全漏洞早已得到修復。

應用程序無法以編程方式啟用或禁用GPS,除非在root設備上。 請允許用戶這樣做。

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


    }
}

方法一:

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

如果以上方法不起作用,請嘗試此操作

方法2:

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

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

您無法從4.0及更高版本以編程方式更改GPS。 由於某些隱私原因,Android指南已更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM