How to get phone number android phone? [duplicate]

送分小仙女□ 提交于 2019-12-24 00:34:46

问题


I try to get phone number using getLine1Number() in android,but it returns null . Also sometimes I need to use another sim and get its number.

How to always get phone number ?


回答1:


If you are referring to get the phone number from android you can try this !!

TelephonyManager phoneManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = phoneManager.getLine1Number();

Add the permission in manifest

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

Workaround:

  • In your code if you really need users phone number and the above code returns null
  • If you get null, you could display something to get the user to input the phone number on his/her own.



回答2:


You can get phone number with below code :

TelephonyManager tele = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String number = tele.getLine1Number();

And add following permission to manifest file :

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



回答3:


On GSM network, basically you can't! The phone number is only know by the Network and not by the phone or the SIM.

It's the operators network who associate the IMEI SIM number with a valid cell phone. And that's the reason why number portability can be achieved: you can have a different IMEI with the same phone number, or you can have an IMEI with a phone number that changes later on.

You can send an SMS to a specific service if you really want to know it, or ask directly to the end user.




回答4:


Using TelephonyManager you can achive this.

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

Specify this line in the manifest.xml

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



回答5:


As posted in my earlier answer

Use below code :

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

In AndroidManifest.xml, give the following permission:

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

But remember, this code does not always work, since Cell phone number is dependent on the SIM Card and the Network operator / Cell phone carrier.

Also, try checking in Phone--> Settings --> About --> Phone Identity, If you are able to view the Number there, the probability of getting the phone number from above code is higher. If you are not able to view the phone number in the settings, then you won't be able to get via this code!

Suggested Workaround:

  1. Get the user's phone number as manual input from the user.
  2. Send a code to the user's mobile number via SMS.
  3. Ask user to enter the code to confirm the phone number.
  4. Save the number in sharedpreference.

Do the above 4 steps as one time activity during the app's first launch. Later on, whenever phone number is required, use the value available in shared preference.




回答6:


TelephonyManager tele = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String number = tele.getLine1Number();



回答7:


The number you get via getLine1Number() API is stored in SIM/USIM card. So the result of this function depend on the implement of SIM/USIM card.



来源:https://stackoverflow.com/questions/26376050/how-to-get-phone-number-android-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!