There are so many topics and unanswered questions regarding possibility to turn on/off GPS from inside a custom program so I decided to publish my own research.
As known to everyone any kind of available solution uses either a hack or at very least the root access. I tested quite a few GPS on/off widgets from Google Play and no one succeeded on all of my devices. The proposed more or less universal solution tested for:
Galaxy Tab 2.3.5 stock rooted,
Motorola Atrix 4G CM10 4.1.2,
HTC Evo 3D 4.0.2 custom rooted,
Galaxy Note II 4.1.2 stock rooted, custom rooted and CM10.1 4.2.1,
Galaxy S III 4.1.2 stock rooted.
Here is the short description of steps involved.
Create a small stub APK which does nothing except receiving a custom broadcast with parameters and updates the security strings for GPS accordingly.
Create a main GUI APK which included the above APK as a asset.
The main APK should install the stub APK into /system/app and reboot the Android. Here is the only place actually requiring the root.
The main APK provides additional goods like a widget, a main GUI panel, optional watchdog for Waze, Google Maps and iGo software and optional control of Bluetooth.
The full source code is here: http://rapidshare.com/files/1458124346/GPSToggler-20130222.7z
Please consider the above code as a sample and not like a final product. Bugs are possible, Also remember any tampering with /system/app area may soft-brick your device.
Hard bricks are unlikely but I accept no responsibility if your device bricked.