package com.jqyd.android.module.lbs.util;
import android.content.Context;
import android.hardware.Camera;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.Log;
public class CheckState_interface {
public static final int NETWORK_CLASS_UNKNOWN = 0;
public static final int NETWORK_CLASS_2_G = 1;
public static final int NETWORK_CLASS_3_G = 2;
public static final int NETWORK_CLASS_4_G = 3;
private Context context;
private LocationManager locMgr;
public CheckState_interface(Context context){
this.context = context;
}
public int checkWifiAndGPRS() {
Boolean WIFI = false;
Boolean GPRS = false;
int netState = 4;
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo networkInfo = connectivity
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (false == networkInfo.isConnectedOrConnecting()) {
WIFI = false;
} else {
WIFI = true;
}
NetworkInfo gprsInfo = connectivity
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (false == gprsInfo.isConnectedOrConnecting()) {
GPRS = false;
} else {
GPRS = true;
}
}
if (WIFI == true && GPRS == true) {
netState = 1;
} else if (WIFI == false && GPRS == true) {
netState = 2;
} else if (WIFI == true && GPRS == false) {
netState = 3;
} else if (WIFI == false && GPRS == false) {
netState = 4;
}
return netState;
}
public int getNetworkClass() {
int networkType;
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
networkType = tm.getNetworkType();
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return NETWORK_CLASS_2_G;
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
return NETWORK_CLASS_3_G;
case TelephonyManager.NETWORK_TYPE_LTE:
return NETWORK_CLASS_4_G;
default:
return NETWORK_CLASS_UNKNOWN;
}
}
/**
* 检测网络状况
* @return
*/
public boolean checkConnection() {
boolean netState = false;//网络连接失败!
if(context!=null){
@SuppressWarnings("static-access")
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
NetworkInfo networkinfo = connectivityManager.getActiveNetworkInfo();
Log.i("NET", "网络监测:" + (connectivityManager == null) + ","+ (networkinfo == null));
if (connectivityManager != null && networkinfo != null) {
netState = true;//当前网络可用
//new Toast(Context.this).makeText(context.this, "检查到没有可用的网络,请打开网络连接!",
// Toast.LENGTH_LONG).show();
}
}
return netState;
}
/**
* 检测移动网络状态
* 0--关闭 1--开启
*/
public int checkNet(){
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
State mobile = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
if(State.CONNECTED==mobile){
return 1;
}
return 0;
}
/**
* 检测WiFi状态
* @return 0--关闭 1--开启
* WIFI_STATE_DISABLED 1 WIFI网卡不可用
WIFI_STATE_DISABLING 0 WIFI正在关闭
WIFI_STATE_ENABLED 3 WIFI网卡可用
WIFI_STATE_ENABLING 2 WIFI网卡正在打开
WIFI_STATE_UNKNOWN 4 未知网卡状态
*/
public int checkWiFi(){
WifiManager wifiManager=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
switch (wifiManager.getWifiState()) {
case 0:
return 0;
case 1:
return 0;
case 2:
return 1;
case 3:
return 1;
case 4:
return 0;
default:
break;
};
return 0;
}
/**
* 检查GPS状态
*/
public int checkGpState() {
int gpsResult = 0;//0、未打开1、已打开
//部分手机在调用GPS时有问题,故做异常处理。例:酷派
try {
locMgr = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
if (!locMgr.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
gpsResult = 0;
}
if (locMgr.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
gpsResult = 1;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return gpsResult;
}
/**
* 检查SIM卡是否可用
*/
public int checkSimState() {
int simResult = 0;//0、SIM卡不可用1、无法读取SIM卡状态2、SIM卡可用
TelephonyManager telPhoneMgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE); // 检查Sim卡状态 int
int simState = telPhoneMgr.getSimState();
Log.i("SIM", String.valueOf(simState).toString());
Log.i("SIM", "TelephonyManager.SIM_STATE_READY:"+TelephonyManager.SIM_STATE_READY);
Log.i("SIM", "TelephonyManager.SIM_STATE_ABSENT:"+TelephonyManager.SIM_STATE_ABSENT);
final int sim_ready = TelephonyManager.SIM_STATE_READY;
final int sim_absent = TelephonyManager.SIM_STATE_ABSENT;
switch(simState){
case sim_ready:
//可用
simResult = 0;
break;
case sim_absent:
//simStateAlter = "SIM卡未找到,请重新装入SIM卡,或者更换新卡!";
simResult = 1;
break;
default:
//simStateAlter = "读取SIM卡失败,请重新装入SIM卡,或者更换新卡!";
simResult = 2;
}
return simResult;
}
/**
* 检查相机是否可用
*/
public int checkCameraState() {
int result = 1;//0、不可用1、可用
Camera mCamera = null;
try {
mCamera = Camera.open();
mCamera.release();
mCamera = null;
} catch (Exception e) {
result = 0;
}
return result;
}
}