Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn on the GPS on Android

Tags:

android

gps

I am developing an android app which needs to activate the GPS.

I read a lot of topics in a lot of forums and the answer I've found is:

it's not possible

But... the "Cerberus" APP turns my GPS on... so... it's possible!

Can anyone help me with this?

like image 435
Elvis Oliveira Avatar asked Mar 29 '12 15:03

Elvis Oliveira


People also ask

What does the GPS icon look like?

The gps symbol is the one that looks like a radar antenna with waves coming out of it. that icon means you have locations turned on - you are allowing apps to use the GPS if they need it. it doesn't mean the GPS is actively working (there is another icon for that).

How do I turn on my GPS on my Samsung?

Launch the Settings app, and then select Location. Step 2. If the switch at the top is Off, turn it On. Alternatively, you can swipe down on the screen to bring up the Quick panel, and then tap the Location icon to enable or disable location services.

What is GPS on my phone?

The location services or GPS (global positioning system) on your phone is very useful for a range of things on your phone – namely finding out where you are on a map and navigation.


2 Answers

No, it's impossible, and inappropriate. You can't just manage the user's phone without their authority. The user must interact to enable GPS.

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));
like image 63
goodm Avatar answered Oct 04 '22 22:10

goodm


I think we have more better version to enable the location without opening the settings just like google map works.

It will looks like this -

enter image description here

Add Dependency in gradle - compile 'com.google.android.gms:play-services-location:10.0.1'

public class MapActivity extends AppCompatActivity {

    protected static final String TAG = "LocationOnOff";

 
    private GoogleApiClient googleApiClient;
    final static int REQUEST_LOCATION = 199;

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

        this.setFinishOnTouchOutside(true);

        // Todo Location Already on  ... start
        final LocationManager manager = (LocationManager) MapActivity.this.getSystemService(Context.LOCATION_SERVICE);
        if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && hasGPSDevice(MapActivity.this)) {
            Toast.makeText(MapActivity.this,"Gps already enabled",Toast.LENGTH_SHORT).show();
        }
        // Todo Location Already on  ... end

        if(!hasGPSDevice(MapActivity.this)){
            Toast.makeText(MapActivity.this,"Gps not Supported",Toast.LENGTH_SHORT).show();
        }

        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && hasGPSDevice(MapActivity.this)) {
            Log.e("TAG","Gps already enabled");
            Toast.makeText(MapActivity.this,"Gps not enabled",Toast.LENGTH_SHORT).show();
            enableLoc();
        }else{
            Log.e("TAG","Gps already enabled");
            Toast.makeText(MapActivity.this,"Gps already enabled",Toast.LENGTH_SHORT).show();
        }
    }


    private boolean hasGPSDevice(Context context) {
        final LocationManager mgr = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        if (mgr == null)
            return false;
        final List<String> providers = mgr.getAllProviders();
        if (providers == null)
            return false;
        return providers.contains(LocationManager.GPS_PROVIDER);
    }

    private void enableLoc() {

        if (googleApiClient == null) {
            googleApiClient = new GoogleApiClient.Builder(MapActivity.this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                        @Override
                        public void onConnected(Bundle bundle) {

                        }

                        @Override
                        public void onConnectionSuspended(int i) {
                            googleApiClient.connect();
                        }
                    })
                    .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                        @Override
                        public void onConnectionFailed(ConnectionResult connectionResult) {

                            Log.d("Location error","Location error " + connectionResult.getErrorCode());
                        }
                    }).build();
            googleApiClient.connect();
         }

         LocationRequest locationRequest = LocationRequest.create();
            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
         locationRequest.setInterval(30 * 1000);
         locationRequest.setFastestInterval(5 * 1000);
         LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                    .addLocationRequest(locationRequest);

         builder.setAlwaysShow(true);

         PendingResult<LocationSettingsResult> result =
                    LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
         result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult result) {
                final Status status = result.getStatus();
                switch (status.getStatusCode()) {
                  case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                       try {
                           // Show the dialog by calling startResolutionForResult(),
                           // and check the result in onActivityResult().
                                status.startResolutionForResult(MapActivity.this, REQUEST_LOCATION);
                       } catch (IntentSender.SendIntentException e) {
                           // Ignore the error.
                       }
                       break;
                 }
             }
         });
     }

}
like image 26
Anirban Avatar answered Oct 04 '22 22:10

Anirban