Android 通过 IMSI 判断手机运营商

 
     private static String mIMSI = null;

     public static String getIMSI (Context context) {
            if ( mIMSI == null) {
                 mIMSI = getIMSIImp(context);
           }
            return mIMSI;
     }

     /**
      * 获取IMSI,默认返回000000000000000
      *
      * @param context
      * @return
      */
     private static String getIMSIImp(Context context) {
           String imsi = null;
           TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE );
           if (tm != null && tm.getSimState() == TelephonyManager.SIM_STATE_READY ) {
                 // 取出IMSI
                imsi = tm.getSubscriberId();
           }
           if (imsi == null || imsi.equals( "")) {
                imsi = getQualcommDubleSimIMSI(context);
           }
           if (imsi == null || imsi.equals( "")) {
                imsi = initMtkDoubleSimIMSI(context);
           }
           if (imsi == null || imsi.equals( "")) {
                imsi = "000000000000000";
           }
           return imsi;

     }

     private static String initMtkDoubleSimIMSI(Context context) {
           String imsi = null;
            try {
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE );
                Class<?> c = Class.forName("com.android.internal.telephony.Phone");
                 for ( int i = 1; i < 3 && imsi == null; i++) {
                     Field fields = c.getField( "GEMINI_SIM_" + i);
                     fields.setAccessible( true);
                     Integer simID = (Integer) fields.get(null);
                     Method m = TelephonyManager.class.getDeclaredMethod("getSubscriberIdGemini" , int.class);
                     imsi = (String) m.invoke(tm, simID);
                }
           } catch (Exception e) {

           }
            return imsi;
     }

     /**
      * 双卡情况 ,获取IMSI
      *
      * @param context
      * @return
      */
     private static String getQualcommDubleSimIMSI(Context context) {
           String imsi = null;
            try {
                Class<?> telphoneCls = Class.forName("android.telephony.MSimTelephonyManager");
                Object obj = context.getSystemService("phone_msim" );
                Method ms = telphoneCls.getMethod("getSubscriberId" , int.class);
                 int simId_1 = 0;
                 int simId_2 = 1;
                imsi = (String) ms.invoke(obj, simId_1);
                 if (imsi == null || imsi.equals( "")) {
                     imsi = (String) ms.invoke(obj, simId_2);
                }
           } catch (Exception e) {

           }
            return imsi;
     }

     /**
      * 通过IMSI判断手机运营商
      *
      * @param context
      * @return ISP_TYPE_?
      */
     public static String getISP(Context context) {
           String imsi = getIMSI(context);
            if (imsi.startsWith( "46000") || imsi.startsWith("46002") || imsi.startsWith("46007" )) {
                 return ISP_TYPE_CHINAMOBILE;
           } else if (imsi.startsWith( "46001")) {
                 return ISP_TYPE_CHINAUNICOM;
           } else if (imsi.startsWith( "46003") || imsi.startsWith("46005")) {
                 return ISP_TYPE_CHINATELECOM;
           }
            return ISP_TYPE_NONE;
     }


你可能感兴趣的:(android)