尊重原作,此文转载于https://www.cnblogs.com/ldq2016/p/7028488.html,感谢作者提供思路。
需求:检测双卡的信号强度和状态
解决方案:
  • 检测sim卡1
private void sim1() {TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);SubscriptionManager mSubscriptionManager = SubscriptionManager.from(this);SubscriptionInfo sub0 = mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(0);if (null != sub0) {//如果不为空 说明sim卡1存在/*sub0.getSubscriptionId() 获取sim卡1的 subId*/Sim1SignalStrengthsListener mSim1SignalStrengthsListener = new Sim1SignalStrengthsListener(sub0.getSubscriptionId());//开始监听if (mTelephonyManager != null) {mTelephonyManager.listen(mSim1SignalStrengthsListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);}}boolean isSimCardExist = false;try {Method method = TelephonyManager.class.getMethod("getSimState", int.class);int simState = (Integer) method.invoke(mTelephonyManager, new Object[]{0});if (TelephonyManager.SIM_STATE_READY == simState) {isSimCardExist = true;}} catch (Exception e) {e.printStackTrace();}Log.i(TAG,"isSimCardExist:" + isSimCardExist);}

这里直接返回了isSimCardExist的状态,通过这个可以判断sim卡是否可用。
信号强度则是由Sim1SignalStrengthsListener类返回,接下来看Sim1SignalStrengthsListener类代码:

public class Sim1SignalStrengthsListener extends PhoneStateListener {public Sim1SignalStrengthsListener(int subId) {super();ReflectUtil.setFieldValue(this, "mSubId", subId);}@Overridepublic void onSignalStrengthsChanged(SignalStrength signalStrength) {super.onSignalStrengthsChanged(signalStrength);int level = getSignalStrengthsLevel(signalStrength);//level返回的即是信号强度0-5Log.i(TAG,"level:" + level);}private int getSignalStrengthsLevel(SignalStrength signalStrength) {int level = -1;try {Method levelMethod = SignalStrength.class.getDeclaredMethod("getLevel");level = (int) levelMethod.invoke(signalStrength);} catch (Exception e) {Log.e(TAG, e.getMessage());}return level;}}

ReflectUtil反射类的代码:

public class ReflectUtil {public static void setFieldValue(final Object obj, final String fieldName, final Object value) {Field field = getAccessibleField(obj, fieldName);if (field == null) {throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]");}try {field.set(obj, value);} catch (IllegalAccessException e) {e.printStackTrace();}}public static Field getAccessibleField(final Object obj, final String fieldName) {if (obj == null) {throw new IllegalArgumentException("object can't be null");}if (fieldName == null || fieldName.length() <= 0) {throw new IllegalArgumentException("fieldName can't be blank");}for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {try {Field field = superClass.getDeclaredField(fieldName);makeAccessible(field);return field;} catch (NoSuchFieldException e) {continue;}}return null;}public static void makeAccessible(Field field) {if ((!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers()) || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) {field.setAccessible(true);}}
}

照搬照抄测试一下结果如下:

08-28 15:48:24.891 8205-8205/com.example.gqiu.helloworld I/lgq: isSimCardExist:true
08-28 15:48:24.896 8205-8205/com.example.gqiu.helloworld I/lgq: sim2 isSimCardExist:true
08-28 15:48:24.928 8205-8205/com.example.gqiu.helloworld I/lgq: level:4
08-28 15:48:24.929 8205-8205/com.example.gqiu.helloworld I/lgq: sim 2 level:4
08-28 15:48:25.285 8205-8205/com.example.gqiu.helloworld I/lgq: level:4
08-28 15:48:28.291 8205-8205/com.example.gqiu.helloworld I/lgq: level:4
08-28 15:48:28.330 8205-8205/com.example.gqiu.helloworld I/lgq: sim 2 level:4
08-28 15:48:31.297 8205-8205/com.example.gqiu.helloworld I/lgq: level:4
08-28 15:48:31.334 8205-8205/com.example.gqiu.helloworld I/lgq: sim 2 level:4
08-28 15:48:34.304 8205-8205/com.example.gqiu.helloworld I/lgq: level:4
08-28 15:48:34.339 8205-8205/com.example.gqiu.helloworld I/lgq: sim 2 level:4
08-28 15:48:37.329 8205-8205/com.example.gqiu.helloworld I/lgq: level:4
08-28 15:48:37.352 8205-8205/com.example.gqiu.helloworld I/lgq: sim 2 level:4
08-28 15:48:40.332 8205-8205/com.example.gqiu.helloworld I/lgq: level:4
08-28 15:48:40.370 8205-8205/com.example.gqiu.helloworld I/lgq: sim 2 level:4
  • sim2检测
//此处0修改为1,其他同sim1,就不必废话了。祝君好运~
SubscriptionInfo sub0 = mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(1);

android 双SIM卡检测是否可用和信号强度相关推荐

  1. android sim卡应用程序,Android双SIM卡API

    MM们 您可以使用MultiSim库从多SIM卡设备中获取详细信息.每个SIM卡上的可用信息:IMEI,IMSI,SIM序列号,SIM状态,SIM操作员代码,SIM操作员名称,SIM国家/地区ISO, ...

  2. Android实现SIM卡2拨打,从第二个SIM卡打电话(Call from second sim)

    我有一个双卡Android手机. 我使用此代码来拨打电话: private void callBack(String phone, Context context) { Intent callInte ...

  3. android+双卡imei,以编程方式在Android中为双SIM卡检索IMEI号码

    对于单个SIM,以下代码有效: TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); String ...

  4. android sim卡 信息,android-如何使用SIM2或双SIM卡提交短信

    我创建了Android短信应用程序 我已经能够顺利发送短信sim卡的短信,则没有报告. 顺利 请帮助我添加其源代码 我的密码 public class MainActivity extends Act ...

  5. android指定sim卡拨号,使用双SIM卡设备中的指定SIM拨打电话

    过去几天我一直在寻找这个,我开始知道: "开箱即用的Android不支持双SIM卡.这是制造商的自定义修改,并且没有公共API来控制它." 下面的链接提供了一个解决方案,但它不能在 ...

  6. android指定sim卡拨号,android – 在双SIM卡设备中使用指定的SIM拨打电话

    过去几天我一直在寻找这个,我开始知道: "开箱即用的Android不支持双SIM卡.这是制造商的自定义修改,并且没有公共API来控制它." 下面的链接提供了一个解决方案,但它不能在 ...

  7. Android实现SIM卡2拨打,android – 在双SIM卡设备中使用指定的SIM拨...

    过去几天我一直在寻找这个,我开始知道: "开箱即用的Android不支持双SIM卡.这是制造商的自定义修改,并且没有公共API来控制它." 下面的链接提供了一个解决方案,但它不能在 ...

  8. 双SIM卡,Android监听SIM卡状态

    最近在搞一个项目,其中有一块关于双SIM卡状态监听,在这一块上没有经验,在网上找了很多例子,但都是只是监听SIM卡改变状态,而我要的是不但要监听SIM状态改变,而且还要知道具体是哪张SIM卡状态改变, ...

  9. 全网通蜂窝路由器_工业路由器双sim卡

    TR341全网通蜂窝路由器,电信/移动/联通5G4G网络,并往下兼容 EDGE.CDMA 1X 及 GPRS 网络.支持双卡双模,标准的抽屉式用户卡接口,支持 1.8V/3V SIM/UIM 卡,内置 ...

最新文章

  1. Android UI开发详解之ActionBar .
  2. SpringBoot 拦截器和过滤器
  3. C++迭代器iterator
  4. scp和rsync的区别和常用参数
  5. android 标题图标,android 中 actionbar 常用方法。设置标题,隐藏图标等
  6. 算法:线性时间选择_机器学习必修课!scikit-learn 支持向量机算法库使用小结
  7. org.springframework.web.client.ResourceAccessException: I/O error on POST request for ************
  8. TCMalloc:线程缓存Malloc以及tcmalloc与ptmalloc性能对比
  9. 问答方式学 Node.js
  10. stm32寄存器版学习笔记04 定时计数器中断
  11. 免费python网络课程-2019年10种免费的Python学习课程
  12. JAVA加密解密→术语、密码分类、OSI与TCP/IP安全体系、Base64、消息摘要算法MD/SHA/MAC、对称加密算法DES/AES/PBE、非对称加密算法DH/RSA/EIGamaI
  13. vue 内存溢出问题解决
  14. nc 二次开发_金蝶云星空(K3CLOUD)和用友NC对比
  15. 3道js面试题引发的脑洞
  16. AlphaGo开源代码
  17. python中返回上一步操作的快捷键_在计算机中返回上一步的快捷键
  18. UiPath常用元素识别
  19. AD20—PCB总结
  20. java后台实现支付宝支付接口、支付宝订单查询接口 前端为APP

热门文章

  1. HTML5作业2绘画h5标志
  2. 发出警报声的c语言程序,PIC单片机警报声C程序
  3. 深圳软件测试培训:Docker下部署MySQL和Wordpress
  4. 开源中国码云git连接
  5. How cc Works 中文译文 1
  6. discuzx管理员、超级版主、版主管理手册
  7. 【acwing】游戏时间2理解
  8. Java项目一 家庭记账软件
  9. Pytorch+Google BERT模型(RoBERTa+LSTM+GRU)实战
  10. oracle 每个月求本年该月及之前的合计