android实现异网双卡双待识别运营商网络
检测的代码
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;
}
}
android实现异网双卡双待识别运营商网络相关推荐
- Golang 获取当前外网IP/地址/运营商
Golang 获取当前外网IP/地址/运营商 - Go语言中文网 - Golang中文社区 package mainimport ("fmt""io/ioutil&quo ...
- Android获取本机电话号码及运营商信息
电话管理器TelephonyManager可访问电话服务信息,包括电话号码.运营商信息.wifi状态等.一些电话信息需要相应的权限,具体可以看API文件中TelephonyManager方法的说明. ...
- 手机170号段银行不识别 运营商称正努力解决
首批用户中曝出170号段网站验证短信接收不到.手机运营商标识不显示.不被银行客服系统识别等体验瑕疵. 就在虚拟运营商的170号段开始大规模放号之际,首批用户中曝出网站验证短信接收不到.手机运营商标识不 ...
- 物联网操作系统再思考-Hello China操作系统的运营商网络协同机制
Hello China定位为物联网操作系统,在我们以前关于物联网操作系统的系列描述文章中,已经总结出物联网操作系统区别于其它操作系统的两大核心机制:物联网相关的特性支持和运营商网络的紧密协同.所谓物联 ...
- 运营商网络中的在线加密(三)
运营商网络中的"在线"加密(三) 5.4. OTN加密:运营商的应用实例 5.4.1.加密的高带宽数据中心互连 (DCI) 业务 数据中心之间的通信流的特点包括:高带宽.分组 ...
- 如何防止运营商网络劫持,避免被他人强行插入广告?
主要的网络劫持形式: 近年来流量劫持(运营商网络劫持)频频发生,各种方式也是层出不穷,易维信-EVTrust总结各种网络劫持现象和其带来的危害 1.域名劫持,用户想要访问网站A,域名却被解析到其它地址 ...
- 电信运营商网络运维方案
随着新一代信息技术加快普及应用,5G.云和人工智能正加速智能社会的到来,三大技术正在重构网络.随着我国5G产业快速推进,中国移动.中国联通.中国电信加快步伐,全业务运营时代已经到来.全业务运营时代的特 ...
- 阿里再爆调整组织架构;苹果秘密研究卫星传输iPhone数据,想摆脱运营商网络;联发科天玑1000:首款采用4颗A77的芯片……...
关注并标星星CSDN云计算 速递.最新.绝对有料.这里有企业新动.这里有业界要闻,打起十二分精神,紧跟fashion你可以的! 每周两次,打卡即read 更快.更全了解泛云圈精彩news go ...
- 计算机网络原理实践报告--跨国公司,通过运营商网络如何实现总部公司与各分公司之间的网络互通
一.实践的实验内容 本次实践的内容为:模拟跨国公司,通过运营商网络如何实现总部公司与各分公司之间的网络互通. 实验目标: 1.运营商网络可以全网互通,并且发生故障点后,可以自动切换路由路径. 2.公司 ...
最新文章
- 如何使用WindowsLiveWriter发文章
- 使用idea对生产环境的JAVA应用进行远程调试
- SeetaFace2 测试
- Windows下 MySQL命令 常用操作
- HBase总结(十二)Java API 与HBase交互实例
- DataGridView新特色、常用操作
- css 样式使用方法的累积
- python 链表两数相加
- java在src创建entity文件_java自动生成entity文件
- android Eclipse导入com.android.internal.R和layoutlib.jar报错解决方案
- 微信小程序仿微信SlideView组件slide-view
- 精通Quartz-入门-Job
- bzoj1596[Usaco2008 Jan]电话网络*
- Excel向数据库插入数据(执行一次只需连接一次)-batch简单使用
- 倒计时 5 天!年度开发者盛会 Unite Shanghai 2019 全日程揭晓(附表)
- Apache(httpd) 报错You don't have permission to access /on this server.
- 转:KVC与KVO机制
- 从 30248.271s 优化到 0.001,跪了....
- 最佳Excel导入实践(一)
- mysql数据库修复工具_MySQL数据库修复软件(Recovery Toolbox for MySQL)