package com.jqmobile.core.android.lbs.base; import android.content.Context; import android.hardware.Camera; import android.location.LocationManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.telephony.TelephonyManager; import android.util.Log; public class CheckState_interface { private Context context; private LocationManager locMgr; public CheckState_interface(Context context){ this.context = context; } /** * 检测网络状况 * @return */ public boolean checkConnection() { boolean netState = false;//网络连接失败! @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; } /** * 检查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; } }