导航菜单

页面标题

页面副标题

大众借. v4.1.2 - NetworkStateReceiver.java 源代码

正在查看: 大众借. v4.1.2 应用的 NetworkStateReceiver.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.base.commonlibrary.netstate;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import com.base.commonlibrary.netstate.ComNetWorkUtil;
import java.util.ArrayList;

public class NetworkStateReceiver extends BroadcastReceiver {
    private static final String ANDROID_NET_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
    public static final String TA_ANDROID_NET_CHANGE_ACTION = "ta.android.net.conn.CONNECTIVITY_CHANGE";
    private static ComNetWorkUtil.netType netType;
    private static BroadcastReceiver receiver;
    private static Boolean networkAvailable = false;
    private static ArrayList<NetChangeObserver> netChangeObserverArrayList = new ArrayList<>();

    private static BroadcastReceiver getReceiver() {
        if (receiver == null) {
            receiver = new NetworkStateReceiver();
        }
        return receiver;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        receiver = this;
        if (intent.getAction().equalsIgnoreCase(ANDROID_NET_CHANGE_ACTION) || intent.getAction().equalsIgnoreCase(TA_ANDROID_NET_CHANGE_ACTION)) {
            if (!ComNetWorkUtil.isNetworkAvailable(context)) {
                networkAvailable = false;
            } else {
                netType = ComNetWorkUtil.getAPNType(context);
                networkAvailable = true;
            }
            notifyObserver();
        }
    }

    public static void registerNetworkStateReceiver(Context context) {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(TA_ANDROID_NET_CHANGE_ACTION);
        intentFilter.addAction(ANDROID_NET_CHANGE_ACTION);
        context.getApplicationContext().registerReceiver(getReceiver(), intentFilter);
    }

    public static void checkNetworkState(Context context) {
        Intent intent = new Intent();
        intent.setAction(TA_ANDROID_NET_CHANGE_ACTION);
        context.sendBroadcast(intent);
    }

    public static void unRegisterNetworkStateReceiver(Context context) {
        if (receiver != null) {
            try {
                context.getApplicationContext().unregisterReceiver(receiver);
            } catch (Exception unused) {
            }
        }
    }

    public static Boolean isNetworkAvailable() {
        return networkAvailable;
    }

    public static ComNetWorkUtil.netType getAPNType() {
        return netType;
    }

    private void notifyObserver() {
        for (int i = 0; i < netChangeObserverArrayList.size(); i++) {
            NetChangeObserver netChangeObserver = netChangeObserverArrayList.get(i);
            if (netChangeObserver != null) {
                if (isNetworkAvailable().booleanValue()) {
                    netChangeObserver.onConnect(netType);
                } else {
                    netChangeObserver.onDisConnect();
                }
            }
        }
    }

    public static void registerObserver(NetChangeObserver netChangeObserver) {
        if (netChangeObserverArrayList == null) {
            netChangeObserverArrayList = new ArrayList<>();
        }
        netChangeObserverArrayList.add(netChangeObserver);
    }

    public static void removeRegisterObserver(NetChangeObserver netChangeObserver) {
        ArrayList<NetChangeObserver> arrayList = netChangeObserverArrayList;
        if (arrayList != null) {
            arrayList.remove(netChangeObserver);
        }
    }
}