Event 2.4 跳转接收消息

依赖: (导Event 的 jar包)

compile files('libs/EventBus2.4.jar')


无网络请求

MessageEvent

public class MessageEvent {//类的用途  事件类 用于封装消息

    private  String msg;

    public MessageEvent(String msg) {
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

MainActivity
/**
 * 订阅消息的页面
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //注册EventBus  单例设计模式
        EventBus.getDefault().register(this);

        findViewById(R.id.bt_jump).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,Main2Activity.class));
            }
        });
    }
    /* public void onEvent(MessageEvent event){
            Toast.makeText(this, "我接收到了EventBus发送过来的消息", Toast.LENGTH_SHORT).show();
        }*/
    public  void onEventMainThread(MessageEvent event){
        Toast.makeText(this, "我接收到了EventBus发送过来的消息", Toast.LENGTH_SHORT).show();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //页面销毁的地方注销掉EventBus
        EventBus.getDefault().unregister(this);
    }
}

xml:
 <Button
        android:id="@+id/bt_jump"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

Main2Activity:
 <Button
        android:id="@+id/bt_jump"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

xml2:
 <Button
        android:id="@+id/bt_send"
        android:text="发送消息"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值