在工程的build.gradle配置
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
在model的build.gradle配置
dependencies {
implementation 'com.github.zhudaihao:livedatbus-master:1.0.1'
}
现在就可以愉快的开始玩耍了
我创建三个activity:MainActivity负责发送消息(全部订阅者都可以接受 和创建后的订阅的接);TextAllActivity和TextCreationActivity在消息发送后 才创建订阅的;
MainActivity对应的xml代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="sendAll" android:text="发布所有订阅者" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="sendCreation" android:text="发布创建的订阅者" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </LinearLayout>
代码很简单就两个按钮,对应MainActivity代码
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 发布给 所有订阅者 * * @param view */ public void sendAll(View view) { HomeBean homeBean = new HomeBean("张三"); LiveDataBus.getDefault().with("homebean", HomeBean.class).postValue(homeBean); startActivity(new Intent(this,TextAllActivity.class)); } /** * 发布给 创建的订阅者 * * @param view */ public void sendCreation(View view) { HomeBean homeBean = new HomeBean("李四"); LiveDataBus.getDefault().withCreation("homebean", HomeBean.class).postValue(homeBean); startActivity(new Intent(this,TextCreationActivity.class)); } }
//TextAllActivity的XML代码就一个按钮就不写了,看看Java代码
/**
* 测试 在发送后 订阅的用户
*/
public class TextAllActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all);
button=findViewById(R.id.bt);
//订阅
LiveDataBus.getDefault().with("homebean", HomeBean.class).observe(this, new Observer<HomeBean>() {
@Override
public void onChanged(HomeBean homeBean) {
//订阅结果回调
button.setText(homeBean.getName());
}
});
}
}
//TextCreationActivity的XML代码就一个按钮就不写了,看看Java代码
/**
* 测试 在发送后 订阅的用户
*/
public class TextCreationActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_creation);
button=findViewById(R.id.bt);
//订阅
LiveDataBus.getDefault().withCreation("homebean", HomeBean.class).observe(this, new Observer<HomeBean>() {
@Override
public void onChanged(HomeBean homeBean) {
//订阅结果回调
button.setText(homeBean.getName());
}
});
}
}
效果如下
这个我通过调用不同方法实现指定对象接收消息;
源码地址:https://github.com/zhudaihao/livedatbus-master