gpt4 book ai didi

java - 如何验证当前设备中是否存在手机号码

转载 作者:行者123 更新时间:2023-12-02 08:58:12 27 4
gpt4 key购买 nike

在我的应用程序中,我想验证用户输入的手机号码是否存在于当前设备上。就像所有银行应用程序一样。如果当前设备上没有注册的手机,银行应用程序将不允许您登录。<​​/p>

我的问题是如何做到这一点。我尝试过 TelephonyManager API 和 SubscriptionManager API。两人均未透露手机号码。使用 SubscriptionManager API,我可以获得以下信息,但不能获取手机号码

SubscriptionManager subscriptionManager = SubscriptionManager.from(getApplicationContext()); 
List subsInfoList = subscriptionManager.getActiveSubscriptionInfoList();
if (subsInfoList != null) {
Log.d("log", "number of sims = " + subsInfoList.size());
Log.d("log", "Current list = " + subsInfoList);
for (SubscriptionInfo subscriptionInfo : subsInfoList) {
Log.d("log", " Number is " + subscriptionInfo.getNumber());
tvNumber.setText(subscriptionInfo.getNumber());
}
}

SIM 数量 = 1

Current list = [{id=1, iccId=89914509009117870249 simSlotIndex=0 displayName=airtel carrierName=airtel nameSource=2 iconTint=-13408298 dataRoaming=1 iconBitmap=android.graphics.Bitmap@326dab88 mcc 404 mnc 45 status 1}] 
Number is

TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); String mobileNumber = tMgr.getLine1Number(); Log.d("log", "mobile number = " + mobileNumber);

最佳答案

将最后一行替换为

TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();

确保启用以下权限非常重要:

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

注意:这可以返回 null、空字符串或 "???????"

引用号:Programmatically obtain the phone number of the Android phone

关于java - 如何验证当前设备中是否存在手机号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60364924/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com