Android device GPS on/off programmatically

后端 未结 5 1152
深忆病人
深忆病人 2020-11-27 07:40

I am using following code for GPS on/off.

//Enable GPS
Intent intent = new Intent(\"android.location.GPS_ENABLED_CHANGE\");
intent.putExtra(\"enabled\", true         


        
5条回答
  •  广开言路
    2020-11-27 08:27

    Use this for turning GPS ON and reverse the condition to put GPS OFF

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

提交回复
热议问题