3

I have a GPSTracker class which extends Service implements LocationListener. And GPSTracker also override onLocationChanged method.

In my MainActivity, I created an instance of GPSTracker and use custom methods I declared in GPSTracker class to get lat/lon.

How can I make my MainActivity to be notified when GPSTracker's onLocationChanged is triggered?

GPSTracker Class

public class GPSTracker extends Service implements LocationListener {
    ...

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude

    ...

    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
    }

    public Location getLocation() {
        ...
        return location;
    }


    public void stopUsingGPS(){
        ...
    }


    public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }
        return latitude;
    }

    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }
        return longitude;
    }

    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    public void showSettingsAlert(){
        ...
        // Showing Alert Message
        alertDialog.show();
    }

    @Override
    public void onLocationChanged(Location location) {
        // do some stuff
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Boy
  • 405
  • 2
  • 9
  • 21

3 Answers3

3

I see two way to notify your MainActivity :

First, you can create a listener that is implemented on your MainActivity and triggered by your GPSTracker.

Second, you can look at the BroadcastReceiver. Just add a BroadcastReceiver on your main activity. On your method OnLocationChanged you just create a new intent :

Intent intent = new Intent("LocationChanged");
intent.setType("text/plain");
sendBroadcast(intent);

I think the first solution is not the easiest but the better.

Alex R.
  • 861
  • 1
  • 13
  • 28
  • The first option sounds good. Can you give me some simple example on how to do that? Thank – Boy Nov 29 '13 at 15:45
  • I think you can try the following example : [link](http://stackoverflow.com/questions/6270132/create-a-custom-event-in-java) – Alex R. Nov 29 '13 at 16:11
1

You can broadcast change from your service.

Intent broadcastIntent = new Intent("com.example.broadcast.gps.location_change");
broadcastIntent .putDouble("latitude", latitude);
broadcastIntent .putDouble("longitude", longitude);
sendBroadcast(broadcastIntent);

And listen for that broadcast in you MainActivity.

BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        double latitude = intent.getDoubleExtra("latitude", 0.0);
        double longitude = intent.getDoubleExtra("longitude", 0.0);
    }
};
registerReceiver(receiver, new IntentFilter("com.example.broadcast.gps.location_change"));
Boban S.
  • 1,662
  • 13
  • 16
0

You should declare a method in MainActivity as below

    public void notifyLocationChanged(Double lat, Double lng){
     //do some thing
    }

And in GPSTracker class at onLocationChanged method

 @Override
public void onLocationChanged(Location location) {
   ((MainActivity)mContext).notifyLocationChanged(location.getLatitude(),location.getLongitude())
}
Ba Tới Xì Cơ
  • 482
  • 4
  • 16
  • I tried this but the app crash and error was thrown. `java.lang.ClassCastException: android.app.Application cannot be cast to com.x.MainActivity at com.x.classes.GPSTracker.onLocationChanged(GPSTracker.java:191)` – Boy Nov 29 '13 at 15:43
  • in MainActivity you creat an instance of GPSTracker like this : GPSTracker gps = new GPSTracker(MainActivity.this); I think it would be ok – Ba Tới Xì Cơ Nov 30 '13 at 02:28