Android之广播监控wifi连接状态
【摘要】 package com.example.floatdialogtest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.NetworkInfo; import a...
-
package com.example.floatdialogtest;
-
-
import android.content.BroadcastReceiver;
-
import android.content.Context;
-
import android.content.Intent;
-
import android.net.NetworkInfo;
-
import android.net.wifi.WifiInfo;
-
import android.net.wifi.WifiManager;
-
-
public class WifiReceiver extends BroadcastReceiver{
-
@Override
-
public void onReceive(Context context, Intent intent) {
-
// TODO Auto-generated method stub
-
if(intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)){
-
//signal strength changed
-
}
-
else if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){//wifi连接上与否
-
System.out.println("网络状态改变");
-
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
-
if(info.getState().equals(NetworkInfo.State.DISCONNECTED)){
-
System.out.println("wifi网络连接断开");
-
}
-
else if(info.getState().equals(NetworkInfo.State.CONNECTED)){
-
-
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
-
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
-
-
//获取当前wifi名称
-
System.out.println("连接到网络 " + wifiInfo.getSSID());
-
-
}
-
-
}
-
else if(intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)){//wifi打开与否
-
int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
-
-
if(wifistate == WifiManager.WIFI_STATE_DISABLED){
-
System.out.println("系统关闭wifi");
-
}
-
else if(wifistate == WifiManager.WIFI_STATE_ENABLED){
-
System.out.println("系统开启wifi");
-
}
-
}
-
}
-
}
权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
receiver intetfilter
<intent-filter >
<action android:name="android.net.wifi.RSSI_CHANGED"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
</intent-filter>
文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。
原文链接:chenyu.blog.csdn.net/article/details/52443588
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)