糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > android 双卡开发获取sim卡默认数据卡 获取sim卡信息 sim1卡 sim2卡 2G/3G/4G信号强度

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

时间:2023-10-31 17:53:56

相关推荐

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<SubscriptionInfo> 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<CellInfo> infos = telManager.getAllCellInfo(); if (infos == null){return;}for (CellInfo info :infos){if (!info.isRegistered()){continue; }if (info instanceof CellInfoLte){//4GCellInfoLte ff = (CellInfoLte)info;dbm = ff.getCellSignalStrength().getDbm();level = ff.getCellSignalStrength().getLevel();asu = 140 + dbm; }else if (info instanceof CellInfoWcdma){//3GCellInfoWcdma ff = (CellInfoWcdma)info;dbm = ff.getCellSignalStrength().getDbm();level = ff.getCellSignalStrength().getLevel();asu = (dbm + 113) / 2; }else if (info instanceof CellInfoGsm){//2GCellInfoGsm 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 双卡开发获取sim卡默认数据卡 获取sim卡信息 sim1卡 sim2卡 2G/3G/4G信号强度》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。