动态获取网络状态,灵活配置显示内容(一站式到家服务)

废话不说,直接上代码

父类

protected StateLayoutManager statusLayoutManager;
    private BroadcastReceiver broadcastReceiver = new InternetBroadcastReceiver(this);
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int layoutId = initView(savedInstanceState);
        setContentView(R.layout.activity_main);
        //注册广播
        IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(broadcastReceiver, intentFilter);

        initStatusLayout(layoutId);
        initBaseView();

        initView();
    }

    protected  void initStatusLayout(int a) {
        statusLayoutManager = StateLayoutManager.newBuilder(this)
                .contentView(a)
                .emptyDataView(R.layout.activity_emptydata)
                .errorView(R.layout.activity_error)
                .loadingView(R.layout.activity_loading)
                .netWorkErrorView(R.layout.activity_networkerror)
                .build();
    }
    protected abstract void initView();
    /**
     * 获取到布局
     */
    private void initBaseView() {
        LinearLayout ll_main = (LinearLayout) findViewById(R.id.ll_main);
        ll_main.addView(statusLayoutManager.getRootLayout());
    }

    //正常展示数据状态
    protected void showContent() {
        statusLayoutManager.showContent();
    }

    //加载数据为空时状态
    protected void showEmptyData() {
        statusLayoutManager.showEmptyData();
    }

    //加载数据错误时状态
    protected void showError() {
        statusLayoutManager.showError();
    }

    //网络错误时状态
    protected void showNetWorkError() {
        statusLayoutManager.showNetWorkError();
    }

    //正在加载中状态
    protected void showLoading() {
        statusLayoutManager.showLoading();
    }

    /**
     * 网络中断
     */
    public void onError() {
        showNetWorkError();
        ConstraintLayout ll_set_network = (ConstraintLayout) findViewById(R.id.ll_set_network);
        ll_set_network.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("android.settings.WIRELESS_SETTINGS");
                startActivity(intent);
            }
        });
    }

    /**
     * 网络连接成功
     */
    protected void onConnect() {
        showContent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解除广播
        unregisterReceiver(broadcastReceiver);
    }

广播

public class InternetBroadcastReceiver extends BroadcastReceiver {

    private BaseMainActivity mContext;

    public InternetBroadcastReceiver(BaseMainActivity mainActivity) {
        mContext = mainActivity;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetworkInfo!=null &&  activeNetworkInfo.isConnected()){
                Log.d("网络链接成功","网络链接成功");
                mContext.onConnect();
            }else {
                Log.d("网络断开","网络断开");
                mContext.onError();
            }
        }
    }




}

接口

public interface IActivity {

    /**
     * 初始化 View
     *
     * @param savedInstanceState
     * @return
     */
    int initView(@Nullable Bundle savedInstanceState);
}

怎么使用呢?让自己的页面继承 BaseMainActivity

@Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_main2;
    }

    @Override
    protected void initView() {

    }

一切OK,完美!最后附上项目下载链接

https://download.csdn.net/download/qq_34115898/11395757

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值