• 随意切换双卡流量,立即获取对应卡的SMIS


     原生的Android是不支持双卡双待功能的,所以Android sdk没有提供API,直到在Android6.0+上增加了相关的API,但是国内的android的情况嘛,大家都懂得,有6.0和没6.0没啥区别, 因为基本各大厂商都已经更改底层系统了;

    所以小编费了九牛二虎之力,依然没有在网上找到对应的解决办法,那怎么办????分析源码吧,呵呵!现解决办法,希望对小伙伴没有帮助!

    直接上代码:

    主要有下面三个方法:

    1:先判断当前手机是否打开本地流量

    2:再判断当前哪张卡走流量,然后获取对应卡的卡槽

    3:再根据对应的卡槽获取subid  然后获取IMSI

    //1: 判断数据流量开关是否打开
    public static boolean isMobileEnabled(Context context) {
    try {
    Method getMobileDataEnabledMethod =
    ConnectivityManager.class.getDeclaredMethod("getMobileDataEnabled");
    getMobileDataEnabledMethod.setAccessible(true);
    ConnectivityManager connectivityManager =
    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    boolean b = (boolean) getMobileDataEnabledMethod.invoke(connectivityManager);
    ;
    return b;
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    e.printStackTrace();
    }
    return true;
    }
    //2:TODO 判断当前那张卡 是走流量的
    @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("GetPhoneInfoHelper", (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;

    }
    //3:TODO  获取双卡双待  卡序列号
    public static String getSubscriberId(Context context, TelephonyManager telephony,int simSlotIndex) {
    Class<?> telephonyClass;
    String rtResult = null;
    try {
    telephonyClass = Class.forName(telephony.getClass().getName());
    Method method = telephonyClass.getMethod("getSubscriberId", new Class[]{int.class});
    rtResult = (String) method.invoke(telephony,simSlotIndex);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return rtResult;
    }


    代码调用:
    String IMSI =  ReflectionGetCard.getSubscriberId(context,tm,ReflectionGetCard.getDefaultDataSubId(context)); 
    注意:我调用没有判断 当前 是否打开本地流量 ,因为我只需要获取对应卡的网络状态等;这个ReflectionGetCard类是上面方法的封装类;
    亲测:红米、oppoR11S、华为荣耀8是正常的获取的;
    PS:在华为荣耀8上 上面调用代码需要更改一下:
    String IMSI = tm.getSubscriberId();

    String str = ReflectionGetCard.getSubscriberId(context,tm,ReflectionGetCard.getDefaultDataSubId(context));
    否则 华为荣亚8 会卡死,不知道什么愿意?有知道的小伙伴或者大神给解答下!谢谢!欢迎随时交流:847638676(同微信)
  • 相关阅读:
    操作系统概念(第十一章) 文件系统实现
    Linux内核中网络数据包的接收-第一部分 概念和框架
    【技术文档】jeecg3.7-maven搭建好开发环境入门
    memcache源码编译安装
    Linux系统级日志系统
    shell流程控制
    EOF多行写入文件防止变量替换
    zip压缩解压
    shell变量的截取总结
    对linux安装中文字体库
  • 原文地址:https://www.cnblogs.com/yi-snow-W/p/10944033.html
Copyright © 2020-2023  润新知