Zahid Hussain
Zahid Hussain

Reputation: 127

How to switch the GPS on and off by using a button click?

i going to make an assistive touch for the android like ios app so i want to add the feature of gps on off there for this. i want to turn on and turn off the gps using a click of image view when i click on it.It goes to the settings and from there you can on and off the location of your phone but i want that not going to setting when i click location is on and click other to off the location.

final ImageView locationOn = view.findViewById(R.id.location);
final ImageView locationOff = view.findViewById(R.id.locationOff); 

locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        GpsStatus = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if(GpsStatus) {
            locationOn.setVisibility(View.INVISIBLE);
            locationOff.setVisibility(View.VISIBLE);
        }
        else{
            locationOn.setVisibility(View.VISIBLE);
            locationOff.setVisibility(View.INVISIBLE);
        }
        locationOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                locationOn.setVisibility(View.INVISIBLE);
                locationOff.setVisibility(View.VISIBLE);
            }
        });
        locationOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                locationOn.setVisibility(View.VISIBLE);
                locationOff.setVisibility(View.INVISIBLE);
            }
        });

when i click on it.It goes to the settings and from there you can on and off the location of your phone but i want that not going to setting when i click location is on and click other to off the location.

Upvotes: 1

Views: 798

Answers (1)

Xay
Xay

Reputation: 248

No it't impossible, and inappropriate. You can't just manage the users phone without his authority.

From Play Store:

"Cerberus automatically enables GPS if it is off when you try to localize your device (only on Android < 2.3.3) and you can protect it from unauthorized uninstalling - more info in the app configuration."

You can do something like this:

startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

By using Intents if API version is not latest

Enable GPS

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);

Disable GPS

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
    intent.putExtra("enabled", false);
    sendBroadcast(intent);

Upvotes: 1

Related Questions