android 双卡开发获取sim卡默认数据卡,获取sim卡信息,sim1卡,sim2卡 2G/3G/4G信号强度

1:默认数据卡

private int getDefalutDataID(){
    SubscriptionManager subscriptionManager = (SubscriptionManager)getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    int subscriberId = 0;
    if (Build.VERSION.SDK_INT > 24) {
        subscriberId = SubscriptionManager.getDefaultDataSubscriptionId();
    }else{
        try {
            Class cls  =  SubscriptionManager.class.getClass();
            Method method = cls.getDeclaredMethod("getDefaultDataSubId");
            subscriberId = (Integer) method.invoke(subscriptionManager);
        }catch (Exception e){e.printStackTrace();}
    }
    return subscriberId;
}
返回值1是卡一返回2是卡2,24以上不需要使用反射

2:获取sim卡信息,1中获取到的sim卡subId为参数,获取对应的SubscriptionInfo实例,可以获取到sim卡的信息

private SubscriptionInfo getSIMInfo(int subId){
    SubscriptionManager subscriptionManager = (SubscriptionManager)getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    List infos = subscriptionManager.getActiveSubscriptionInfoList();
    SubscriptionInfo infoRet = null;
    for (SubscriptionInfo info:infos){
        Log.e("xubaipei",info.toString());
        if (info.getSubscriptionId() ==  subId){
            infoRet = info;
        }
    }
    return infoRet;
}

3:sim1,sim2监听信号强度

new 出2个PhoneStateListener实例来监听sim卡1和卡2,new出的mListener

通过反射改变实例内的mSubId 属性来切换监听的sim卡信号强度,subscriberId 为1是卡1,2是卡2 ,一般是函数getDefaultDataId()中获取的默认数据卡

try {
    Field field = PhoneStateListener.class.getDeclaredField("mSubId");
    field.setAccessible(true);
    field.setInt(mListener,subscriberId);
}catch (Exception e){
    e.printStackTrace();
}

可以通过获取signalStrength中toString 的值来获取所有的信号强度,也可以像我这样获取所有的CellInfo来逐个判断

2G,3G信号,2G/3G信号强度符合dbm = --113 + 2*asu;4G信号强度符合asu = 140 +dmb

mListener = new PhoneStateListener() {

    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        if (isHidden()) {
            return;
        }
        int level = 0;
        int dbm = 0;
        int asu = 0;

        List infos = telManager.getAllCellInfo();
        if (infos == null){return;}
        for (CellInfo info :infos){
            if (!info.isRegistered()){
                continue;
            }
            if (info instanceof CellInfoLte){//4G
                CellInfoLte ff = (CellInfoLte)info;
                dbm = ff.getCellSignalStrength().getDbm();
                level = ff.getCellSignalStrength().getLevel();
                asu = 140 + dbm;
            }else
            if (info instanceof CellInfoWcdma){//3G
                CellInfoWcdma ff = (CellInfoWcdma)info;
                dbm = ff.getCellSignalStrength().getDbm();
                level = ff.getCellSignalStrength().getLevel();
                asu = (dbm + 113) / 2;
            }else
            if (info instanceof  CellInfoGsm){//2G
                CellInfoGsm ff = (CellInfoGsm)info;
                dbm = ff.getCellSignalStrength().getDbm();
                level = ff.getCellSignalStrength().getLevel();
                asu = (dbm + 113) / 2;
            }
            mSignalView.setProgress(level * 25);
            if (dbm == 0){
                return;
            }
            mSignalText.setText(String.valueOf(asu));
        }
    }
};


开始监听信号:


telManager.listen(mListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

你可能感兴趣的:(android-开发,android,framework)