简体   繁体   English

如何从双 SIM 卡设备获取两个电话号码?

[英]How to get both phone number from dual SIM device?

I know how to get user's phone number, but let's say the user's phone is dual SIM.我知道如何获取用户的电话号码,但假设用户的电话是双 SIM 卡。 Is there any way to get both phone numbers?有什么办法可以得到这两个电话号码吗? Currently I am getting the active phone number only.目前我只得到有效的电话号码。

If the phone number is indeed stored in the SIM card, then you can use subscriptionmanager API ( https://developer.android.com/reference/android/telephony/SubscriptionManager.html ) to get the details on each subscription ie for each SIM card. 如果电话号码确实存储在SIM卡中,则可以使用subscriptionmanager API( https://developer.android.com/reference/android/telephony/SubscriptionManager.html )获取有关每个订阅(即每个SIM)的详细信息卡。

You can call 你可以打电话

Please note that for this to work, the SIM card should have the phone number in it. 请注意,要执行此操作,SIM卡中应包含电话号码。

Please note this API is only supported from API level 22 请注意,此API仅在API级别22中受支持

Adding example code : 添加示例代码:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager subscriptionManager = SubscriptionManager.from(getApplicationContext());
            List<SubscriptionInfo> subsInfoList = subscriptionManager.getActiveSubscriptionInfoList();

            Log.d("Test", "Current list = " + subsInfoList);

            for (SubscriptionInfo subscriptionInfo : subsInfoList) {

                String number = subscriptionInfo.getNumber();

                Log.d("Test", " Number is  " + number);
            }
        }

I have Kotlinized @manishg code (from 5 years ago):我有 Kotlinized @manishg 代码(5 年前):

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
    val subscriptionManager = SubscriptionManager.from(applicationContext)
    val subsInfoList = subscriptionManager.activeSubscriptionInfoList
    Log.d("Test", "Current list = $subsInfoList")
    for (subscriptionInfo in subsInfoList) {
        val number = subscriptionInfo.number
        Log.d("Test", " Number is  $number")
    }
}

Note #1: you must add to the manifest:注意#1:您必须添加到清单中:

uses-permission android:name="android.permission.READ_PHONE_STATE"

Note #2: you should have a permission check on your code, like:注意#2:您应该对您的代码进行权限检查,例如:

  val subsInfoList = if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                        //    ActivityCompat#requestPermissions
        return
    } else {
        //todo return "no permissions"
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM