获取SIM卡信息

在很多时候,大家可能要去取到SIM的信息,比如当前插入了几张卡,或者卡在那个卡槽里(在双卡槽的时候),读取SIM卡的号码,或者判断是联通的、移动的还是电信的等等,现在就将源码中的SIM卡信息的一部分与大家分享之。(java代码如下)

 static class SIMInfo {
  public long mSimId;
  public String mICCId;
  public String mDisplayName = "";
  public String mNumber = "";
  public int mDispalyNumberFormat = SimInfo.DISLPAY_NUMBER_DEFAULT;
  public int mColor;
  public int mDataRoaming = SimInfo.DATA_ROAMING_DEFAULT;
  public int mSlot = SimInfo.SLOT_NONE;

  private SIMInfo() {
  }

  public static class ErrorCode {
   public static final int ERROR_GENERAL = -1;
   public static final int ERROR_NAME_EXIST = -2;
  }

  private static SIMInfo fromCursor(Cursor cursor) {
   SIMInfo info = new SIMInfo();
   info.mSimId = cursor.getLong(cursor
     .getColumnIndexOrThrow(SimInfo._ID));
   info.mICCId = cursor.getString(cursor
     .getColumnIndexOrThrow(SimInfo.ICC_ID));
   info.mDisplayName = cursor.getString(cursor
     .getColumnIndexOrThrow(SimInfo.DISPLAY_NAME));
   info.mNumber = cursor.getString(cursor
     .getColumnIndexOrThrow(SimInfo.NUMBER));
   info.mDispalyNumberFormat = cursor.getInt(cursor
     .getColumnIndexOrThrow(SimInfo.DISPLAY_NUMBER_FORMAT));
   info.mColor = cursor.getInt(cursor
     .getColumnIndexOrThrow(SimInfo.COLOR));
   info.mDataRoaming = cursor.getInt(cursor
     .getColumnIndexOrThrow(SimInfo.DATA_ROAMING));
   info.mSlot = cursor.getInt(cursor
     .getColumnIndexOrThrow(SimInfo.SLOT));
   return info;
  }

  /**
   *
   * @param ctx
   * @return the array list of Current SIM Info
   */
  public static List<SIMInfo> getInsertedSIMList(Context ctx) {
   ArrayList<SIMInfo> simList = new ArrayList<SIMInfo>();
   Cursor cursor = ctx.getContentResolver().query(SimInfo.CONTENT_URI,
     null, SimInfo.SLOT + "!=" + SimInfo.SLOT_NONE, null, null);
   try {
    if (cursor != null) {
     while (cursor.moveToNext()) {
      simList.add(SIMInfo.fromCursor(cursor));
     }
    }
   } finally {
    if (cursor != null) {
     cursor.close();
    }
   }
   return simList;
  }

  /**
   *
   * @param ctx
   * @return array list of all the SIM Info include what were used before
   */
  public static List<SIMInfo> getAllSIMList(Context ctx) {
   ArrayList<SIMInfo> simList = new ArrayList<SIMInfo>();
   Cursor cursor = ctx.getContentResolver().query(SimInfo.CONTENT_URI,
     null, null, null, null);
   try {
    if (cursor != null) {
     while (cursor.moveToNext()) {
      simList.add(SIMInfo.fromCursor(cursor));
     }
    }
   } finally {
    if (cursor != null) {
     cursor.close();
    }
   }
   return simList;
  }

  /**
   *
   * @param ctx
   * @param SIMId
   *            the unique SIM id
   * @return SIM-Info, maybe null
   */
  public static SIMInfo getSIMInfoById(Context ctx, long SIMId) {
   if (SIMId <= 0)
    return null;
   Cursor cursor = ctx.getContentResolver().query(
     ContentUris.withAppendedId(SimInfo.CONTENT_URI, SIMId),
     null, null, null, null);
   try {
    if (cursor != null) {
     if (cursor.moveToFirst()) {
      return SIMInfo.fromCursor(cursor);
     }
    }
   } finally {
    if (cursor != null) {
     cursor.close();
    }
   }
   return null;
  }

  /**
   *
   * @param ctx
   * @param SIMName
   *            the Name of the SIM Card
   * @return SIM-Info, maybe null
   */
  public static SIMInfo getSIMInfoByName(Context ctx, String SIMName) {
   if (SIMName == null)
    return null;
   Cursor cursor = ctx.getContentResolver().query(SimInfo.CONTENT_URI,
     null, SimInfo.DISPLAY_NAME + "=?",
     new String[] { SIMName }, null);
   try {
    if (cursor != null) {
     if (cursor.moveToFirst()) {
      return SIMInfo.fromCursor(cursor);
     }
    }
   } finally {
    if (cursor != null) {
     cursor.close();
    }
   }
   return null;
  }

  /**
   * @param ctx
   * @param cardSlot
   * @return The SIM-Info, maybe null
   */
  public static SIMInfo getSIMInfoBySlot(Context ctx, int cardSlot) {
   if (cardSlot < 0)
    return null;
   Cursor cursor = ctx.getContentResolver().query(SimInfo.CONTENT_URI,
     null, SimInfo.SLOT + "=?",
     new String[] { String.valueOf(cardSlot) }, null);
   try {
    if (cursor != null) {
     if (cursor.moveToFirst()) {
      return SIMInfo.fromCursor(cursor);
     }
    }
   } finally {
    if (cursor != null) {
     cursor.close();
    }
   }
   return null;
  }

  /**
   * @param ctx
   * @param iccid
   * @return The SIM-Info, maybe null
   */
  public static SIMInfo getSIMInfoByICCId(Context ctx, String iccid) {
   if (iccid == null)
    return null;
   Cursor cursor = ctx.getContentResolver().query(SimInfo.CONTENT_URI,
     null, SimInfo.ICC_ID + "=?", new String[] { iccid }, null);
   try {
    if (cursor != null) {
     if (cursor.moveToFirst()) {
      return SIMInfo.fromCursor(cursor);
     }
    }
   } finally {
    if (cursor != null) {
     cursor.close();
    }
   }
   return null;
  }

  /**
   * @param ctx
   * @param SIMId
   * @return the slot of the SIM Card, -1 indicate that the SIM card is
   *         missing
   */
  public static int getSlotById(Context ctx, long SIMId) {
   if (SIMId <= 0)
    return SimInfo.SLOT_NONE;
   Cursor cursor = ctx.getContentResolver().query(
     ContentUris.withAppendedId(SimInfo.CONTENT_URI, SIMId),
     new String[] { SimInfo.SLOT }, null, null, null);
   try {
    if (cursor != null) {
     if (cursor.moveToFirst()) {
      return cursor.getInt(0);
     }
    }
   } finally {
    if (cursor != null) {
     cursor.close();
    }
   }
   return SimInfo.SLOT_NONE;
  }

  /**
   * @param ctx
   * @param SIMName
   * @return the slot of the SIM Card, -1 indicate that the SIM card is
   *         missing
   */
  public static int getSlotByName(Context ctx, String SIMName) {
   if (SIMName == null)
    return SimInfo.SLOT_NONE;
   Cursor cursor = ctx.getContentResolver().query(SimInfo.CONTENT_URI,
     new String[] { SimInfo.SLOT }, SimInfo.DISPLAY_NAME + "=?",
     new String[] { SIMName }, null);
   try {
    if (cursor != null) {
     if (cursor.moveToFirst()) {
      return cursor.getInt(0);
     }
    }
   } finally {
    if (cursor != null) {
     cursor.close();
    }
   }
   return SimInfo.SLOT_NONE;
  }

  /**
   * @param ctx
   * @return current SIM Count
   */
  public static int getInsertedSIMCount(Context ctx) {
   Cursor cursor = ctx.getContentResolver().query(SimInfo.CONTENT_URI,
     null, SimInfo.SLOT + "!=" + SimInfo.SLOT_NONE, null, null);
   try {
    if (cursor != null) {
     return cursor.getCount();
    }
   } finally {
    if (cursor != null) {
     cursor.close();
    }
   }
   return 0;
  }

  /**
   * @param ctx
   * @return the count of all the SIM Card include what was used before
   */
  public static int getAllSIMCount(Context ctx) {
   Cursor cursor = ctx.getContentResolver().query(SimInfo.CONTENT_URI,
     null, null, null, null);
   try {
    if (cursor != null) {
     return cursor.getCount();
    }
   } finally {
    if (cursor != null) {
     cursor.close();
    }
   }
   return 0;
  }

  /**
   * set display name by SIM ID
   *
   * @param ctx
   * @param displayName
   * @param SIMId
   * @return -1 means general error, -2 means the name is exist. >0 means
   *         success
   */
  public static int setDisplayName(Context ctx, String displayName,
    long SIMId) {
   if (displayName == null || SIMId <= 0)
    return ErrorCode.ERROR_GENERAL;
   Cursor cursor = ctx.getContentResolver().query(SimInfo.CONTENT_URI,
     new String[] { SimInfo._ID }, SimInfo.DISPLAY_NAME + "=?",
     new String[] { displayName }, null);
   try {
    if (cursor != null) {
     if (cursor.getCount() > 0) {
      return ErrorCode.ERROR_NAME_EXIST;
     }
    }
   } finally {
    if (cursor != null) {
     cursor.close();
    }
   }
   ContentValues value = new ContentValues(1);
   value.put(SimInfo.DISPLAY_NAME, displayName);
   return ctx.getContentResolver().update(
     ContentUris.withAppendedId(SimInfo.CONTENT_URI, SIMId),
     value, null, null);
  }

  /**
   * @param ctx
   * @param number
   * @param SIMId
   * @return >0 means success
   */
  public static int setNumber(Context ctx, String number, long SIMId) {
   if (number == null || SIMId <= 0)
    return -1;
   ContentValues value = new ContentValues(1);
   value.put(SimInfo.NUMBER, number);
   return ctx.getContentResolver().update(
     ContentUris.withAppendedId(SimInfo.CONTENT_URI, SIMId),
     value, null, null);
  }

  /**
   *
   * @param ctx
   * @param color
   * @param SIMId
   * @return >0 means success
   */
  /**
   * set the format.0: none, 1: the first four digits, 2: the last four
   * digits.
   *
   * @param ctx
   * @param format
   * @param SIMId
   * @return >0 means success
   */
  public static int setDispalyNumberFormat(Context ctx, int format,
    long SIMId) {
   if (format < 0 || SIMId <= 0)
    return -1;
   ContentValues value = new ContentValues(1);
   value.put(SimInfo.DISPLAY_NUMBER_FORMAT, format);
   return ctx.getContentResolver().update(
     ContentUris.withAppendedId(SimInfo.CONTENT_URI, SIMId),
     value, null, null);
  }

  /**
   * set data roaming.0:Don't allow data when roaming, 1:Allow data when
   * roaming
   *
   * @param ctx
   * @param roaming
   * @param SIMId
   * @return >0 means success
   */
  public static int setDataRoaming(Context ctx, int roaming, long SIMId) {
   if (roaming < 0 || SIMId <= 0)
    return -1;
   ContentValues value = new ContentValues(1);
   value.put(SimInfo.DATA_ROAMING, roaming);
   return ctx.getContentResolver().update(
     ContentUris.withAppendedId(SimInfo.CONTENT_URI, SIMId),
     value, null, null);
  }

  /**
   * Insert the ICC ID and slot if needed
   *
   * @param ctx
   * @param ICCId
   * @param slot
   * @return
   */
  public static Uri insertICCId(Context ctx, String ICCId, int slot) {
   if (ICCId == null) {
    throw new IllegalArgumentException("ICCId should not null.");
   }
   Uri uri;
   ContentResolver resolver = ctx.getContentResolver();
   String selection = SimInfo.ICC_ID + "=?";
   Cursor cursor = resolver.query(SimInfo.CONTENT_URI, new String[] {
     SimInfo._ID, SimInfo.SLOT }, selection,
     new String[] { ICCId }, null);
   try {
    if (cursor == null || !cursor.moveToFirst()) {
     ContentValues values = new ContentValues();
     values.put(SimInfo.ICC_ID, ICCId);
     values.put(SimInfo.COLOR, -1);
     values.put(SimInfo.SLOT, slot);
     uri = resolver.insert(SimInfo.CONTENT_URI, values);
     // setDefaultName(ctx, ContentUris.parseId(uri), null);
    } else {
     long simId = cursor.getLong(0);
     int oldSlot = cursor.getInt(1);
     uri = ContentUris
       .withAppendedId(SimInfo.CONTENT_URI, simId);
     if (slot != oldSlot) {
      ContentValues values = new ContentValues(1);
      values.put(SimInfo.SLOT, slot);
      resolver.update(uri, values, null, null);
     }
    }
   } finally {
    if (cursor != null) {
     cursor.close();
    }
   }

   return uri;
  }

  private static String getSuffixFromIndex(int index) {
   if (index < 10) {
    return "0" + index;
   } else {
    return String.valueOf(index);
   }

  }

  public static final class SimInfo implements BaseColumns {
   public static final Uri CONTENT_URI = Uri
     .parse("content://telephony/siminfo");

   public static final String DEFAULT_SORT_ORDER = "name ASC";

   /**
    * <P>
    * Type: TEXT
    * </P>
    */
   public static final String ICC_ID = "icc_id";
   /**
    * <P>
    * Type: TEXT
    * </P>
    */
   public static final String DISPLAY_NAME = "display_name";
   public static final int DEFAULT_NAME_MIN_INDEX = 01;
   public static final int DEFAULT_NAME_MAX_INDEX = 99;
   /**
    * <P>
    * Type: TEXT
    * </P>
    */
   public static final String NUMBER = "number";

   /**
    * 0:none, 1:the first four digits, 2:the last four digits.
    * <P>
    * Type: INTEGER
    * </P>
    */
   public static final String DISPLAY_NUMBER_FORMAT = "display_number_format";
   public static final int DISPALY_NUMBER_NONE = 0;
   public static final int DISPLAY_NUMBER_FIRST = 1;
   public static final int DISPLAY_NUMBER_LAST = 2;
   public static final int DISLPAY_NUMBER_DEFAULT = DISPLAY_NUMBER_FIRST;

   /**
    * Eight kinds of colors. 0-7 will represent the eight colors.
    * Default value: any color that is not in-use.
    * <P>
    * Type: INTEGER
    * </P>
    */
   public static final String COLOR = "color";
   public static final int COLOR_1 = 0;
   public static final int COLOR_2 = 1;
   public static final int COLOR_3 = 2;
   public static final int COLOR_4 = 3;
   public static final int COLOR_5 = 4;
   public static final int COLOR_6 = 5;
   public static final int COLOR_7 = 6;
   public static final int COLOR_8 = 7;
   public static final int COLOR_DEFAULT = COLOR_1;

   /**
    * 0: Don't allow data when roaming, 1:Allow data when roaming
    * <P>
    * Type: INTEGER
    * </P>
    */
   public static final String DATA_ROAMING = "data_roaming";
   public static final int DATA_ROAMING_ENABLE = 1;
   public static final int DATA_ROAMING_DISABLE = 0;
   public static final int DATA_ROAMING_DEFAULT = DATA_ROAMING_DISABLE;

   /**
    * <P>
    * Type: INTEGER
    * </P>
    */
   public static final String SLOT = "slot";
   public static final int SLOT_NONE = -1;

   public static final int ERROR_GENERAL = -1;
   public static final int ERROR_NAME_EXIST = -2;

  }
 }

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值