Android – Automatically turn on GPS

androidgps

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 this issue of starting the GPS when the app starts?
Please help.

Best Answer

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.