概述
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 {
@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卡切换流量(流量卡切换判断)
|