我在我的應用中使用廣播接收器類,並且正在廣播接收器類中對WiFi狀態進行操作。如果WiFi斷開或連接,然後做一些操作。當WiFi連接其顯示正確的消息,但是當WiFi斷開其不顯示WiFi斷開連接消息,而不是顯示其他語句。使用廣播接收器獲取WiFi狀態並對其進行操作
這是我的廣播接收機類代碼。
public class WifiReceiver extends BroadcastReceiver {
private static final String TAG = "NetworkStateReceiver";
@Override
public void onReceive(final Context context, final Intent intent) {
Log.d(TAG, "Network connectivity change");
if (intent.getExtras() != null) {
final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
//check if wifi is connected
if (ni != null && ni.isConnectedOrConnecting()) {
Log.i(TAG, "Network " + ni.getTypeName() + " connected");
}
else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
Log.i(TAG, "wifi disconnected");
}
else {
Log.i(TAG, "Network else state");
}
}
在清單
<receiver android:name=".WifiReceiver">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
爲什麼會投票?任何原因? –