Android(安卓)EventBus 传递消息
16lz
2022-04-04
挺好用的 试了一下在 Activity 和 Service 里面传递消息
public class MyService extends Service { @Override public IBinder onBind(Intent intent) { throw new UnsupportedOperationException("Not yet implemented"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { //三个参数分别是,消息订阅者(接收者),接收方法名,事件类 EventBus.getDefault().register(this, "test", TestEvent.class); return super.onStartCommand(intent, flags, startId); } public void test(TestEvent event) { Log.e("e", "e"); }}
startService(new Intent(MainActivity.this, MyService.class)); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { EventBus.getDefault().post(new TestEvent()); } });
register之后就可以post了
更多相关文章
- android延时更新UI
- [Android] 监听系统网络连接打开或者关闭的消息
- android handle ui 更新
- android handle ui 更新
- android的单元测试
- Android(安卓)调用中国气象的webservice
- android屏幕监听,手机是否处于锁屏状态,在锁屏之上弹出Toast.makeT
- Android(安卓)自定义 Toast 显示时间
- android 刷新View