Android:在广播接收器中连接到wifi/3g时,检查网络连接返回是否未连接 [英] Android: Checking network connectivity return not connected when connected to wifi/3g in Broadcast receiver

查看:114
本文介绍了Android:在广播接收器中连接到wifi/3g时,检查网络连接返回是否未连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查设备是否已连接在broadcastReceiver中. 下面是我的代码:

I want to check if device in connected or not in broadcastReceiver. below is my code :

public boolean isOnline(Context context) {
NetworkInfo info = (NetworkInfo) ((ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

if (info == null || !info.isConnected()) {
    Log.e("UpdateDataReceiver","info: "+info);
    return false;
}
return true;
}

问题与我的代码: 以上功能会在后台触发BroadcastReceiver时(即使应用程序处于后台)也返回false(即使连接了wifi) 当应用程序处于前台时,它将返回true.

Issue with my code: above function returns me false (even when wifi connected) when BroadcastReceiver fires in background(when app is in background) and it returns true when app is in foreground.

信息:NetworkInfo:类型:WIFI [],状态:DISCONNECTED/BLOCKED,原因: (未指定),额外:(无),漫游:false,故障转移:false, isAvailable:true,isConnectedToProvisioningNetwork:false,simId:0

info: NetworkInfo: type: WIFI[], state: DISCONNECTED/BLOCKED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false, simId: 0

设备信息:Redmi注意事项

Device Info: Redmi Note

推荐答案

这是我的处理方式,事实证明,即使存在网络连接,在特定情况下,getActiveNetworkInfo始终会返回DISCONNECTED/BLOCKED.这是BroadcastReceiver中意图为filter ConnectivityManager.CONNECTIVITY_ACTION的接收方法.

This is how I'm handling it as it turns out getActiveNetworkInfo will always return you DISCONNECTED/BLOCKED in a specific case even if there is network connection. This is the receive method in the BroadcastReceiver with intent filter ConnectivityManager.CONNECTIVITY_ACTION.

@Override
public void onReceive(Context context, Intent intent) {
    ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = conn.getActiveNetworkInfo();

    NetworkInfo intentNetworkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

    if (intentNetworkInfo == null) {
        intentNetworkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
    }

    if (networkInfo == null) {
        networkInfo = intentNetworkInfo;
    } else {
        //when low battery get ActiveNetwork might receive DISCONNECTED/BLOCKED but the intent data is actually CONNECTED/CONNECTED
        if (intentNetworkInfo != null && networkInfo.isConnectedOrConnecting() != intentNetworkInfo.isConnectedOrConnecting()) {
            networkInfo = intentNetworkInfo;
        }
    }

    //do something with networkInfo object
}

我一直在寻找更好的解决方案,但没有结果.我能够在设备(Pixel 7.1.2)上重现100%的情况如下:

I've searched for better solution but no results. The case I've been able to reproduce 100% on my device (Pixel 7.1.2) is the following:

  • 设备必须处于低电量状态< 15%(其他设备< 20%)
  • Wifi已开启,应用已启动
  • 将应用发送到后台关闭wifi并转到3g(反之亦然)
  • 返回到应用程序
  • 在这种情况下,应用程序将报告getActiveNetworkInfo的断开/阻塞状态.
  • The device must be on low battery < 15% (other devices <20%)
  • Wifi is on, app is launched
  • Send app to background turnoff wifi and go to 3g (or vice versa)
  • Go back to the app
  • In that situation the app will report DISCONNECTED/BLOCKED from getActiveNetworkInfo.

如果在应用程序中更改连接性可以,但是如果在后台则不会.在调试时不会发生这种情况,因为即使电池电量不足,它也会为设备充电.

If you change connectivity while in app it will be ok but if it is on background it wont. This won't happen while you are debugging because it will be charging the device even if the battery is low.

在上面的示例中,ConnectivityManager和WifiManager中的EXTRA_NETWORK_INFO实际上是相同的字符串"networkInfo",但是如果在其他Android版本中它们不同,我就不会冒险,所以这是多余的样板.

In the example above EXTRA_NETWORK_INFO in ConnectivityManager and WifiManager is actually same string "networkInfo" but I didn't wan't to risk it if in other Android versions they are different, so it is extra boilerplate.

您可以直接从意图中使用networkInfo,但我想在此说明,在某些情况下,actualNetworkInfo不是实际的网络信息.

You can directly use the networkInfo from the intent but I wanted to show here that there is a case where actualNetworkInfo is not the actual network info.

这篇关于Android:在广播接收器中连接到wifi/3g时,检查网络连接返回是否未连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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