代码如下:
/**
* 检查sim卡是否存在
* @return
*/
private boolean checkPhoneNet(){
TelephonyManager mTelephonyManager=(TelephonyManager) this.getSystemService(Service.TELEPHONY_SERVICE);
if(mTelephonyManager.getSimState()!=TelephonyManager.SIM_STATE_READY) //SIM卡没有就绪
{
MessageBox.alert(getApplicationContext(), "SIM卡没有就绪");
return false;
}else{
ConnectivityManager cManager=(ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cManager.getActiveNetworkInfo();
if (info != null && info.isAvailable()){
//能联网
// MessageBox.alert(getApplicationContext(), "能联网");
return true;
}else{
//do something
MessageBox.alert(getApplicationContext(), "不能联网");
//不能联网
return false;
}
}
}
/**
* 判断手机是否支持NFC功能和OpenMobileAPI
* */
private boolean isSupportMocam() {
boolean isSupportMocam = false;
// 判断手机是否有NFC功能
PackageManager pm = getPackageManager();
boolean hasNfcFeature = pm.hasSystemFeature(PackageManager.FEATURE_NFC);
// 如果手机支持NFC,尝试能否加载OpenMobile API的类
if (hasNfcFeature) {
try {
Class.forName("org.simalliance.openmobileapi.SEService");
isSupportMocam = true;
} catch (ClassNotFoundException e) {
e.printStackTrace();
MessageBox.alert(getApplicationContext(), "加载OpenMobile失败");
Log.d(TAG,"Class not Found:org.simalliance.openmobileapi.SEService");
}
}else{
MessageBox.alert(getApplicationContext(), "您的手机不支持NFC");
}
return isSupportMocam;
}
/**
* 判断NFC是否打开
*
* @return
*/
public boolean hasNFC(){
NfcManager manager = (NfcManager) this.getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
if (adapter != null && adapter.isEnabled()) {
// adapter存在,能启用
// Toast.makeText(getApplicationContext(), "您的手机NFC已打开", 1).show();
return true;
}
MessageBox.alert(getApplicationContext(), "您的手机没有打开NFC");
return false;
}
注释:MessageBox.alert是一个表示弹出对话框,一个封装工具