首页 > 代码库 > Android 广播监听wifi
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) {} }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。