如何检测用户何时将一个wifi连接转到其他wifi?

时间:2018-05-22 08:22:10

标签: java android wifi android-wifi

我有WiFi连接检测问题。我的目标是检测用户何时在不同的WiF之间切换。我找到了this,但它只检测WiFi建立的时间。在我的情况下,我需要知道什么时候一个WiFi网络在手机上改变为另一个。

1 个答案:

答案 0 :(得分:1)

您可以使用BroadcastReceiver

public class ConnectivityReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = conMan.getActiveNetworkInfo();
        if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                if(netInfo.isConnected()) {
                    WifiManager wifiManager = (WifiManager) context.getAplicationContext().getSystemService (Context.WIFI_SERVICE);
                    WifiInfo info = wifiManager.getConnectionInfo ();
                    String ssid  = info.getSSID();
                    Log.d("Wifi Connected", "Wifi name is "+ info.getSSID());
                }
        }
    }
}