android - How to send event from Activity to service with greenrobot event bus? -
simple eventbus.getdefault().post() bring exception not main thread. how send event activity service greenrobot event bus?
chronometer = (chronometer)findviewbyid(r.id.chrono); chronometer.setbase(systemclock.elapsedrealtime()); chronometer.setonchronometerticklistener(new chronometer.onchronometerticklistener() { @override public void onchronometertick(chronometer arg0) { long countup = (systemclock.elapsedrealtime() - arg0.getbase()) / 1000; string astext = (countup / 60) + ":" + (countup % 60); log.e("astext", "astext" + astext); chronometerevents event=new chronometerevents(); event.settime(astext); bus.post(event); } }); public class chronometerevents { private string time; public chronometerevents() { } public string gettime() { return time; } public void settime(string time) { this.time = time; } } public void onevent(chronometerevents freq) { log.e("chronometer", "chronometer" + freq.gettime()); }
service class
public class newlocationupdateservice extends service { private eventbus bus = eventbus.getdefault(); @override public int onstartcommand(intent intent, int flags, int startid) { bus.unregister(this); return start_not_sticky; } @override public ibinder onbind(intent intent) { return binder; } @override public boolean onunbind(intent intent) { return super.onunbind(intent); } public void onevent(chronometerevents freq) { log.e("chronometer", "chronometer" + freq.gettime()); } }
is posible send using greenrobot event bus?
i recommend reconsidering architecture here. eventbus not designed cross process boundaries , android services don't lend idea.
the eventbus great tool easy misuse.
if want send information activity service it's best use intent's, if need closer interaction should binding. eventbus should not replace these interactions.
Comments
Post a Comment