首页 > 解决方案 > 如何获取有关断开网络的详细信息?

问题描述

有没有办法使用android广播接收器或networkcallback获取有关断开网络的详细信息?

EXTRA_NETWORK_INFO、getNetworkInfo(NetworkType) 已弃用。

getAllNetworks() 返回框架当前跟踪的所有网络的数组,但不返回断开连接的网络。

标签: androidandroid-broadcastandroid-thingsandroid-things-console

解决方案


是的,首先像这样注册广播接收器:

final IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(receiver, intentFilter);

当接收者是:

new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // this part is trigered when there is some network changes, do checks here
            }
        };

也不要忘记注销它。


推荐阅读