android监听wifi断开,使用BroadcastReceiver获取Android中断开的WiFi网络的SSID?

我有以下BroadcastRecevier:

public class WiFiConnectionEventsReceiver extends BroadcastReceiver {

private static final String TAG = WiFiConnectionEventsReceiver.class.getSimpleName();

@Override

public void onReceive(Context context, @NonNull Intent intent) {

Log.v(TAG, "action: " + intent.getAction());

Log.v(TAG, "component: " + intent.getComponent());

Bundle extras = intent.getExtras();

if (extras != null) {

for (String key : extras.keySet()) {

Log.v(TAG, "key [" + key + "]: " +

extras.get(key));

}

} else {

Log.v(TAG, "no extras");

}

ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo netInfo = conMan.getActiveNetworkInfo();

if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) {

Log.d("NetworkInfo", "Have Wifi Connection");

Log.d("NetworkInfo", netInfo.getExtraInfo());

Log.d("NetworkInfo", netInfo.getTypeName());

}

else {

Log.d("NetworkInfo", "Don't have Wifi Connection");

Log.d("NetworkInfo", netInfo.getExtraInfo());

Log.d("NetworkInfo", netInfo.getTypeName());

}

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

Log.d("WifiInfo", wifiManager.getConnectionInfo().toString());

}

}

它接收网络连接和断开连接事件 .

当它连接到WiFi网络时,我能够轻松获得网络的SSID .

但我希望能够在网络断开时获得WiFi网络的SSID(无需存储先前连接的网络,然后以这种方式匹配它们等) . 这可能吗?

WiFi连接时记录日志:

V/WiFiConnectionEventsReceiver: action: android.net.conn.CONNECTIVITY_CHANGE

V/WiFiConnectionEventsReceiver: component: ComponentInfo{com.example.test/com.example.test.WiFiConnectionEventsReceiver}

V/WiFiConnectionEventsReceiver: key [networkInfo]: [type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: "SKY123”, roaming: false, failover: false, isAvailable: true]

V/WiFiConnectionEventsReceiver: key [networkType]: 1

V/WiFiConnectionEventsReceiver: key [inetCondition]: 100

V/WiFiConnectionEventsReceiver: key [extraInfo]: "SKY123”

D/NetworkInfo: Have Wifi Connection

D/NetworkInfo: "SKY123”

D/NetworkInfo: WIFI

D/WifiInfo: SSID: SKY123, BSSID: 10:40:03:ad:6x:c9, MAC: 02:00:00:00:00:00, Supplicant state: COMPLETED, RSSI: -79, Link speed: 43Mbps, Frequency: 2412MHz, Net ID: 1, Metered hint: false, score: 60

WiFi断开连接时记录(注意未知的SSID):

V/WiFiConnectionEventsReceiver: action: android.net.conn.CONNECTIVITY_CHANGE

V/WiFiConnectionEventsReceiver: component: ComponentInfo{com.example.test/com.example.test.WiFiConnectionEventsReceiver}

V/WiFiConnectionEventsReceiver: key [networkInfo]: [type: WIFI[], state: DISCONNECTED/DISCONNECTED, reason: (unspecified), extra: , roaming: false, failover: false, isAvailable: true]

V/WiFiConnectionEventsReceiver: key [networkType]: 1

V/WiFiConnectionEventsReceiver: key [inetCondition]: 0

V/WiFiConnectionEventsReceiver: key [extraInfo]:

V/WiFiConnectionEventsReceiver: key [noConnectivity]: true

D/NetworkInfo: Don't have Wifi Connection

D/NetworkInfo: id

D/NetworkInfo: MOBILE

D/WifiInfo: SSID: , BSSID: , MAC: 02:00:00:00:00:00, Supplicant state: COMPLETED, RSSI: -127, Link speed: -1Mbps, Frequency: -1MHz, Net ID: -1, Metered hint: false, score: 0

我想要这个信息的原因是我希望能够跟踪用户在每个网络上的WiFi使用情况 - 连接和断开连接时间 .

如果没有断开连接的网络的SSID,我能想到的唯一方法就是:

if (WiFi network ABC disconnected)

if (if previous stored connection for WiFi network ABC has no disconnection time)

set WiFI network ABC disconnection time to now

然而,上述方法感觉不稳定,例如,如果由于某种原因错过了WiFi断开事件等等

你可能感兴趣的:(android监听wifi断开)