Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android device GPS on/off programmatically

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);

Programmatically 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.

like image 800
Shrikant Salunkhe Avatar asked Mar 20 '14 09:03

Shrikant Salunkhe


People also ask

How do I turn on GPS automatically on Android?

Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.

How do you check GPS is on or off in Android programmatically?

Add a TextView and a Button in the layout file. The TextView will display if the GPS is enabled or disabled upon the Button trigger.

How do I open location settings in Android programmatically?

Get current location settingsSettingsClient client = LocationServices. getSettingsClient(this); Task<LocationSettingsResponse> task = client. checkLocationSettings(builder.

How do you ask someone to enable GPS at the launch of an application?

In RESOLUTION_REQUIRED case, we ask user to give permissions, this is where we show the user prompt to enable GPS. Task<LocationSettingsResponse> result = LocationServices. getSettingsClient(getActivity()).


2 Answers

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.

like image 184
Lucifer Avatar answered Sep 24 '22 07:09

Lucifer


This code works on Rooted Phone

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        String[] cmds = {"cd /system/bin" ,"settings put secure location_providers_allowed +gps"};
        try {
            Process p = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(p.getOutputStream());
            for (String tmpCmd : cmds) {
                os.writeBytes(tmpCmd + "\n");
            }
            os.writeBytes("exit\n");
            os.flush();
        }
        catch (IOException e){
            e.printStackTrace();
        }

    }
}
like image 27
DarwinFernandez Avatar answered Sep 23 '22 07:09

DarwinFernandez