Boy
Boy

Reputation: 435

Listen onLocationChanged from activities

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;
    }

}

Upvotes: 3

Views: 2916

Answers (3)

Alex R.
Alex R.

Reputation: 861

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.

Upvotes: 3

Ba Tới Xì Cơ
Ba Tới Xì Cơ

Reputation: 492

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())
}

Upvotes: 0

Boban S.
Boban S.

Reputation: 1662

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"));

Upvotes: 1

Related Questions