功能:实现一个监听热点打开和关闭状态显示
创建一个WifiApView .java类继承LinearLayout ,该类中主要监听wifi热点状态变化,并且控制热点修改热点图标的显示
public class WifiApView extends LinearLayout {
private static final String TAG = WifiApView.class.getSimpleName();
public static final boolean DEBUG = false;
private Context mContext;
private ImageView ivHot;
private WifiManager mWifiManager;
public WifiApView(Context context) {
super(context);
mContext = context;
init();
initView(context);
}
public WifiApView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
initView(context);
}
public WifiApView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
init();
initView(context);
}
private void initView(Context context) {
View rootView = LayoutInflater.from(context).inflate(R.layout.layout_wifi_ap, this);
ivHot =(ImageView)findViewById(R.id.iv_hot);
updateHotspotStatus(isHotspotEnable());
}
private void init(){
mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Log.e(TAG," onAttachedToWindow() ");
registerBroadCastReceiver();
}
private void registerBroadCastReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
mContext.registerReceiver(receiver, filter);
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (DEBUG) Log.v(TAG, "onReceive: " + intent);
String action = intent.getAction();
if(action.equals(WifiManager.WIFI_AP_STATE_CHANGED_ACTION)){
int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_AP_STATE, 0);
if (state == WifiManager.WIFI_AP_STATE_ENABLED) {
updateHotspotStatus(true);
} else if (state == WifiManager.WIFI_AP_STATE_DISABLED) {
updateHotspotStatus(false);
} else if (state == WifiManager.WIFI_AP_STATE_DISABLING) {
} else if (state == WifiManager.WIFI_AP_STATE_ENABLING) {
}
}
}
};
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mContext.unregisterReceiver(receiver);
}
private boolean isHotspotEnable() {
boolean result=false;
int state=mWifiManager.getWifiApState();
switch (state) {
case WifiManager.WIFI_AP_STATE_ENABLING:
case WifiManager.WIFI_AP_STATE_ENABLED:
result=true;
break;
case WifiManager.WIFI_AP_STATE_DISABLING:
case WifiManager.WIFI_AP_STATE_DISABLED:
break;
}
return result;
}
private void updateHotspotStatus(boolean enable) {
if (enable) {
ivHot.setVisibility(View.VISIBLE);
} else {
ivHot.setVisibility(View.GONE);
}
}
}
创建一个layout_wifi_ap.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_hot"
android:layout_width="25dp"
android:layout_marginTop="15dp"
android:layout_height="25dp"
android:visibility="gone"
android:scaleType="centerCrop"
android:src="@drawable/iv_hot"/>
</LinearLayout>