Sunday, January 1, 2012

Check Current GPS Status ON/OFF

If you want to check your Android Mobile Device's GPS Status off or on programmatically
this tutorial is for you :)

We will create very simple app to do this work

okay! so let's try this small app

 -------------------------------------------
App Name: GpsStatusCheck
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: GpsStatusCheckActivity
-------------------------------------------

GpsStatusCheckActivity.java


package com.rdc;

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 GpsStatusCheckActivity extends Activity {
 TextView textGpsStatus;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  textGpsStatus = (TextView) findViewById(R.id.gpsstatus);
 }

 @Override
 protected void onResume() {
  super.onResume();
  displayGpsStatus();
 }

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


main.xml





AndroidManifest.xml

 

 
  
   
    
    
   
  

 


You can download the complete source code zip file here : GpsStatusCheck

cheers!!

I'd love to hear your thoughts!

No comments:

Post a Comment