Android 4.1.2 How to turn off GPS programmatically

前端 未结 5 601
有刺的猬
有刺的猬 2020-12-05 22:20

I am trying to switch on GPS by code. The following is the code I\'ve used.

String provider = Settings.Secure.getString(context.getContentResolver(),Settings         


        
相关标签:
5条回答
  • 2020-12-05 22:40
    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:

    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); intent.putExtra("enabled", false);
    sendBroadcast(intent);
    
    0 讨论(0)
  • 2020-12-05 22:41

    Code to turn on GPS on 4.0 and above:

    @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);
        }
    }
    
    0 讨论(0)
  • 2020-12-05 22:41

    You cannot change GPS off on programmatically from versions 4.0 & above. Android guidelines have been changed due to some privacy reasons .

    0 讨论(0)
  • 2020-12-05 22:45

    Android Guidelines have changed above version 4.0. You cannot change GPS off on programmatically for versions above 4.0. However, you can change wifi off on programmatically

    0 讨论(0)
  • 2020-12-05 22:48

    How to turn off GPS programmatically

    The GPS radio will be on so long as:

    • It is enabled
    • One or more apps are trying to obtain the user's location via 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.

    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. Please allow the user to do that.

    0 讨论(0)
提交回复
热议问题