1.配置build.gradle(或直接导入jar包)
在build.gradle中导入以下配置
例如:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.squareup:otto:1.3.5'
}
2.创建一个单例的BusProvider(名字貌似可以自定义,不过要让自己和别人看的懂)
public final class BusProvider {
private static final Bus BUS = new Bus();
public static Bus getInstance() {
return BUS;
}
private BusProvider() {
// No instances.
}
3.在需要使用的地方注册它,记得不用的时候取消注册
@Override
public void onStart() {
super.onStart();
BusProvider.getInstance().register(this);
}
@Override
public void onDestroy() {
super.onDestroy();
BusProvider.getInstance().unregister(this);
}
4.创建一个xxxEvent类,里面包含你要操作的数据类型
例如:
public class MessageEvent {
public String message;
public MessageEvent(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
5.在需要发送的消息的地方发送
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BusProvider.getInstance().post(dothing());
}
});
6.写一个产生事件的公有的返回值为xxxEvent的方法并加上注解:@Produce
@Produce
public MessageEvent dothing() {
return new MessageEvent(str.getText().toString());
}
7.在需要订阅的地方,也就是需要传值得地方写上一个公共方法并加上注解@Subscribe
@Subscribe
public void onEventMainThread(MessageEvent event) {
text.setText(event.message);
}
在这个方法中处理传过来的值
附上源代码:
Activity代码如下
public class MainActivity extends AppCompatActivity {
private EditText str;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
str = (EditText) findViewById(R.id.str);
button = (Button) findViewById(R.id.set);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BusProvider.getInstance().post(dothing());
}
});
}
@Produce
public MessageEvent dothing() {
return new MessageEvent(str.getText().toString());
}
@Override
public void onStart() {
super.onStart();
BusProvider.getInstance().register(this);
}
@Override
public void onDestroy() {
super.onDestroy();
BusProvider.getInstance().unregister(this);
}
}
Fragment代码如下
public class mainFragment extends Fragment {
private TextView text;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.mian, container, false);
text = (TextView) view.findViewById(R.id.text);
return view;
}
@Subscribe
public void onEventMainThread(MessageEvent event) {
text.setText(event.message);
}
@Override
public void onStart() {
super.onStart();
BusProvider.getInstance().register(this);
}
@Override
public void onDestroy() {
super.onDestroy();
BusProvider.getInstance().unregister(this);
}
}
获取事物实例代码如下:
public final class BusProvider {
private static final Bus BUS = new Bus();
public static Bus getInstance() {
return BUS;
}
private BusProvider() {
// No instances.
}
}
事件类代码如下
public class MessageEvent {
public String message;
public MessageEvent(String message) {
this.message = message;
}
public String getMessage() {
return message;
}