Android determines which card is currently using data traffic

Overview

slotId refers to the card slot. The value starts from 0 and increases according to the number of card slots. For example, the value of card slot 1 is 0, the value of card slot 2 is 1, and so on.
subId refers to the sim card id in the database. The value increases according to the number of inserted sim cards. Every time a sim card is inserted, the subId increases by 1.

step:

1. Determine whether to open data traffic
2. Get subId
3. Convert subId to card slot slotId

Sample code

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;
    }
}

If there are any errors, please correct them.

reference:

How does Android determine whether WIFI and data traffic RGPS are turned on at the same time?
Get the card slot ID of the current traffic card and determine the SIM card switching traffic (traffic card switching judgment)

Guess you like

Origin blog.csdn.net/fengyulinde/article/details/121794587