EventBus和有无网络判断

//网络判断
  if (isOnline() == false){
            Toast.makeText(this,"无网络",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this,WepActivity.class);
            startActivity(intent);
        }

//网络
    public boolean isOnline() {
        //得到一个连接管理者
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        //得到联网信息
        NetworkInfo info = manager.getActiveNetworkInfo();
        //判断设备是否有网
//        Log.i("=====", "isOnline: " + info);
//        EventBus.getDefault().postSticky(info);
        return (info != null && info.isConnected());
    }

EventBus传值
首先创建一个接收消息的类。

public class MessageEvent {
    String title;
    String pic;
    String time;

    public MessageEvent(String title, String pic,String time) {
        this.title = title;
        this.pic = pic;
        this.time = time;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }
    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}
//在适配器中EventBus发送
EventBus.getDefault().postSticky(new MessageEvent(bean.get(position).getTitle(),bean.get(position).getPic(),bean.get(position).getShareURL()));
//在Activity中接收消息
public class OtherActivity extends AppCompatActivity {

    private TextView mTv;
    private ImageView mIv;
    private TextView mTv2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        initView();
        EventBus.getDefault().register(this);
    }

    private void initView() {
        mTv = (TextView) findViewById(R.id.tv);
        mIv = (ImageView) findViewById(R.id.iv);
        mTv2 = (TextView) findViewById(R.id.tv2);
    }

    @Subscribe(sticky = true)
    public void onEventMainThread(MessageEvent bean) {
        Glide.with(OtherActivity.this).load(bean.getPic()).into(mIv);
        mTv.setText(bean.getTitle());
        mTv2.setText(bean.getTime());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().removeAllStickyEvents();
        EventBus.getDefault().unregister(this);
    }
}
//GreenDao
    public static DaoSession session;
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"text");
        Database database = helper.getWritableDb();
        session = new DaoMaster(database).newSession();
    }
MyApp.session.getChildListBeanDao().insertInTx(listBeen);

你可能感兴趣的:(EventBus和有无网络判断)