Android判断当前使用数据流量的是哪张卡

概述

slotId是指卡槽,值从0开始,值按卡槽个数递增,例如卡槽一值为0,卡槽2为1,依次类推。
subId是指数据库中sim卡id,值按插入的sim卡个数递增,每插入一张sim卡,subId加1。

步骤:

1.判断是否打开数据流量
2.获取subId
3.将subId转换为卡槽slotId

示例代码

import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Build;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;

import java.lang.reflect.Method;

public class ReadPhoneUtil {

    /**
     * 判断手机数据流量是否打开
     *
     * @param context*
     * @return true 连接 false 未连接
     **/

    @SuppressWarnings({"rawtypes", "unchecked"})
    public static boolean isMobileDataOpen(Context context) {
        try {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            Class ownerClass = mConnectivityManager.getClass();

            Method method = ownerClass.getMethod("getMobileDataEnabled", null);
            return (Boolean) method.invoke(mConnectivityManager, null);
        } catch (Exception e) {
            return false;
        }
    }

    public static int getSlotId(Context context) {
        if (!isMobileDataOpen(context)) {
            return -1;
        }
        int dataSubId = 0;
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                dataSubId = SubscriptionManager.getDefaultDataSubscriptionId();
            } else {
                dataSubId = getDataSubId(context);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SubscriptionManager.getSlotIndex(dataSubId);
    }

    private static int getDataSubId(Context context) {
        int defaultDataSlotId = getDefaultDataSlotId(context);
        try {
            Object obj = Class.forName("android.telephony.SubscriptionManager").getDeclaredMethod("getSubId", int.class)
                    .invoke(null, defaultDataSlotId);
            if (obj != null) {
                if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
                    return (int) (((long[]) obj)[0]);
                }
                return ((int[]) obj)[0];
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return defaultDataSlotId;
    }

    private static int getDefaultDataSlotId(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager subscriptionManager = SubscriptionManager.from(context.getApplicationContext());
            if (subscriptionManager != null) {
                try {
                    Class<?> subClass = Class.forName(subscriptionManager.getClass().getName());
                    Method getSubID = subClass.getMethod("getDefaultDataSubscriptionInfo");
                    SubscriptionInfo subInfo = (SubscriptionInfo) getSubID.invoke(subscriptionManager);
                    if (subInfo != null) {
                        return subInfo.getSimSlotIndex();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else {
            try {
                Class cls = Class.forName("android.telephony.SubscriptionManager");
                Method getSubId;
                try {
                    getSubId = cls.getDeclaredMethod("getDefaultDataSubId");
                } catch (NoSuchMethodException e) {
                    getSubId = cls.getDeclaredMethod("getDefaultDataSubscriptionId");
                }
                int subId = (int) getSubId.invoke(null);
                int slotId;
                if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
                    Method getSlotId = cls.getDeclaredMethod("getSlotId", long.class);
                    slotId = (int) getSlotId.invoke(null, (long) subId);
                } else {
                    Method getSlotId = cls.getDeclaredMethod("getSlotId", int.class);
                    slotId = (int) getSlotId.invoke(null, subId);
                }
                return slotId;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return -1;
    }
}

如有错误,欢迎纠正。

参考:

Android如何判断WIFI和数据流量RGPS是否同时打开
Android获取当前流量卡的卡槽id,判断SIM卡切换流量(流量卡切换判断)

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值