Automatically turn on GPS

后端 未结 1 1088
难免孤独
难免孤独 2021-01-14 23:47

I googled about it and there were solutions regarding the exploitation of Power Manager Bug to resolve this issue. But as the bug has now been solved. So how can now I solve

1条回答
  •  心在旅途
    2021-01-15 00:39

    Use this function :

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

    Make a call for this function from the onCreate() method of your activity.

    OR :

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

    Thanks.

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