美文网首页
Android 实时监测网络,网络变化监听 适配7.0方案

Android 实时监测网络,网络变化监听 适配7.0方案

作者: zhengLH | 来源:发表于2019-06-04 12:10 被阅读0次

    【友情链接】 https://www.jianshu.com/p/89e3fbd33964
    【总结】android 7.0之后必须动态注册 网络监听。

     **
     * 网络状态监听
     *
     * @author lee
     * @date 2018/11/29
      */
    
    public class NetworkReceiver extends BroadcastReceiver {
    
    private NetworkCallback networkCallback;
    private ConnectivityManager.NetworkCallback callback;
    
    public NetworkReceiver(Activity activity, NetworkCallback networkCallback) {
        this.networkCallback = networkCallback;
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            ConnectivityManager connManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    
            callback = new ConnectivityManager.NetworkCallback() {
    
                @Override
                public void onAvailable(Network network) {
                    super.onAvailable(network);
                    networkCallback.onAvailable();
                }
    
                @Override
                public void onLost(Network network) {
                    super.onLost(network);
                    networkCallback.onLost();
                }
            };
            connManager.registerDefaultNetworkCallback(callback);
        } else {
            IntentFilter intentFilter = new IntentFilter();
            //intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
            //intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
            intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    
            activity.registerReceiver(this, intentFilter);
        }
    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() == ConnectivityManager.CONNECTIVITY_ACTION) {
            NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (NetworkUtils.isConnected()) {
                this.networkCallback.onAvailable();
            } else {
                this.networkCallback.onLost();
            }
        }
    }
    
    /**
     * 取消网络监听
     *
     * @param activity
     */
    public void unRegister(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            ConnectivityManager connManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    
            connManager.unregisterNetworkCallback(callback);
        } else {
            activity.unregisterReceiver(this);
        }
    }
    
    /**
     * 网络状态回调
     */
    public interface NetworkCallback {
    
        /**
         * 网络可用
         */
        void onAvailable();
    
        /**
         * 网络不可用,丢失连接
         */
        void onLost();
    
         }
    
      }
    

    相关文章

      网友评论

          本文标题:Android 实时监测网络,网络变化监听 适配7.0方案

          本文链接:https://www.haomeiwen.com/subject/nfklxctx.html