萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> Android 監聽wifi廣播的兩種方式

Android 監聽wifi廣播的兩種方式

 1.XML中聲明

<receiver android:name=".NetworkConnectChangedReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>

2.代碼中注冊
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(new NetworkConnectChangedReceiver(), filter);

3.實現

package com.example.testss;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiManager;
import android.os.Parcelable;
import android.util.Log;

public class NetworkConnectChangedReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {// 這個監聽wifi的打開與關閉,與wifi的連接無關
            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            Log.e("H3c", "wifiState" + wifiState);
            switch (wifiState) {
            case WifiManager.WIFI_STATE_DISABLED:
                break;
            case WifiManager.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;
                State state = networkInfo.getState();
                boolean isConnected = state == State.CONNECTED;// 當然,這邊可以更精確的確定狀態
                Log.e("H3c", "isConnected" + isConnected);
                if (isConnected) {
                } else {

                }
            }
        }
        // 這個監聽網絡連接的設置,包括wifi和移動數據的打開和關閉。.
        // 最好用的還是這個監聽。wifi如果打開,關閉,以及連接上可用的連接都會接到監聽。見log
        // 這個廣播的最大弊端是比上邊兩個廣播的反應要慢,如果只是要監聽wifi,我覺得還是用上邊兩個配合比較合適
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
<p>	ConnectivityManager manager = (ConnectivityManager) context
  .getSystemService(Context.CONNECTIVITY_SERVICE);
 	NetworkInfo gprs = manager
  .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
 	NetworkInfo wifi = manager
  .getNetworkInfo(ConnectivityManager.TYPE_WIFI);</p><p> 	Log.i(TAG, "網絡狀態改變:" + wifi.isConnected() + " 3g:" + gprs.isConnected());</p><p>            NetworkInfo info = intent
                    .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (info != null) {
                Log.e("H3c", "info.getTypeName()" + info.getTypeName());
                Log.e("H3c", "getSubtypeName()" + info.getSubtypeName());
                Log.e("H3c", "getState()" + info.getState());
                Log.e("H3c", "getDetailedState()"
                        + info.getDetailedState().name());
                Log.e("H3c", "getDetailedState()" + info.getExtraInfo());
                Log.e("H3c", "getType()" + info.getType());

                if (NetworkInfo.State.CONNECTED == info.getState()) {
                } else if (info.getType() == 1) {
                    if (NetworkInfo.State.DISCONNECTING == info.getState()) {

                    }
                }
            }
        }
    }
}</p>
copyright © 萬盛學電腦網 all rights reserved