I am using following code for GPS on/off.
//Enable GPS
Intent intent = new Intent(\"android.location.GPS_ENABLED_CHANGE\");
intent.putExtra(\"enabled\", true
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.