判断SIM卡状态,获取SIM卡信息

时间:2024-03-13 21:47:10

public String resdSIM(){
     //取得相关服务
     TelephonyManager tm = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);
     
     StringBuffer sb = new StringBuffer();
     switch(tm.getSimState()){
     case TelephonyManager.SIM_STATE_ABSENT: //无卡
           sb.append("无卡");
      break;
     case TelephonyManager.SIM_STATE_UNKNOWN: //未知状态
           sb.append("未知状态");
      break;
     case TelephonyManager.SIM_STATE_NETWORK_LOCKED: //需要networkpin解锁
           sb.append("需要networkpin解锁");
      break;
     case TelephonyManager.SIM_STATE_PIN_REQUIRED: // 需要pin解锁
           sb.append("需要pin解锁");
      break;
     case TelephonyManager.SIM_STATE_PUK_REQUIRED: //需要puk解锁
           sb.append("需要puk解锁");
      break;
     case TelephonyManager.SIM_STATE_READY: //良好
           sb.append("良好");
      break;
     }
     

     以下代码需要 READ_PHONE_STATE 权限

     <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

 

     if(tm.getSimSerialNumber() != null){
           sb.append("@" + tm.getSimSerialNumber().toString());
     }else{
           sb.append("@无法取得sim卡号");
     }
     
     if(tm.getSimOperator().equals("")){
           sb.append("@无法取得供货商代码");
     }else{
           sb.append("@" + tm.getSimOperator().toString());
     }
     
     if(tm.getSimOperatorName().equals("")){
           sb.append("@无法取得供货商");
     }else{
           sb.append("@" + tm.getSimOperator().toString());
     }
     
     if(tm.getSimCountryIso().equals("")){
           sb.append("@无法取得国籍");
     }else{
           sb.append("@" + tm.getSimCountryIso());
     }
     
     if(tm.getNetworkOperator().equals("")){
           sb.append("@无法取得网络运营商");
     }else{
           sb.append("@" + tm.getNetworkOperator().toString());
     }
     
     if(tm.getNetworkOperatorName().equals("")){
           sb.append("@无法取得网络运营商名称");
     }else{
           sb.append("@" + tm.getNetworkOperatorName().toString());
     }
     
     if(tm.getNetworkType() == 0){
           sb.append("@无法取得网络类型");
     }else{
           sb.append("@" + tm.getNetworkType());
     }
     
     return sb.toString();
    }