Android 監聽wifi廣播的兩種方式

來源:互聯網
上載者:User

 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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.