0

I have an offline-map App which uses GPS-Tracker. My primary goal is to sense changes in latitude and longitude and update a TextView.
My GPS-Tracker class is implemented as follows:

public class GPSTracker extends Service implements LocationListener {

    ...

    public GPSTracker(Context context) {
        ...
        getLocation();
    }

    public Location getLocation() {
        // returns Location
    }

    @Override
    public void onLocationChanged(Location location) {
        // Method 1: update UI elements (textview) directly 
        MainActivity.tvGPS.setText("lat: " + Double.toString(location.getLatitude()) + "lon: " + Double.toString(location.getLongitude());

        // Method 2: call a method from MainActivity which updates UI
        MainActivity.setNewPosition(location.getLatitude() , location.getLatitude());

        // Method 3: sense changes in Latitude and Longitude directly in MainActivity and update UI
    }
}

Question:
How can I directly sense changes of Latitude and Longitude (in MainActivity) and modify UI.
what I so far tested:

  • UI update from GPSTracker/onLocationChanged (as Method 1 and 2 in the code): It works but it is not from MainActivity
  • using LocalBroacastManager as mentioned here but it demands constantly checking of location to detect changes
  • using Observer Pattern as mentioned here: changes are sensible only in Observer Class not in MainActivity
  • Community
    • 1
    • 1
    Behy
    • 483
    • 7
    • 23

    2 Answers2

    0

    You could pass the MainActivity as a parameter in the constructor of GPSTracker.

    private TextView tvGPS;
    
    public GPSTracker(Activity a) {
          ...
          tvGPS = (TextView)a.findViewById(R.id.tvGPS); // whatever ID
    }
    

    Then you could change the text directly in onLocationChanged.

    @Override
    public void onLocationChanged(Location location) {
        tvGPS.setText("lat: " + Double.toString(location.getLatitude()) + "lon: " + Double.toString(location.getLongitude());
    }
    
    user0815
    • 213
    • 2
    • 11
    • Is it more or less the same as defining a public TextView which is accessible from other classes? this concept would increase the complexity, especially when UI-update from several classes is demanded. – Behy Apr 22 '16 at 12:32
    0

    Please, don't pass Activity instance into tracker with static methods usage! This is very bad practice!

    You can use event bus. http://square.github.io/otto/

    Create a bus provider :

    public class BusProvider {
        private static final Bus BUS = new Bus(ThreadEnforcer.ANY);
    
        public static Bus getInstance() {
            return BUS;
        }
    
        private BusProvider() {
            // No instances.
        }
    }
    

    Create a bus event:

         public class LocationChangedEvent{
    
        private Location location;
    
          private Location location;
    
            public LocationChangedEvent(Location location) {
                this.location = location;
            }
    
            public Location getLocation() {
                return location;
            }
    
            public void setLocation(Location location) {
                this.location = location;
            }
     }
    
    
    
    public class GPSTracker extends Service implements LocationListener {
    
            ...
    
        public GPSTracker(Context context) {
            ...
            getLocation(); 
        }
    
        public Location getLocation() {
            // returns Location
        }
    
        @Override
        public void onLocationChanged(Location location) {
           new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        BusProvider.getInstance().post(new LocationChangedEvent(location));
                    }
                });
        }
    }   
    

    inside your activity:

     @Subscribe
        public void onLocationUpdated(LocationChangedEvent locationChangedEvent){
            //TODO update ui as you need
    
        }
    

    Don't forget to register/ unregister bus

    Bogdan Ustyak
    • 5,639
    • 2
    • 21
    • 16