检测的代码
private void checkIMSi() {
boolean simStateBySlotIdx = SimUtils.getSimStateBySlotIdx(this, 0);
boolean simStateBySlotIdx1 = SimUtils.getSimStateBySlotIdx(this, 1);
if (simStateBySlotIdx) {
logger.error("卡一aaaa");
providersName = SimUtils.getSimOperatorName(this, 0);
check1();
} else {
check1();
logger.error("没有卡一");
}
if (simStateBySlotIdx1) {
logger.error("卡二sssss");
providersName1 = SimUtils.getSimOperatorName(this, 1);
check1();
} else {
check1();
logger.error("没有卡二");
}
}
private void check1() {
if (providersName != null && providersName1 != null) {
if (providersName.equals("中国电信") || providersName1.equals("中国电信")) {
logger.error("中国电信");
} else {
logger.error("no");
}
} else if (providersName != null) {
if (providersName.equals("中国电信")) {
logger.error("providersName ------" + providersName);
} else {
}
} else if (providersName1 != null) {
if (providersName1.equals("中国电信")) {
logger.error("providersName ------" + providersName1);
} else {
}
} else {
}
}
工具类
import android.Manifest;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.lang.reflect.Method;
/**
* The type Sim utils.
*/
public class SimUtils {
private static final String TAG = SimUtils.class.getSimpleName();
private static final String SIM_STATE = "getSimState";
private static final String SIM_OPERATOR_NAME = "getNetworkOperatorName";
private static final String SIM_NETWORK_TYPE = "getNetworkType";
private static final String SIM_IMEI = "getImei";
private static final String SIM_LINE_NUMBER = "getLine1Number";
/**
* Gets sim phonenumber.
*
* @param context the context
* @param slotIdx the slot idx
* @return the sim phonenumber
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
public static String getSimPhonenumber(Context context, int slotIdx) {
if (PermissionUtil.hasSelfPermission(context, Manifest.permission.READ_PHONE_STATE) ||
PermissionUtil.hasSelfPermission(context, "android.permission.READ_PRIVILEGED_PHONE_STATE")) {
Log.d(TAG, "READ_PHONE_STATE permission has BEEN granted to getSimPhonenumber().");
if (getSimStateBySlotIdx(context, slotIdx)) {
return (String) getSimByMethod(context, SIM_LINE_NUMBER, getSubidBySlotId(context, slotIdx));
}
return null;
} else {
Log.d(TAG, "READ_PHONE_STATE permission has NOT been granted to getSimPhonenumber().");
return null;
}
}
/**
* Gets sim imei.
*
* @param context the context
* @param slotIdx the slot idx
* @return the sim imei
*/
public static String getSimImei(Context context, int slotIdx) {
if (PermissionUtil.hasSelfPermission(context, Manifest.permission.READ_PHONE_STATE) ||
PermissionUtil.hasSelfPermission(context, "android.permission.READ_PRIVILEGED_PHONE_STATE")) {
Log.d(TAG, "READ_PHONE_STATE permission has BEEN granted to getSimImei().");
return (String) getSimByMethod(context, SIM_IMEI, slotIdx);
} else {
Log.d(TAG, "READ_PHONE_STATE permission has NOT been granted to getSimImei().");
return null;
}
}
/**
* Gets sim network type.
*
* @param context the context
* @param slotIdx the slot idx
* @return the sim network type
*/
@RequiresApi(api = Build.VERSION_CODES.M)
public static int getSimNetworkType(Context context, int slotIdx) {
if (PermissionUtil.hasSelfPermission(context, Manifest.permission.READ_PHONE_STATE)) {
Log.d(TAG, "READ_PHONE_STATE permission has BEEN granted to getSimNetworkType().");
if (getSimStateBySlotIdx(context, slotIdx)) {
return (int) getSimByMethod(context, SIM_NETWORK_TYPE, getSubidBySlotId(context, slotIdx));
}
} else {
Log.d(TAG, "READ_PHONE_STATE permission has NOT been granted to getSimNetworkType().");
}
return TelephonyManager.NETWORK_TYPE_UNKNOWN;
}
/**
* Gets sim network name.
*
* @param context the context
* @param slotIdx the slot idx
* @return the sim network name
*/
@RequiresApi(api = Build.VERSION_CODES.M)
public static String getSimNetworkName(Context context, int slotIdx) {
return getNetworkName(getSimNetworkType(context, slotIdx));
}
/**
* Gets sim operator name.
*
* @param context the context
* @param slotIdx the slot idx
* @return the sim operator name
*/
public static String getSimOperatorName(Context context, int slotIdx) {
if (getSimStateBySlotIdx(context, slotIdx)) {
return (String) getSimByMethod(context, SIM_OPERATOR_NAME, getSubidBySlotId(context, slotIdx));
}
return null;
}
/**
* Gets sim state by slot idx.
*
* @param context the context
* @param slotIdx :0(sim1),1(sim2)
* @return the sim state by slot idx
*/
public static boolean getSimStateBySlotIdx(Context context, int slotIdx) {
boolean isReady = false;
Object getSimState = getSimByMethod(context, SIM_STATE, slotIdx);
if (getSimState != null) {
int simState = Integer.parseInt(getSimState.toString());
if ((simState != TelephonyManager.SIM_STATE_ABSENT) && (simState != TelephonyManager.SIM_STATE_UNKNOWN)) {
isReady = true;
}
}
return isReady;
}
/**
* Gets sim by method.
*
* @param context the context
* @param method the method
* @param param the param
* @return the sim by method
*/
public static Object getSimByMethod(Context context, String method, int param) {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
assert telephony != null;
Class> telephonyClass = Class.forName(telephony.getClass().getName());
Class>[] parameter = new Class[1];
parameter[0] = int.class;
Method getSimState = telephonyClass.getMethod(method, parameter);
Object[] obParameter = new Object[1];
obParameter[0] = param;
Object ob_phone = getSimState.invoke(telephony, obParameter);
if (ob_phone != null) {
return ob_phone;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* The type Current network.
*/
public static class CurrentNetwork {
/**
* The Which sim.
*/
public String whichSim;//那张卡
/**
* The Net work name.
*/
public String netWorkName;//几G网络
/**
* The Operate name.
*/
public String operateName;//卡生厂商
}
/**
* Gets current network.
*
* @param context the context
* @return the current network
*/
@RequiresApi(api = Build.VERSION_CODES.M)
public static CurrentNetwork getCurrentNetwork(Context context) {
CurrentNetwork currentNetwork = new CurrentNetwork();
ConnectivityManager connectionManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
assert connectionManager != null;
NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
assert tm != null;
Log.d(TAG, "state:" + tm.getSimState());
if (networkInfo != null) {
if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
boolean status = networkInfo.isConnected();
int sim1NetWorkType = getSimNetworkType(context, 0);
int sim2NetWorkType = getSimNetworkType(context, 1);
if (networkInfo.getSubtype() == sim1NetWorkType) {
if (getSimStateBySlotIdx(context, 0)) {
currentNetwork.netWorkName = getNetworkName(sim1NetWorkType);
currentNetwork.operateName = getSimOperatorName(context, 0);
currentNetwork.whichSim = "卡1";
}
} else if (networkInfo.getSubtype() == sim2NetWorkType) {
if (getSimStateBySlotIdx(context, 1)) {
currentNetwork.netWorkName = getNetworkName(sim2NetWorkType);
currentNetwork.operateName = getSimOperatorName(context, 1);
currentNetwork.whichSim = "卡2";
}
}
}
} else {
// Logger.d(TAG, "network info is null: ");
}
return currentNetwork;
}
/**
* Gets network name.
*
* @param networkType the network type
* @return the network name
*/
public static String getNetworkName(int networkType) {
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return "2G";
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
return "3G";
case TelephonyManager.NETWORK_TYPE_LTE:
return "4G";
default:
return "UNKNOWN";
}
}
/**
* to
*
* @param context the context
* @param slotId the slot id
* @return subid by slot id
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
public static int getSubidBySlotId(Context context, int slotId) {
SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(
Context.TELEPHONY_SUBSCRIPTION_SERVICE);
try {
assert subscriptionManager != null;
Class> telephonyClass = Class.forName(subscriptionManager.getClass().getName());
Class>[] parameter = new Class[1];
parameter[0] = int.class;
Method getSimState = telephonyClass.getMethod("getSubId", parameter);
Object[] obParameter = new Object[1];
obParameter[0] = slotId;
Object ob_phone = getSimState.invoke(subscriptionManager, obParameter);
if (ob_phone != null) {
Log.d(TAG, "slotId:" + slotId + ";" + ((int[]) ob_phone)[0]);
return ((int[]) ob_phone)[0];
}
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
}