[Android] Android 監聽WIFI

1.註冊一個廣播接收器,監聽網絡狀態
java

private void Start()
    {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        this.registerReceiver(myBroadcastReceiver, filter);
    }

2.取消註冊廣播器網絡

private void Stop(){
 this.unregisterReceiver(myBroadcastReceiver);
 }

3.新建監聽類NetworkConnectChangedReceiver繼承BroadcastReceiverapp

public class NetworkConnectChangedReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        // 這個監聽wifi的打開與關閉,與wifi的鏈接無關
        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction()))
        {
            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            switch (wifiState)
            {
            case WifiManager.WIFI_STATE_ENABLED:
                Log.e("APActivity", "WIFI_STATE_ENABLED");
                break;
            case WifiManager.WIFI_STATE_ENABLING:
                Log.e("APActivity", "WIFI_STATE_ENABLING");
                break;
            case WifiManager.WIFI_STATE_DISABLED:
                Log.e("APActivity", "WIFI_STATE_DISABLED");
                break;
            case WifiManager.WIFI_STATE_DISABLING:
                Log.e("APActivity", "WIFI_STATE_DISABLING");
                break;
            }
        }
        // 這個監聽wifi的鏈接狀態便是否連上了一個有效無線路由,當上邊廣播的狀態是WifiManager.WIFI_STATE_DISABLING,和WIFI_STATE_DISABLED的時候,根本不會接到這個廣播。
        // 在上邊廣播接到廣播是WifiManager.WIFI_STATE_ENABLED狀態的同時也會接到這個廣播,固然剛打開wifi確定尚未鏈接到有效的無線
        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction()))
        {
            Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (null != parcelableExtra)
            {
                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
                switch (networkInfo.getState())
                {
                case CONNECTED:
                    Log.e("APActivity", "CONNECTED");
                    break;
                case CONNECTING:
                    Log.e("APActivity", "CONNECTING");
                    break;
                case DISCONNECTED:
                    Log.e("APActivity", "DISCONNECTED");
                    break;
                case DISCONNECTING:
                    Log.e("APActivity", "DISCONNECTING");
                    break;
                case SUSPENDED:
                    Log.e("APActivity", "SUSPENDED");
                    break;
                case UNKNOWN:
                    Log.e("APActivity", "UNKNOWN");
                    break;
                default:
                    break;
                }
            }
        }
        // 這個監聽網絡鏈接的設置,包括wifi和移動數據的打開和關閉。
        // 最好用的仍是這個監聽。wifi若是打開,關閉,以及鏈接上可用的鏈接都會接到監聽。見log
        // 這個廣播的最大弊端是比上邊兩個廣播的反應要慢,若是隻是要監聽wifi,我以爲仍是用上邊兩個配合比較合適
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
        {
            NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (info != null)
            {
                StringBuilder sb = new StringBuilder();
                sb.append("info.getTypeName() : " + info.getTypeName() + "\n");
                sb.append("getSubtypeName() : " + info.getSubtypeName() + "\n");
                sb.append("getState() : " + info.getState() + "\n");
                sb.append("getDetailedState() : " + info.getDetailedState().name() + "\n");
                sb.append("getDetailedState() : " + info.getExtraInfo() + "\n");
                sb.append("getType() : " + info.getType());
                Log.e("APActivity", sb.toString());
            }
        }
    }
}
相關文章
相關標籤/搜索