Android网络框架

内容


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import androidx.lifecycle.LiveData;

import java.util.LinkedList;
import java.util.List;

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 synchronized 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 synchronized void notifyConnectChange(){
        for (IConnect iConnect : list) {
            if(isConnect){
                iConnect.onConnect();
            } else{
                iConnect.onDisConnect();
            }
        }
    }

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


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


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值