之前做的,拿出来备用
package com.example.wifitest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.storage.StorageManager;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
/**
*
* @ClassName: MainActivity
* @Description: TODO(状态栏信号,数据图标显示)
* @author ybf
* @date 2017-4-24
* @see MainActivity
*/
public class MainActivity extends Activity {
protected static final String TAG = "MainActivity";
/**
* 移动数据和wifi
*/
private ImageView mobileAndWifi;
/**
* 热点
*/
private ImageView ap;
/**
* 网络类型
*/
private ImageView singType;
/**
* 型号强弱
*/
private ImageView singStrong;
private ServiceState mServiceState;
private SignalStrength mSignalStrength;
int mDataNetType = TelephonyManager.NETWORK_TYPE_UNKNOWN;
int mDataState = TelephonyManager.DATA_DISCONNECTED;
int mPhoneSignalIconId;
int mMobileTypeIconId;
int mDataTypeIconId;
boolean mDataVisible;
public Context mContext;
private WifiManager mWifiManager;
private TelephonyManager mPhone;
private int signalLevel;
private int mobileType;// 获取网络类型
private int mobileDateType;// 获取移动数据类型
private boolean isMobile;
private boolean isWifiConnected;
private boolean isApState;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
initView();
initData();
}
private void initView() {
// TODO Auto-generated method stub
mobileAndWifi = (ImageView) findViewById(R.id.mobileAndWifi);
ap = (ImageView) findViewById(R.id.ap);
singType = (ImageView) findViewById(R.id.singType);
singStrong = (ImageView) findViewById(R.id.singStrong);
}
private void initData() {
// TODO Auto-generated method stub
mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
registerPhoneStateListerer();
registReceiverWifi();
refreshViews();
}
// 注册手机状态监听
void registerPhoneStateListerer() {
mPhone = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhone.listen(mPhoneStateListerer, PhoneStateListener.LISTEN_SERVICE_STATE// 监测是否有服务
| PhoneStateListener.LISTEN_SIGNAL_STRENGTHS // 监测信号变化
| PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);// 监测连接网络类型变化
}
PhoneStateListener mPhoneStateListerer = new PhoneStateListener() {
@Override
public void onDataConnectionStateChanged(int state, int networkType) {
// TODO Auto-generated method stub
super.onDataConnectionStateChanged(state, networkType);
mDataState = state;
mDataNetType = networkType;
refreshViews();
}
@Override
public void onServiceStateChanged(ServiceState serviceState) {
// TODO Auto-generated method stub
super.onServiceStateChanged(serviceState);
mServiceState = serviceState;
Log.v(TAG, "serviceState " + serviceState);
refreshViews();
}
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
// TODO Auto-generated method stub
super.onSignalStrengthsChanged(signalStrength);
mSignalStrength = signalStrength;
Log.e(TAG, "signalStrength--->" + signalStrength);
signalLevel = signalStrength.getLevel();// 信号强弱
Log.e(TAG, "signalLevel--->" + signalLevel);
refreshViews();
}
};
/**
* 刷新View 信号强度, 网络类型 ,当前数据类型, 热点状态
*/
private void refreshViews() {
mobileType = getMobileTypeIconId();
mobileDateType = getMobileDataTypeIconId();
setSignalStrong();
setMobileTypeIcon();
setDataTypeIcon();
setApIcon();
}
/**
* 设置当前数据类型
*/
private void setDataTypeIcon() {
isMobile = isMobile(mContext);
isWifiConnected = isWifiConnected(mContext);
isApState = isWifiApState();
if (isMobile) { // 判断是否连接移动数据
mobileAndWifi.setImageResource(mobileDateType);
mobileAndWifi.setVisibility(View.VISIBLE);
} else if (isWifiConnected) {// 判断是否连接wifi
mobileAndWifi.setImageResource(R.drawable.wifi_client);
mobileAndWifi.setVisibility(View.VISIBLE);
} else {
mobileAndWifi.setVisibility(View.GONE);
}
}
/**
* 设置热点AP 状态
*/
public void setApIcon() {
if (isApState) {
ap.setImageResource(R.drawable.wifi_ap);
ap.setVisibility(View.VISIBLE);
} else {
ap.setVisibility(View.GONE);
}
}
/**
* 网络链接状态改变广播
*/
public BroadcastReceiver mwifiReciver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
Log.e(TAG, "wifi-action----->" + action);
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
setDataTypeIcon();
}
}
};
/**
* 注册网络链接状态广播
*/
public void registReceiverWifi() {
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
mContext.registerReceiver(mwifiReciver, filter);
}
/**
* 发注册网络链接状态广播
*/
public void unregistReceiverWifi() {
mContext.unregisterReceiver(mwifiReciver);
}
/**
* 是否连接 移动数据
*/
public boolean isMobile(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo MobileNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (MobileNetworkInfo.isConnected()) {
return true;
}
return false;
}
/**
* 是否连接WIFI
*/
public boolean isWifiConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetworkInfo.isConnected()) {
return true;
}
return false;
}
/**
* 热点AP 是否开启
*/
public boolean isWifiApState() {
if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
return true;
} else {
return false;
}
}
/**
* 获取移动流量数据类型 图片 2G 3G 4G
*/
private int getMobileDataTypeIconId() {
int icon = 0;
if (mServiceState != null) {
int type = mServiceState.getDataNetworkType();
Log.v(TAG, "getMobileDataTypeIconId " + type);
int c = TelephonyManager.getNetworkClass(type);
switch (c) {
case TelephonyManager.NETWORK_CLASS_2_G:// 1
icon = R.drawable.mobiledata_2g;
break;
case TelephonyManager.NETWORK_CLASS_3_G:// 2
icon = R.drawable.mobiledata_3g;
break;
case TelephonyManager.NETWORK_CLASS_4_G:// 3
icon = R.drawable.mobiledata_4g;
break;
}
}
return icon;
}
/**
* 设置网络类型
*/
private void setMobileTypeIcon() {
singType.setImageResource(mobileType);
}
/**
* 设置手机信号强度,设置图片
*/
private void setSignalStrong() {
if (hasSimCard()) {
if (hasService() && mSignalStrength != null) {
singStrong.setImageResource(getSignalIconId(signalLevel));
} else {
singStrong.setImageResource(getSignalIconId(0));
}
} else {
singStrong.setImageResource(getSignalIconId(0));
}
}
/**
* 获取当前网络类型 2G 3G 4G
*/
private int getMobileTypeIconId() {
int icon = 0;
if (mServiceState != null) {
int type = mServiceState.getDataNetworkType();
Log.v(TAG, "getMobileTypeIconId " + type);
int c = TelephonyManager.getNetworkClass(type);
Log.v(TAG, "c " + c);
switch (c) {
case TelephonyManager.NETWORK_CLASS_2_G: // 1
icon = R.drawable.mobile_type_2g;
break;
case TelephonyManager.NETWORK_CLASS_3_G:// 2
icon = R.drawable.mobile_type_3g;
break;
case TelephonyManager.NETWORK_CLASS_4_G:// 3
icon = R.drawable.mobile_type_4g;
break;
}
}
return icon;
}
/**
* 判断是否包含SIM卡
*
*/
public boolean hasSimCard() {
int simState = mPhone.getSimState();
boolean result = true;
switch (simState) {
case TelephonyManager.SIM_STATE_ABSENT:
result = false; // 没有SIM卡
break;
case TelephonyManager.SIM_STATE_UNKNOWN:
result = false;
break;
}
return result;
}
private boolean hasService() {
if (mServiceState != null) {
switch (mServiceState.getVoiceRegState()) {
case ServiceState.STATE_POWER_OFF:
return false;
case ServiceState.STATE_OUT_OF_SERVICE:
case ServiceState.STATE_EMERGENCY_ONLY:
return mServiceState.getDataRegState() == ServiceState.STATE_IN_SERVICE;
default:
return true;
}
} else {
return false;
}
}
int signal_icons[] = { R.drawable.signal_0, R.drawable.signal_1, R.drawable.signal_2, R.drawable.signal_3,
R.drawable.signal_4 };
private int getSignalIconId(int level) {
if (level < 0) {
return 0;
}
if (level >= signal_icons.length) {
return signal_icons[signal_icons.length - 1];
}
return signal_icons[level];
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregistReceiverWifi();
}
}