zoukankan      html  css  js  c++  java
  • NetChangeReceiver

    package com.android.demo.lileidemo.listener;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Handler;
    import android.os.HandlerThread;

    import com.android.demo.lileidemo.MyApplication;
    import com.android.demo.lileidemo.constant.AppConstants;
    import com.android.demo.lileidemo.utils.ActivityUtil;

    /**
    * date: 03/27/2020.
    * author: lilei.
    */
    public class NetChangeReceiver extends BroadcastReceiver{
    private static final String TAG = AppConstants.APP_TAG + "NetChangeReceiver ";
    private HandlerThread mHandlerThread = new HandlerThread("NetChangeReceiver");
    private Handler mWorker;
    private static volatile NetChangeReceiver mInstance;
    private static boolean lastCheckNetConnected = true;//last net disconnect check net connect state.
    private static long lastCheckMillis = 0; //last net disconnect time millis.

    public NetChangeReceiver() {
    mHandlerThread.start();
    mWorker = new Handler(mHandlerThread.getLooper());
    }

    /**
    * get Instance.
    *
    * @return instance.
    */
    public static NetChangeReceiver getInstance() {
    if (mInstance == null) {
    synchronized (NetChangeReceiver.class) {
    if (mInstance == null) {
    mInstance = new NetChangeReceiver();
    }
    }
    }
    return mInstance;
    }

    public void registerIviStateChangeListener() {
    //LogUtil.d(TAG + "registerIviStateChangeListener()");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
    NetworkInfo info = intent
    .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
    if (info == null) {
    return;
    }

    if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
    if (info.getType() == ConnectivityManager.TYPE_WIFI
    || info.getType() == ConnectivityManager.TYPE_MOBILE) {
    // LogUtil.d(TAG + "onReceive() 111 " + getConnectionType(info.getType())
    // + " connected");
    doNetConnectCollectData(info.getType());
    }
    } else if (NetworkInfo.State.DISCONNECTED == info.getState() && info.isAvailable()) {
    if (info.getType() == ConnectivityManager.TYPE_WIFI
    || info.getType() == ConnectivityManager.TYPE_MOBILE) {
    // LogUtil.d(TAG + "onReceive() 222 " + getConnectionType(info.getType())
    // + " disconnected");
    doNetDisconnectCollectData(info.getType());
    }
    }
    }

    }

    private void doNetConnectCollectData(int netType) {
    boolean isNetworkConnected = ActivityUtil.isNetworkConnected(
    MyApplication.getAppContext());
    // LogUtil.d(TAG + "doNetConnectCollectData() isNetworkConnected:" + isNetworkConnected
    // + " lastCheckNetConnected:" + lastCheckNetConnected + " lastCheckMillis:" + lastCheckMillis);
    }

    private void doNetDisconnectCollectData(int netType) {
    // LogUtil.d(TAG + "doNetDisconnectCollectData() 11");
    }

    private String getConnectionType(int type) {
    String connType = "";
    if (type == ConnectivityManager.TYPE_MOBILE) {
    connType = "Mobile network";
    } else if (type == ConnectivityManager.TYPE_WIFI) {
    connType = "WIFI network";
    }
    return connType;
    }

    }
  • 相关阅读:
    解决 Mac launchpad 启动台 Gitter 图标无法删除的问题
    React 与 React-Native 使用同一个 meteor 后台
    解决 React-Native mac 运行报错 error Failed to build iOS project. We ran "xcodebuild" command but it exited with error code 65. To debug build logs further, consider building your app with Xcode.app, by ope
    一行命令更新所有 npm 依赖包
    swift学习笔记
    IOS语言总结
    focusSNS学习笔记
    别小看锤子,老罗真的很认真
    windowsphone开发页面跳转到另一个dll中的页面
    【令人振奋】【转】微软潘正磊谈DevOps、Visual Studio 2013新功能、.NET未来
  • 原文地址:https://www.cnblogs.com/adamli/p/13140660.html
Copyright © 2011-2022 走看看