Get access to the results of a running service from two different actions in Android

I have a service that gets the location of a user and translates latitude and longitude in intention.

I believe that I need a service to start the service (as opposed to a related service), because I would like it to be able to send a push notification to the user when a new geoFire entry from firebase comes in their radius even when the application is closed.

The service is currently configured for location translation.

Given that the location service will be a running service, I don’t know how to access the broadcast from two separate actions.

I believe that I could achieve this if it were a related service, but since I want it to remain indefinitely, I'm not sure what to do.

Here is a minimal example demonstrating my problem .

In particular, I can get the latitude and longitude for display in the first step by registering the broadcast receiver, but I can not get the latitude and longitude for display in the second step by registering a separate broadcast receiver.

I have read the documentation for Android about services , but still do not know what I have to do to solve this problem.

I tested all this on an emulator from Android Studio: Nexus 5X API 25 x86.

UPDATE November 8, 2017. I updated the github project to contain the EventBus solution presented below. It is located in the eventBus branch. The leading branch still contains ualtered, problematic code related to the issue.

To view:

git clone https://github.com/Atticus29/locationServiceMCV.git git checkout eventBus 

Then open the AndroidStudio or IDE of your choice and browse / launch as usual.

+1
source share
1 answer

Passing results is one way to solve this problem. But if you want to add lib to your project, I highly recommend the Green Robot EventBus . This is a very lightweight lib, as it is fast. In addition, it simply sends complex objects through it without executing the Parcelable interface.

This library operates on the basis of the publisher / subscriber, where in your case both Acts will be subscribers and the Service will be the publisher.

Event bus

First of all, you should add a dependency on your build.gradle file, as shown below.

 implementation 'org.greenrobot:eventbus:3.0.0' 

If you are in a Kotlin project, add this dependency as well:

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

Then define an Event class that will be responsible for transferring your data through the application. Please note that this can be done from Service to Activity . From a Fragment to another. Between actions, etc.

 public class MessageEvent { public double lat; public double lng; // Add additional fields here if needed public MessageEvent(double lat, double lng) { this.lat= lat; this.lng = lng; } } 

After that, you need to subscribe to these events on Activity .

 public class MyActivity extends AppCompatActivity { // ... @Override public void onResume() { 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); } @Override public void onPause() { super.onPause(); // This line will unregister your Activity. // It 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) public void onMessageEvent(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); double lat = event.lat; double lng = event.lng; // Do whatever you want with the data. }; } 

With this in mind, you are ready to publish your events. Now, in your Service , call the following code when you get the new Lat / Lng information.

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

Learn more about setting up and configuring EventBus in documents or in this tutorial .

+3
source

All Articles