Skip to content Skip to sidebar Skip to footer

Access The Results Of A Started Service From Two Different Activities In Android

I have a service that gets the user's location and broadcasts the latitude and longitude in an intent. I believe that I need the service to be a started service (as opposed to bou

Solution 1:

Broadcasting the results is one way to solve this issue. But if you're willing to add a lib on your project I would strongly recommend Green Robot's EventBus. It's a very lightweight lib, as it's fast. Also, it's simple to post complex objects through it without having to implement the Parcelable interface.

This lib works on a publisher/subscriber fashion, where in your case, both Activities would be the subscribers, and the Service would be the publisher.

Event Bus

First of all, you have to add the dependency on your build.gradle file as below.

implementation 'org.greenrobot:eventbus:3.0.0'

If you're on a Kotlin project, also add this dependency:

kapt 'org.greenrobot:eventbus-annotation-processor:3.0.1'

Then, define an Event class that will be responsible to carry your data through the app. Note that this could be done from a Service to an Activity. From a Fragment to another. Between Activities, and so on.

publicclassMessageEvent { 
    publicdouble lat;
    publicdouble lng;
    // Add additional fields here if neededpublicMessageEvent(double lat, double lng){
        this.lat= lat;
        this.lng = lng;
    }
}

After that, you have to subscribe to those events on your Activity.

publicclassMyActivityextendsAppCompatActivity {
    // ...@OverridepublicvoidonResume() {
         super.onResume();
         // This line will register your Activity to the EventBus// making sure that all the methods annotated with @Subscribe // will be called if their specific Events are posted.
         EventBus.getDefault().register(this);
     }

     @OverridepublicvoidonPause() {
         super.onPause();
         // This line will unregister your Activity.// It's a good practice to put this on the onPause() method// to make your event handling system tied to the Activity lifecycle.
         EventBus.getDefault().unregister(this);
     }

    // The threadMode MAIN makes sure this method is called on the main Thread. // But you could also set it up to be called on other threads if needed. Check the docs for more info.@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)publicvoidonMessageEvent(MessageEvent event) {
        /* Call this line below if you want to remove the sticky event.
        *  That will prevent that this event will be seen by other subscribers once they subscribe.
        *  In your specific use case, you don't have to remove the sticky event here. 
        */// EventBus.getDefault().removeStickyEvent(event);doublelat= event.lat;
        doublelng= event.lng;
        // Do whatever you want with the data.
    };
}

With that implemented, you're ready to post your events. Now, inside your Service, call the following code when you get the new Lat/Lng information.

EventBus.getDefault().postSticky(new MessageEvent(lat,lng));

You can read more on how to setup and customize the EventBus on the docs, or on this tutorial.

Post a Comment for "Access The Results Of A Started Service From Two Different Activities In Android"