Android 广播监听wifi
第一步:配置AndroidManifest.xml,在application结点下面添加如下代码
<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>
同样配置AndroidManifest.xml,添加权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
第二步:注册广播
IntentFilter filter = new IntentFilter(); /** * NETWORK_STATE_CHANGED_ACTION * * wifi是否连接正常 * */ filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); /** * WIFI_STATE_CHANGED_ACTION * * 有无wifi源及各个状态间转变 * */ filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); /** * CONNECTIVITY_ACTION * * wifi如果打开,关闭,以及连接上可用的连接都会接到监听 * 这个广播的最大弊端是比上边两个广播的反应要慢,如果只是要监听wifi,我觉得还是用上边两个配合比较合适 * */ filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(new NetworkConnectChangedReceiver(new NetworkConnectChangedActionImp()), filter);
第三步:
import android.content.Context; import android.content.Intent; public interface NetworkConnectChangedAction { void doAfterCONNECTED(Context context , Intent intent); void doAfterCONNECTING(Context context , Intent intent); void doAfterDISCONNECTED (Context context , Intent intent); void doAfterDISCONNECTING (Context context , Intent intent); void doAfterSUSPENDED(Context context , Intent intent); void doAfterUNKNOWN(Context context , Intent intent); }
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class NetworkConnectChangedReceiver extends BroadcastReceiver { private static NetworkConnectChangedAction action = null; static{ action = new NetworkConnectChangedActionImp(); } @Override public void onReceive(Context context, Intent intent ) { if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); /** * GPRS */ //NetworkInfo gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); /** * INFO */ //NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); /** * WIFI */ NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (wifi != null) { if (NetworkInfo.State.CONNECTED == wifi.getState()) { action.doAfterCONNECTED(context, intent); } else if (NetworkInfo.State.CONNECTING == wifi.getState()) { action.doAfterCONNECTING(context, intent); } else if (NetworkInfo.State.DISCONNECTED == wifi.getState()) { action.doAfterDISCONNECTED(context, intent); } else if (NetworkInfo.State.DISCONNECTING == wifi.getState()) { action.doAfterDISCONNECTING(context, intent); } else if (NetworkInfo.State.SUSPENDED == wifi.getState()) { action.doAfterSUSPENDED(context, intent); } else if (NetworkInfo.State.UNKNOWN == wifi.getState()) { action.doAfterUNKNOWN(context, intent); } } } } }
第四步:具体实现
import android.content.Context; import android.content.Intent; public class NetworkConnectChangedActionImp implements NetworkConnectChangedAction{ @Override public void doAfterCONNECTED(Context context, Intent intent) {} @Override public void doAfterCONNECTING(Context context, Intent intent) {} @Override public void doAfterDISCONNECTED(Context context, Intent intent) {} @Override public void doAfterDISCONNECTING(Context context, Intent intent) {} @Override public void doAfterSUSPENDED(Context context, Intent intent) {} @Override public void doAfterUNKNOWN(Context context, Intent intent) {} }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。