connected/disconntected 连接 Wi-Fi 时检测

Detect when connected/disconntected to Wi-Fi

我想知道 phone 何时失去 Wi-Fi 连接。我知道我应该使用广播接收器。所以我注册了广播接收器:

intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
registerReceiver(wifiScanReceiver, intentFilter);

然后我创建了广播接收器:

public class WifiScanReceiver extends BroadcastReceiver {

    public WifiScanReceiver() {

    }

    public void onReceive(Context c, Intent intent) {
        Log.i("TAG", "trigged");
    }
}

然后我连接到 Wi-Fi,当我打开 on/off 路由器时 - 没有任何反应。我做错了什么?

我拥有所有权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

尝试使用 ConnectivityManager class 将其放入 onReceive

ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = conMan.getActiveNetworkInfo();

    if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) 
        // Connection Successful 
    else
        // Connection  Failed 
  } 

由于您已在清单中授予 权限 以访问 Internet 和网络,因此该部分没问题。

此外,

您可以尝试 ConnectivityManager Class 检查您设备的网络/wifi 连接。

public boolean checkMobileInternetConn() {
        //Create object for ConnectivityManager class which returns network related info
        ConnectivityManager connectivity = (ConnectivityManager) _context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        //If connectivity object is not null
        if (connectivity != null) {
            //Get network info - WIFI internet access
            NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (info != null) {
                //Look for whether device is currently connected to WIFI network
                if (info.isConnected()) {
                    return true;
                }
            }
        }
        return false;
    }

(完整代码参考this post。)

试试这个 n 随时恢复 :)

nothing happens. What am I doing wrong?

据我所知,您还没有声明 BroadcastReceiver,单独添加权限不会触发您的广播接收器。

我的解决方案是基于通过清单声明它,但是你的问题是我看不到你在你的代码中声明了 broadcaster class,所以你可以选择在清单中或在你的代码中声明它.

所以尝试声明它,它应该可以工作。我已经用这两个声明测试了你的代码并且它有效:

<receiver android:name=".WifiScanReceiver">
    <intent-filter>
        <action android:name="android.net.wifi.STATE_CHANGE"/>
    </intent-filter>
</receiver>

或者在 onCreate 方法中以编程方式声明它new WifiScanReceiver():

final IntentFilter filters = new IntentFilter();
filters.addAction("android.net.wifi.STATE_CHANGE");
super.registerReceiver(new WifiScanReceiver(), filters);

您的 WifiScanReceiver 中也需要这个 (reference):

public void onReceive(Context context, Intent intent) {

        ConnectivityManager cm =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;

        if (isWiFi) {
            Log.i("TAG", "trigged");
        }
    }