Apr 12, 2011

Detect GPS ON/OFF status using android.provider.Settings.Secure

android.provider.Settings.Secure is secure system settings, containing system preferences that applications can read but are not allowed to write. These are for preferences that the user must explicitly modify through the system UI or specialized APIs for those values, not modified directly by applications.

Start from API level 8, Android 2.2, Settings.Secure.isLocationProviderEnabled() is added as helper method for determining if a location provider is enabled.

Detect GPS ON/OFF status using android.provider.Settings.Secure

package com.AndroidGPS;

import android.app.Activity;
import android.content.ContentResolver;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.TextView;

public class AndroidGPS extends Activity {

TextView textGpsStatus;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
textGpsStatus = (TextView)findViewById(R.id.gpsstatus);


protected void onResume() {
// TODO Auto-generated method stub

private void displayGpsStatus(){
ContentResolver contentResolver = getBaseContext().getContentResolver();
boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
textGpsStatus.setText("GPS: ON");
textGpsStatus.setText("GPS: OFF");

Another method setLocationProviderEnabled() is also provided to enable or disable a single location provider. But up to this writing, I can't make it work! Also, according to Settings.Secure document, secure system settings can be read by applications but cannot be write. I don't know what is the purpose of the method setLocationProviderEnabled()!

- How to check if GPS is currently enabled or disabled
- Start Location setting if GPS disabled

1 comment:

  1. Thanks for complete example.. its working like a charm... :)


Infolinks In Text Ads