//utils.java
public static boolean isWifiOn(Context mContext)
{
ConnectivityManager connectivity = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi != null && mWifi.isConnected())
{
return true;
}
return false;
};
private static boolean isHasSIMCard(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);//取得相关系统服务
int state = tm.getSimState();
Log.d(TAG, "sim state: " + state);
if (state == TelephonyManager.SIM_STATE_ABSENT || state == TelephonyManager.SIM_STATE_UNKNOWN) {
return false;
}
return true;
}
public static boolean is4gOn(Context mContext)
{
return isHasSIMCard(mContext);
}
public static boolean isEthernetOn(Context mContext)
{
ConnectivityManager connectivity = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
if (info != null && info.isConnected())
{
return true;
}
else
{
return false;
}
}
//判断mobile是否可用
public static boolean isMobileConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo != null) {
return mMobileNetworkInfo.isAvailable();
}
}
return false;
}
public static int getNetType(Context context){
//-1表示无网络,1表示有线网,2表示wifi,3表示sim卡网络
int netType = -1;
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if(networkInfonull){
return netType;
}
int nType = networkInfo.getType();
if(nTypeConnectivityManager.TYPE_MOBILE){
netType = 3;
} else if(nTypeConnectivityManager.TYPE_WIFI){
netType = 2;
} else if(nTypeConnectivityManager.TYPE_ETHERNET){
netType = 1;
}
return netType;
}
//mainactivity.java
private void display4g() {
if (Utils.is4gOn(this)) {
//Utils.send_led_msg(“dotwifi1”);
if(Utils.isMobileConnected(this)){
int num =Utils.getNetType(this);
//Toast.makeText(MainActivity.this,“num=”+num,Toast.LENGTH_LONG).show();
if(num == 1||num ==2){
iv_4g.setBackgroundResource(R.drawable.vs4g_1);
}else if(num ==3){
iv_4g.setBackgroundResource(R.drawable.vs4g_2);
Toast.makeText(MainActivity.this,“The card connection is normal and you can access the Internet through the card”,Toast.LENGTH_LONG).show();
}
}else{
iv_4g.setBackgroundResource(R.drawable.vs4g_1);
Toast.makeText(MainActivity.this,“The card connection is normal, but the Internet can not be accessed through the card”,Toast.LENGTH_LONG).show();
}
Log.e(TAG,“wozhendefu”);
} else {
iv_4g.setBackgroundResource(R.drawable.vs4g);
//Utils.send_led_msg(“dotwifi0”);
Log.e(TAG,“lipu”);
}
}
//判断是否插入了sim的广播信号
else if(ACTION_SIM_STATE_CHANGED.equals(action)
// || ACTION_SIM_CARD_STATE_CHANGED.equals(action)
// || ACTION_SIM_CARRIER_CONFIG_CHANGED.equals(action)
){
display4g();
}
//添加变量
private final static String ACTION_SIM_STATE_CHANGED = “android.intent.action.SIM_STATE_CHANGED”;
//注册
filter = new IntentFilter();
filter.addAction(ACTION_SIM_STATE_CHANGED);
registerReceiver(netReceiver, filter);



