android 判断双卡手机号码,android双卡机中判断当前使用的是那张卡的数据流量

今天主要讲的是在android 5.1及以上如何判断双卡手机中判断当前的流量是使用的那张卡。

(例子中的代码会使用到java反射的知识)

首先判断数据流量开关是否打开:

/**

* 判断数据流量开关是否打开

* @param context

* @return

*/

public static boolean isMobileEnabled(Context context) {

try {

Method getMobileDataEnabledMethod = ConnectivityManager.class.getDeclaredMethod("getMobileDataEnabled");`

getMobileDataEnabledMethod.setAccessible(true);

ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

return (Boolean) getMobileDataEnabledMethod.invoke(connectivityManager);

}catch (Exception e) {

e.printStackTrace();`

}

return true;

}

只有在已经打开了流量开关的时候才会去判断是使用的那张卡的流量,当没有开启数据流量时即不用判断。

下边的方法是用于获取当前手机有几张可用的手机卡,获取这些手机卡的基本信息。

public

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取双卡 Android 手机的电话号码可以使用下面的代码: ```java TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); String sim1PhoneNumber = telephonyManager.getLine1Number(0); String sim2PhoneNumber = telephonyManager.getLine1Number(1); ``` 需要注意的是,有些运营商可能不会将电话号码存储在 SIM ,因此可能无法获取到电话号码。 获取双卡 Android 手机的运营商频段可以使用下面的代码: ```java TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); List<CellInfo> cellInfoList = telephonyManager.getAllCellInfo(); for (CellInfo cellInfo : cellInfoList) { if (cellInfo instanceof CellInfoGsm) { CellIdentityGsm cellIdentityGsm = ((CellInfoGsm) cellInfo).getCellIdentity(); int mcc = cellIdentityGsm.getMcc(); int mnc = cellIdentityGsm.getMnc(); int arfcn = cellIdentityGsm.getArfcn(); // TODO: 处理 GSM 频段信息 } else if (cellInfo instanceof CellInfoCdma) { CellIdentityCdma cellIdentityCdma = ((CellInfoCdma) cellInfo).getCellIdentity(); int sid = cellIdentityCdma.getSystemId(); int nid = cellIdentityCdma.getNetworkId(); int bid = cellIdentityCdma.getBasestationId(); int freq = cellIdentityCdma.getFrequency(); // TODO: 处理 CDMA 频段信息 } else if (cellInfo instanceof CellInfoLte) { CellIdentityLte cellIdentityLte = ((CellInfoLte) cellInfo).getCellIdentity(); int mcc = cellIdentityLte.getMcc(); int mnc = cellIdentityLte.getMnc(); int earfcn = cellIdentityLte.getEarfcn(); // TODO: 处理 LTE 频段信息 } } ``` 需要注意的是,以上代码需要在 Android 5.1 及以上的版本上运行,并且需要在 AndroidManifest.xml 文件添加相应的权限: ```xml <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值