android切换sim卡,Android双SIM卡API

您可以使用MultiSim库从多SIM卡设备中获取详细信息。

每个SIM卡上的可用信息:IMEI,IMSI,SIM序列号,SIM状态,SIM操作员代码,SIM操作员名称,SIM国家/地区ISO,网络操作员代码,网络操作员名称,网络操作员ISO,网络类型,漫游状态。

只需在应用程序级Gradle脚本中添加以下几行:

dependencies {

compile 'com.kirianov.multisim:multisim:2.0@aar'

}

不要忘记在AndroidManifest.xml中添加所需的权限:

在代码中使用类似的代码:

MultiSimTelephonyManager multiSimTelephonyManager = new MultiSimTelephonyManager(this);

// or

MultiSimTelephonyManager multiSimTelephonyManager = new MultiSimTelephonyManager(this, new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

updateInfo();

}

});

public void updateInfo() {

// for update UI

runOnUiThread(new Runnable() {

@Override

public void run() {

multiSimTelephonyManager.update();

useInfo();

}

}

// for update background information

multiSimTelephonyManager.update();

useInfo();

}

public void useInfo() {

// get number of slots:

if (multiSimTelephonyManager != null) {

multiSimTelephonyManager.sizeSlots();

}

// get info from each slot:

if (multiSimTelephonyManager != null) {

for(int i = 0; i < multiSimTelephonyManager.sizeSlots(); i++) {

multiSimTelephonyManager.getSlot(i).getImei();

multiSimTelephonyManager.getSlot(i).getImsi();

multiSimTelephonyManager.getSlot(i).getSimSerialNumber();

multiSimTelephonyManager.getSlot(i).getSimState();

multiSimTelephonyManager.getSlot(i).getSimOperator();

multiSimTelephonyManager.getSlot(i).getSimOperatorName();

multiSimTelephonyManager.getSlot(i).getSimCountryIso();

multiSimTelephonyManager.getSlot(i).getNetworkOperator();

multiSimTelephonyManager.getSlot(i).getNetworkOperatorName();

multiSimTelephonyManager.getSlot(i).getNetworkCountryIso();

multiSimTelephonyManager.getSlot(i).getNetworkType();

multiSimTelephonyManager.getSlot(i).isNetworkRoaming();

}

}

}

// or for devices above android 6.0

MultiSimTelephonyManager multiSimTelephonyManager = new MultiSimTelephonyManager(MyActivity.this, broadcastReceiverChange);

用法:

// get info about slot 'i' by methods:

multiSimTelephonyManager.getSlot(i).

强制更新信息:

// force update phone info (needed on devices above android 6.0 after confirm permissions request)

multiSimTelephonyManager.update();

权限请求的句柄(6.0及更高版本):

// in YourActivity for update info after confirm permissions request on  devices above android 6.0

@Override

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (multiSimTelephonyManager != null) {

multiSimTelephonyManager.update();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中获取SIM卡信息需要使用TelephonyManager类和SubscriptionManager类。 首先,你需要在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.READ_PHONE_STATE" /> ``` 然后,在你的Java代码中,你可以使用以下代码获取SIM卡信息: ```java // 获取系统的TelephonyManager和SubscriptionManager TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); SubscriptionManager subscriptionManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); // 获取当前活动的SIM卡数量 int activeSimCount = telephonyManager.getActiveModemCount(); // 遍历所有的SIM卡信息 for (int i = 0; i < activeSimCount; i++) { SubscriptionInfo subscriptionInfo = subscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(i); if (subscriptionInfo != null) { // 获取SIM卡的详细信息 int subscriptionId = subscriptionInfo.getSubscriptionId(); String carrierName = subscriptionInfo.getCarrierName().toString(); String countryIso = subscriptionInfo.getCountryIso().toString(); String displayName = subscriptionInfo.getDisplayName().toString(); String iccId = subscriptionInfo.getIccId().toString(); String number = subscriptionInfo.getNumber().toString(); int mcc = subscriptionInfo.getMcc(); int mnc = subscriptionInfo.getMnc(); int simSlotIndex = subscriptionInfo.getSimSlotIndex(); int subscriptionType = subscriptionInfo.getSubscriptionType(); boolean isEmbedded = subscriptionInfo.isEmbedded(); // 在此处处理SIM卡信息 } } ``` 注意:上述代码需要在Android 5.1及以上版本中运行。在Android 5.1以下版本中获取SIM卡信息的方法与上述代码略有不同。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值