6

In my Activity I send EventBus

@Override
protected void onResume() {
        super.onResume();
        EventBus.getDefault().post(new String("We are the champions"));
}

In my background service I register EventBus and try to get sent message from Activity like this

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

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

public void onEventBackgroundThread(String s){
        Log.d(TAG, "onEventBackgroundThread: " + s);
    }

But nothing happens. I tried to search internet but couldn't get answer.

Errors I get

No subscribers registered for event class java.lang.String
No subscribers registered for event class de.greenrobot.event.NoSubscriberEvent

1.Edit

I already have EventBus communication from Service to Activity. But now I want to have it also to work in reverse direction(From Activity to Service). So is it possible that it conflicts?

Rafael
  • 6,091
  • 5
  • 54
  • 79
  • Did you find a solution to this? – iRuth Jun 07 '15 at 20:53
  • It actually works in both directions when I start service with startService(Intent); in one of app Activities. But if I try to use new thread for background service it can't make communication. – Rafael Jun 08 '15 at 05:03
  • Yes, I eventually got it to work by using `startService`. Thanks for your response. – iRuth Jun 08 '15 at 07:03
  • I'm not sure I understand what you mean by "thread in background service", but I started threads in the background service that I created and I was able to accomplish my task using the `EventBus`. – iRuth Jun 08 '15 at 07:08
  • I tried to search my old code, but couldn't find the reason I couldn't communicate with background service. But as far as I remember it was something related to threads. – Rafael Jun 08 '15 at 09:18
  • Answer for background service is mentioned [here][1] [1]: http://stackoverflow.com/questions/15431768/how-to-send-event-from-service-to-activity-with-otto-event-bus – M.Hefny Jul 25 '15 at 21:12

2 Answers2

2

First you need to define a custom message event class:

public class MyMessageEvent {
   String s;
}

Then you should add a subscriber method into your service. Key thing is to include your MyMessageEvent as the parameter:

public void onEventBackgroundThread(MyMeasageEvent  myEvent){
        Log.d(TAG, "onEventBackgroundThread: " + myEvent.s);
    }

Plus make sure your service registers with EventBus.

Finally, create a MyMessageEvent in your activity and post it on the eventbus:

MyMessageEvent myEvent = new MyMessageEvent() ;
myEvent.s = "hello" ;

EventBus.getDefault().post(myEvent) ;

If it still doesn't get received, try splitting the post onto a seperate line from the get default. Sometimes chaining doesn't work.

Max Worg
  • 2,932
  • 2
  • 20
  • 35
DEzra
  • 2,978
  • 5
  • 31
  • 50
0

I have not used greenrobot before but I think it should be similar to guava's eventbus. I think you should create an event class instead of using String. You can have a look at https://github.com/greenrobot/EventBus. e.g.

public class MessageEvent {
String s;
}
vincentzhou
  • 710
  • 6
  • 18