Pregunta

So, I have an Android application that is asking for the GPS from the LocationManager class. I have an Activity that wants to use that data, and a custom class that implements LocationListener. I have written some custom methods to return the GPS values to my other class.

Currently, I am asking for and releasing the location updates from my Activity class, not my implementation of the LocationListener class... I think this is the right way to go about it, but I just wanted to get any feedback on the life cycle, etc. (the class that implements LocationListener is not an Activity, so I don't think I even could call onPause() etc, ?)

This is the Activity:

package com.jessescott.sonicity;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;


public class PlayActivity extends Activity {

    // GLOBALS
    private static final String TAG = "SoniCity";
    LocationManager locationManager;
    MyLocationListener locationListener;

    TextView latitude, longitude;
    TextView ActualLatitude, ActualLongitude;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play_layout);

        // GPS
        locationListener = new MyLocationListener(this);    


        // TextViews
        latitude  = (TextView) findViewById(R.id.Latitude);
        longitude = (TextView) findViewById(R.id.Longitude);

        ActualLatitude  = (TextView) findViewById(R.id.ActualLat);
        ActualLatitude.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v(TAG, "Asking For New Latitude");
                ActualLatitude.setText(locationListener.getCurrentLatitude());  
            }
        });

        ActualLongitude = (TextView) findViewById(R.id.ActualLon);
        ActualLongitude.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v(TAG, "Asking For New Latitude");
                ActualLongitude.setText(locationListener.getCurrentLongitude());    
            }
        });

    }

    @Override
    protected void onPause() {
        super.onPause();
        // Stop GPS
        locationManager.removeUpdates(locationListener);
        locationManager = null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 5, locationListener);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

} /*  */

... and this is the LocationListener implementation :

 package com.jessescott.sonicity;

    import android.content.Context;
    import android.location.Location;
    import android.location.LocationListener;
    import android.os.Bundle;
    import android.util.Log;

    class MyLocationListener implements LocationListener {

      private static final String TAG = "SoniCity";
      float currentLatitude     = 0;
      float currentLongitude    = 0;

      public MyLocationListener(Context context) {
            super();
      }

      // Define all LocationListener methods
      public void onLocationChanged(Location location) {
        currentLatitude  = (float)location.getLatitude();
        currentLongitude = (float)location.getLongitude();


      }

      public void onProviderDisabled (String provider) { 
        Log.v(TAG, "Provider is " + provider);
      }

      public void onProviderEnabled (String provider) { 
        Log.v(TAG, "Provider is " + provider);
      }

      public void onStatusChanged (String provider, int status, Bundle extras) {
        Log.v(TAG, "Status is " + status);
      }

      // Custom Methods

      public String getCurrentLatitude() {
          String lat = Float.toString(currentLatitude);
          return lat;
      }

      public String getCurrentLongitude() {
          String lon = Float.toString(currentLongitude);
          return lon;
      }

    } /* */

I guess since I've moved it to a separate class (they used to be the same one), I just want to make sure I'm calling th request/remove in the right place.

¿Fue útil?

Solución

I haven't looked at your code in detail, but from what I've seen it looks fine to me. onResume() is the right place to register your listener, and onPause() is the right place to unregister it, so that e.g. battery life is optimised.

A couple of things to consider going forward:

  • For a robust app, make sure you pay attention to the information that you get from onStatusChanged, onProviderEnabled and onProviderDisabled. I've seen various questions on stackoverflow.com to which the answer was to pay attention to status changes etc.
  • Also for a better estimate of location, you might consider using the Location.accuracy value, because the accuracy of GPS location estimates can change. One minute they might be very accurate, but then the visible satellites change and the accuracy can suddenly be much lower. In my apps I use a simple Kalman filter for this, and I posted the code for it in my answer to the question Smooth GPS data.
  • Licenciado bajo: CC-BY-SA con atribución
    No afiliado a StackOverflow
    scroll top