其他分享
首页 > 其他分享> > 网络

网络

作者:互联网

public class CacheConnectManager {
    
    //单例
    public static CacheConnectManager cacheConnectManager;
    private CacheConnectManager() {
    }

    public static CacheConnectManager getInstance() {
        if (cacheConnectManager == null) {
            cacheConnectManager = new CacheConnectManager();
        }
        return cacheConnectManager;
    }


    
    private boolean isConnect;
    private Context context;
    private List<IConnect> list = new LinkedList<>();

    public boolean isConnect() {
        return isConnect;
    }
    public void init(Context applicationContext){
        this.context = applicationContext;
        getCurrentConnectStatus();
        initReceiver();
    }
    //获得链接状态
    public void getCurrentConnectStatus() {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if(activeNetworkInfo != null && activeNetworkInfo.isConnected()){
            isConnect = true;
        } else{
            isConnect = false;
        }
    }
    //注册广播
    public void initReceiver() {
        IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(broadcastReceiver,intentFilter);
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //网络发生改变自动发送广播
            if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)){
                getCurrentConnectStatus();
                notifyConnectChange();
            }
        }
    };

    //判断状态
    public void notifyConnectChange(){
        for (IConnect iConnect : list) {
            if(isConnect){
                iConnect.onConnect();
            } else{
                iConnect.onDisConnect();
            }
        }
    }

    //注册接口
    public void registerConnectListener(IConnect connect){
        if (!list.contains(connect)) {
            list.add(connect);
        }
    }
    //取消注册接口
    public void unRegisterConnectListener(IConnect connect){
        if (list.contains(connect)) {
            list.remove(connect);
        }
    }


    //链接状态返回
    public interface IConnect{
        void onConnect();
        void onDisConnect();
    }


}

loadpage

package com.example.framework.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.framework.R;

public abstract class LoadPage extends FrameLayout {
    public LoadPage(@NonNull Context context) {
        this(context, null);
    }

    public LoadPage(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadPage(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private View load_layout;
    private View error_layout;
    private View success_layout;
    private TextView textError;

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        LayoutInflater from = LayoutInflater.from(context);
        LayoutParams layoutParams = new LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.MATCH_PARENT);


        load_layout = from.inflate(R.layout.load_layout, null);
        addView(load_layout, layoutParams);

        error_layout = from.inflate(R.layout.error_layout, null);
        textError = error_layout.findViewById(R.id.error);
        addView(error_layout, layoutParams);

        success_layout = from.inflate(getSuccessLayout(), null);
        addView(success_layout, layoutParams);

        //没有时执行
        showSuccessLayout();
    }

    protected abstract int getSuccessLayout();

    public void showLoadLayout() {
        load_layout.setBackgroundColor(Color.WHITE);
        load_layout.setVisibility(VISIBLE);
        error_layout.setVisibility(GONE);
        success_layout.setVisibility(GONE);
    }

    public void showTransparentLoadLayout() {
        load_layout.setBackgroundColor(Color.TRANSPARENT);
        load_layout.setVisibility(VISIBLE);
        error_layout.setVisibility(GONE);
        success_layout.setVisibility(VISIBLE);
    }
    public void showErrorLayout() {
        load_layout.setVisibility(GONE);
        error_layout.setVisibility(VISIBLE);
        success_layout.setVisibility(GONE);
    }

    public void showSuccessLayout() {
        load_layout.setVisibility(GONE);
        error_layout.setVisibility(GONE);
        success_layout.setVisibility(VISIBLE);
    }
    public void showErrorText(String error){
        showErrorLayout();
        textError.setText(error);
    }
}


标签:layout,setVisibility,void,网络,context,error,public
来源: https://blog.csdn.net/qq_47448371/article/details/116404197