从sim一或sim二android检查移动数据是否已打开 [英] Check mobile data is on from sim one or sim two android

查看:441
本文介绍了从sim一或sim二android检查移动数据是否已打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何,是否可以以编程方式检查来自sim 1或sim 2的移动数据是否处于活动状态?

Is there anyway to check mobile data is active from sim 1 or sim 2 in Android programmatically??

我还想获得数据处于活动状态的运营商提供程序.

Also i want to get the carrier provider in which the data is active.

Eq: sim 1:运营商1 Sim 2:运营商2

Eq : sim 1 : carrier 1 Sim 2 : carrier 2

如果数据在sim 2上处于活动状态 它应该返回运营商2的名称.

If data is active on sim 2 It should return the carrier 2 name.

推荐答案

我的方式(Android> = 22)

my way (Android >= 22)

int getDefaultDataSubscriptionId(final SubscriptionManager subscriptionManager)  {
        if (android.os.Build.VERSION.SDK_INT >= 24)  {
            int nDataSubscriptionId = SubscriptionManager.getDefaultDataSubscriptionId();

            if (nDataSubscriptionId != SubscriptionManager.INVALID_SUBSCRIPTION_ID)  {
                return (nDataSubscriptionId);
            }
        }

        try  {
            Class<?> subscriptionClass = Class.forName(subscriptionManager.getClass().getName());
            try {
                Method getDefaultDataSubscriptionId = subscriptionClass.getMethod("getDefaultDataSubId");

                try {
                    return ((int) getDefaultDataSubscriptionId.invoke(subscriptionManager));
                }
                catch (IllegalAccessException e1) {
                    e1.printStackTrace();
                } catch (InvocationTargetException e1) {
                    e1.printStackTrace();
                }
            } catch (NoSuchMethodException e1) {
                e1.printStackTrace();
            }
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }

        return (SubscriptionManager.INVALID_SUBSCRIPTION_ID);
    }

    @TargetApi(22)
    public String getUIText22(final TelephonyManager telephonyManager) {
        SubscriptionManager subscriptionManager = (SubscriptionManager) getContext().getApplicationContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);

        int nDataSubscriptionId = getDefaultDataSubscriptionId(subscriptionManager);

        if (nDataSubscriptionId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
            SubscriptionInfo si = subscriptionManager.getActiveSubscriptionInfo(nDataSubscriptionId);

            if (si != null) {
                return (si.getCarrierName().toString());
            }
        }
    }

这篇关于从sim一或sim二android检查移动数据是否已打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆