如何从设备获取eSIM电话号码?

时间:2019-12-19 06:59:42

标签: android phone-number

我想从设备获取eSIM电话号码。

我正在使用SubscriptionInfo类获取电话号码,但它始终返回null。

SubscriptionManager subscriptionMgr = SubscriptionManager.from(context);
SubscriptionInfo subscriptionInfo = subscriptionMgr.getActiveSubscriptionInfoForSimSlotIndex(simIndex);

String phoneNumber = subscriptionInfo.getNumber()

我也在清单文件中授予权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

1 个答案:

答案 0 :(得分:0)

您必须检查是否已授予许可。

public void getSIMNumber() {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
        SubscriptionManager subscriptionManager = SubscriptionManager.from(mContext.getApplicationContext());
        //TODO check if permission is Granted

        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);
        }
    }

}