SIM 카드의 고유 ID 가져오기

5072 단어 작업
    IMSI      
Android 5.0  google       ,               sim      ,  5.0         ,               。
    : tManager.getSubscriberId();
                IMSI
     :
public static String getPhoneImsiNum(Context context) {
        int subId1 = -1;
        int subId2 = -1;
        String imsi1 = null;
        String imsi2 = null;
        try {
            TelephonyManager tManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //    Android 5.0 L  
                Method getSubscriberId = tManager.getClass().getMethod("getSubscriberId", int.class);

                ContentResolver contentResolver = context.getContentResolver();
                Cursor c = contentResolver.query(Uri.parse("content://telephony/siminfo"),
                        new String[]{"_id"}, "sim_id" + " = ?", new String[]{"0"}, null);
                if (null != c && c.moveToFirst()) {
                    subId1 = c.getInt(c.getColumnIndexOrThrow("_id"));
                    Log.d("PhoneUtil", "subId1:" + subId1);
                    c.close();
                }

                c = contentResolver.query(Uri.parse("content://telephony/siminfo"),
                        new String[]{"_id"}, "sim_id" + " = ?", new String[]{"1"}, null);
                if (null != c && c.moveToFirst()) {
                    subId2 = c.getInt(c.getColumnIndexOrThrow("_id"));
                    Log.d("PhoneUtil", "subId2:" + subId2);
                    c.close();
                }

                if (subId1 > 0) {
                    imsi1 = (String) getSubscriberId.invoke(tManager, subId1);
                }
                if (subId2 > 0) {
                    imsi2 = (String) getSubscriberId.invoke(tManager, subId2);
                }

                if (!TextUtils.isEmpty(imsi1) && !TextUtils.isEmpty(imsi2)) {
                    return imsi1 + "," + imsi2;
                } else {
                    if (!TextUtils.isEmpty(imsi1)) {
                        return imsi1;
                    } else {
                        return imsi2;
                    }
                }
            } else { //Android 5.0   api  ismi   sdk < 21
                return tManager.getDeviceId();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
      ,            ,  ...            ,    api              ....  

          :
/***
     * sim_id   0   1
     *   sim_id   subId      subid       imsi  sim_id  subid      ,    simid   subid         
	               subid        
     * @return
     */
    public int getSubId(int simid,Context context) {
        Uri uri = Uri.parse("content://telephony/siminfo");
        Cursor cursor = null;
        ContentResolver contentResolver = context.getContentResolver();
        try {
            cursor = contentResolver.query(uri, new String[]{"_id", "sim_id"}, "sim_id = ?",
                    new String[]{String.valueOf(simid)}, null);
            if (null != cursor) {
                if (cursor.moveToFirst()) {
                    return cursor.getInt(cursor.getColumnIndex("_id"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != cursor) {
                cursor.close();
            }
        }
        return -1;
    }
  。。。   imsi   sim      ,   5.1.1         sub  SubscriptionInfo             iccid         。。   api      。。。     5.1          :
  @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
    public static String getSimIccId(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { //    Android 5.1.0 L  
            SubscriptionManager sub = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
            List info = sub.getActiveSubscriptionInfoList();
            int count = sub.getActiveSubscriptionInfoCount();
            if (count > 0) {
                if (count > 1) {
                    String icc1 = info.get(0).getIccId();
                    String icc2 = info.get(1).getIccId();
                    return icc1 + "," + icc2;
                } else {
                    for (SubscriptionInfo list : info) {
                        String icc1 = list.getIccId();
                        return icc1;
                    }
                }
            } else {
                Log.d("PhoneUtil", " SIM ");
                return "";
            }
        } else { //  5.1.0      
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            return tm.getSimSerialNumber();
        }
        return "";
    }

좋은 웹페이지 즐겨찾기