Android 雙卡獲取當前使用流量在線卡的信息


最近接觸了一個項目,需要獲取在線流量卡的信息,下面的方式,可以獲取大部分手機的正確手機卡信息。

一  獲取獲取IMEI

 
         
public static String getDeviced(int soltId,Context context) {
return (String) getPhoneInfo(soltId,"getDeviceId", context);
}

    獲取 IMSI 

 public static String getSubscriberId(int subId, Context context) {
        String imsi = (String) getPhoneInfo(subId, "getSubscriberId", context);
        return imsi;
    }

獲取 iccid 

 public static String getSimSerialNumber(int subId, Context context) {
        String imsi = (String) getPhoneInfo(subId, "getSimSerialNumber", context);
        return imsi;
    }

subId 為卡槽 id 

二、雙卡的時候獲取哪個卡是使用流量的卡

 @SuppressLint("MissingPermission")
    public static Integer getDefaultDataSubId(Context context)
    {
        Integer id = -1;


        try {
            SubscriptionManager sm = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
                sm = SubscriptionManager.from(context.getApplicationContext());
                Method getSubId = sm.getClass().getMethod("getDefaultDataSubId");
                if(getSubId != null)
                {
                    id = (int) getSubId.invoke(sm);
                }
            }
        } catch (NoSuchMethodException e) {
            try {
                SubscriptionManager sm = null;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
                    sm = SubscriptionManager.from(context.getApplicationContext());
                    Method getSubId = sm.getClass().getMethod("getDefaultDataSubscrptionId");
                    if(getSubId != null)
                    {
                        id = (int) getSubId.invoke(sm);
                    }
                }
            } catch (NoSuchMethodException e1) {
//
                try {
                    SubscriptionManager sm = null;
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
                        sm = SubscriptionManager.from(context.getApplicationContext());
                        Method getSubId = sm.getClass().getMethod("getDefaultDataPhoneId");
//            Method getSubId = Class.forName("android.telephony.SubscriptionManager").getDeclaredMethod("getSubId", new Class[]{Integer.TYPE});
                        if(getSubId != null)
                        {
                            id = (int) getSubId.invoke(sm);
                           Log.v("",(int) getSubId.invoke(sm) + "");
                        }
                    }
                } catch (NoSuchMethodException e2) {
                    e.printStackTrace();
                } catch (IllegalAccessException e2) {
                    e.printStackTrace();
                } catch (InvocationTargetException e2) {
                    e.printStackTrace();
                }
            } catch (IllegalAccessException e1) {
                e.printStackTrace();
            } catch (InvocationTargetException e1) {
                e.printStackTrace();
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return  id;

    }

      使用 getDefaultDataSubId  可以獲取大部分手機哪個是在線的數據的卡 ,getDefaultDataSubscrptionId 是榮耀系列的手機能獲取到,但小米、紅米.vivo 等手機的獲取不到。

     上面兩個可以獲取大部分的,三星的好像不行,三星的把副卡的數據流量打開時,會自動關閉主卡。三星的使用 這個getDefaultDataPhoneId  , 這個可以獲取到三星的哪個手機卡在線嗎,但是當使用上面的獲取SIMI 的方法時,兩個卡,返回的都是主卡的值。

    對三星的手機做了一下特殊處理:

  

  if ( Utils.getSubscriberId(0,mContext).equals( Utils.getSubscriberId(1,mContext))) {
                if (id == 1) {
                    imsi=SystemUtil.getPhoneIMSI(mContext);
                    imei=SystemUtil.getIMEI(mContext);
                    iccid=SystemUtil.getPhoneICCID(mContext);
                }else {
                    imsi=Utils.getSubscriberId(id,mContext);
                    imei=Utils.getDeviced(id,mContext);
                    iccid=Utils.getSimSerialNumber(id,mContext);
                }

            }else {
                imsi=Utils.getSubscriberId(id,mContext);
                imei=Utils.getDeviced(id,mContext);
                iccid=Utils.getSimSerialNumber(id,mContext);
            }

當傳 0、 1 時獲取到的SIMI 信息一樣時

獲取副卡信息 使用 下面的方法

 @SuppressLint("MissingPermission")
    public static String getPhoneIMSI(Context context) {
        TelephonyManager mTelephonyMgr = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        String str = "";
         try {
             str=mTelephonyMgr.getSubscriberId();
         }catch (Exception e) {
             str = "";
         }

        return str;
    }
 @SuppressLint("MissingPermission")
    public static String getIMEI(Context ctx) {
        TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
        if (tm != null) {
            try {
                return tm.getDeviceId();
            }catch (Exception e) {
                return "";
            }

        }
        return null;
    }
 @SuppressLint("MissingPermission")
    public static String getPhoneICCID(Context context) {
        TelephonyManager mTelephonyMgr = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        try {
            return mTelephonyMgr.getSimSerialNumber();
        }catch (Exception e) {
            return "";
        }

    }

獲取主卡信息 ,還可以使用  一 標題里面的方法。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM