Can I use greenrobot EventBus for Communication between Activity and Service?
Asked Answered
M

2

23

EventBus Can I use this library for Activity to Service communication ?

I have tried this in my app as follows:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EventBus.getDefault().register(this);
    setContentView(R.layout.activity_music_player);
    Intent serviceIntent=new Intent(MusicPlayerActivityTest.this,MusicPlayerServiceTest.class);
    startService(serviceIntent);
    EventBus.getDefault().post(new SetSongList(songArraList, 0));
}
@Override
protected void onDestroy() {
    EventBus.getDefault().unregister(this);
    super.onDestroy();
}

and in my service onEvent called

Meeting answered 28/9, 2015 at 19:49 Comment(7)
did you try it before posting here?Kazak
yes i tried. but am getting an error says "Subscriber class has no public methods called onEvent"Meeting
well... that's a different thing. You should post some code and logcat so we can help you to fix it (if possible)Kazak
can u explain how to use it?Meeting
"am getting an error says "Subscriber class has no public methods called onEvent"" -- we cannot help you with your subscriber, unless you post the code for your subscriber. In general, yes, you can use greenrobot's EventBus to communicate from a service to an activity (or vice versa). See github.com/commonsguy/cw-omnibus/tree/master/EventBus/… for an example.Stuppy
But onEvent is not calling on serviceMeeting
@MarianoZorrilla what makes you think that he didnt try?Everyday
V
30

You have to register the Subscriber, not the emitter.

So, remove register/unregister from your app if you do expect to get the event. If so, just add the onEvent (AnyEvent event) method to the Application class.

Then register EventBus in your service in onStart() and unregister in onStop().

It should work better then.

In your Application

public class MyApp extend Application {
  @Override
  public void onCreate() {
    super.onCreate();
    ...
    EventBus.getDefault().post(new SetSongList(songArraList, 0));
  }
}

or in your Activity

public class MyActivity extend Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    EventBus.getDefault().post(new SetSongList(songArraList, 0));
  }
}

and in your Service

public class MyService extends Service {
  ...
  @Override
  public void onCreate() {
    super.onCreate();
    EventBus.getDefault().register(this);
  }

  @Override
  public void onDestroy() {
    EventBus.getDefault().unregister(this);
    super.onDestroy();
  }

  public void onEvent(SetSongList event){
    // do something with event
  }
  ...
}
Vial answered 27/10, 2015 at 15:35 Comment(5)
Shouldn't it be @Subscribe public void onEvent(SetSongList event){?Cheesecloth
This doesn't seem to work anyway, it produces the following error: Could not dispatch event: class someEvent to subscribing class class someService java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()Cheesecloth
This is for EventBus 2.4, the API changed with the new version 3.0. Post your code in a new thread that I can have a look.Vial
what if i want to make the communication vice-versa? from the service to the activiity ? how can i do that?Hemisphere
Activity -> Service event flow will only work if the service runs in the same process.Single
C
5

In my case comment by zyamys helped. Also, answer by Anthony is correct because of mistake in code.

In case you use a different process, the answer is NO. If you use different process it means it runs on a different virtual machine (like Davlik). All static fields are different...! Example (AndroidManifest.xml):

<service android:name=".GPSTracker" android:process=":my_gps_tracker" />

If you run service in the same process answer is YES. Example (AndroidManifest.xml):

<service android:name=".GPSTracker"/>

In the first case, I suggest of using Intents in combination with send/receive broadcast functionality to send data between service and activity.

Commixture answered 27/3, 2018 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.