无法接收Android设备的所有网络接口 [英] Not receiving all network interfaces of an Android device

查看:148
本文介绍了无法接收Android设备的所有网络接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查询对某些特定网络接口所做的网络更改.

I'm trying to query network changes made to some specific network interfaces.

我正在通过这样注册BroadcastReceiver来做到这一点:

I'm doing it by registering a BroadcastReceiver like this:

<receiver android:name="com.my.app.ReceiverNetworkInterfaceChange">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

当发生更改时,它会触发一个使用此调用查询接口的方法:

And when there is a change, it triggers a method which query the interfaces using this call:

final List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());

我一直在通过打开和关闭飞行模式进行测试.接收器检测到更改并触发更改.

I've been testing it by turning airplane mode on and off. The receiver detects the change and its triggered.

问题在于,更改飞行模式状态后,并非从getNetworkInterfaces()调用接收到所有接口.如果我直接调用该方法而不涉及飞行模式,则接口列表包含一个名为"ppp0"的接口,但是在打开和关闭飞行模式后,调用不会在列表中返回该"ppp0"接口,因此我可以不能查询...

The problem is that after changing the airplane mode status, not all interfaces are received from the getNetworkInterfaces() call. If I call the method directly without involving the airplane mode, the list of interfaces contains an interface called "ppp0", but after turning airplane mode on and off, the call does not return that "ppp0" interface on the list so I can't query it...

为什么在此特定界面上会发生这种情况?

Why is this happening for this specific interface?

推荐答案

我最终要做的是每分钟检查一次接口,而不是使用BroadcastReceiver.这样,接口最终在一段时间后返回到接口列表.

What I eventually did was checking the interfaces every minute instead of using the BroadcastReceiver. This way, the interface eventually got back to the interfaces list after a while.

这篇关于无法接收Android设备的所有网络接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆