I am using following code for GPS on/off.
//Enable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
context.sendBroadcast(intent);
//Disable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
context.sendBroadcast(intent);
Programatically I need to on/off GPS on android device. I am using above code for that. But It doesn't work on all the devices. Please help me. Thanks In advance.
From my personal experience, I am answering this,
The hack code you shown in the your question has been stopped working from Android version 4.4. Your will fire this Exception starting from Kitkat version
java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE
The First answer's code will not work any more, it only display animated GPS icon in notification bar.
For The security purpose Google developer has block above both methods which were previously working fine.
Hence conclusion is that You can not programmatically start GPS On or Off.
This code works on Rooted Phone
Try this code:
UPDATE:
Now we can on/off GPS from our app using the default enable location dialog like Google maps.
The documentation can be found here:
Android Location Dialog
Sample code:
i will suggest you use following code for Android kitkat 4.4.x
/////////////////////////
Use this for turning GPS ON and reverse the condition to put GPS OFF