bhanu kaushik
bhanu kaushik

Reputation: 391

GPS automatic enabling problems

I am working on the application which automatically directs user to the GPS settings when the applications is launched.

  Problems:

1.Do i have to declare Write Secure Settings in the manifest for this.(if yes, then its showing "permisson is only granted to system apps) what to do in this case, if the answer is yes

2.What my application is doing now, its taking user directly to the GPS settings, what i want is, that a dialog box will appear(only if GPS is off) having "GPS settings" and "cancel" button.And if user select "cancel", i want to display another dialog box , showing "App will not work with GPS OFF".

What to do with these problems, please suggest.

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

I am using this code for GPS settings.

Upvotes: 0

Views: 124

Answers (2)

M D
M D

Reputation: 47817

Not any Extra permission required for Write Secure Settings. You just need to check that your GPS is enabled or disabled using below:

// flag for GPS status
 boolean isGPSEnabled = false;

 // Declaring a Location Manager
protected LocationManager locationManager;

locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

if (!isGPSEnabled ) {
    // no GPS provider is enabled
    //Create your Dialog over here
        } else{
    // GPS provider is enabled
 }

Upvotes: 2

Asha Soman
Asha Soman

Reputation: 1856

You can check the state of GPS like following

LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
  boolean state = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}

Upvotes: 0

Related Questions