Permissions Journey: ACCESS_FINE_LOCATION

Public on May 22, 2009
Our second journey takes a peek to ACCESS_FINE_LOCATION permission, where you can use GPS to locate directions. The implementation is basically the same with ACCESS_COARSE_LOCATION except on this article we would further expand the code to use the function requestLocationUpdates.

First up you need to enable your GPS
Settings -> Security and Location -> Enable GPS satellites

Then you need to add ACCESS_FINE_LOCATION to your manifest file
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

Main.java
private SampleLocationListener mLocationListener;
private LocationManager m_location_manager;
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  mLocationListener = new SampleLocationListener();
  m_location_manager = (LocationManager) getSystemService(LOCATION_SERVICE);
  Boolean gpsEnabled = m_location_manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  Log.d("onCreateonCreate",String.valueOf(gpsEnabled));
  m_location_manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, mLocationListener);
}
@Override
protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  m_location_manager.removeUpdates(mLocationListener);
}
private class SampleLocationListener implements LocationListener {
    public void onLocationChanged(Location location) {
      // TODO Auto-generated method stub
      if(location != null){
        Log.d("SampleLocationListener",location.toString());
      }else{
        Log.d("SampleLocationListener","No location");
      }
    }
  public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
    Log.d("SampleLocationListener onProviderDisabled",provider);
  }
  public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    Log.d("SampleLocationListener onProviderEnabled",provider);
  }
  public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
    Log.d("SampleLocationListener onStatusChanged",provider);
    }
}


Samsung Moment M900 Android Phone (Sprint)

Explanation
First of you need you to create a listener class that would extends LocationListener.
private class SampleLocationListener implements LocationListener { ..... }

Then create an instance of your created LocationListener
mLocationListener = new SampleLocationListener();

Then get the location service that would create a location manager for your GPS later on
m_location_manager = (LocationManager) getSystemService(LOCATION_SERVICE);

You could then check if the GPS is enable or not
Boolean gpsEnabled = m_location_manager.isProviderEnabled(LocationManager.GPS_PROVIDER);

Then you could continue to create a listener to the GPS service, (every 1 sec, on 1 distance change)
m_location_manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, mLocationListener);

Then on your SampleLocationListener class where the magic happens, here you could know if the network had change from GPS to Network, vise versa or other types of network, if the location has been changed, etc.

** Just one piece of advice, remember to shut off or remove the listener when the users close the app
m_location_manager.removeUpdates(mLocationListener);

Rerefences
http://www.google.com/codesearch/p?hl=en#OOgMjZrCM0A/trunk/OpenStreetMapViewer/src/org/andnav/osm/OpenStreetMapActivity.java&q=requestLocationUpdates
http://www.google.com/codesearch/p?hl=en#2OJgWTG3g9s/trunk/VoteReport/src/com/votereport/android/LocationFinderActivity.java&q=requestLocationUpdates
http://www.google.com/codesearch/p?hl=en#wYGukkcNuKc/trunk/Android20090319/Tutorial/AndroidPedometer/src/chikamune/minoru/pedometer/PedometerActivity.java&q=requestLocationUpdates

Update History
   Jan 17, 2012 - Visual Update




[Advertising]Faster Thinking - Speed up your brain


Faster Thinking Game



sentiment_satisfied Emoticon