Get mobile Number on GSM Mobile using android code

折月煮酒 提交于 2019-12-08 01:59:59

问题


I am using following android code to get mobile number and it's working on android emulators only:

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

But when I tested it in real device (Samsung Galaxy Chat B5330) it gives nothing (Empty String)

Please help with some code snippet.


回答1:


For GSM, the phone number is on the SIM card, and some carriers just don't put it on the card, and then the phone does not know what it is, but in this case you should get an empry string rather than a null

if the carrier stores the number on your SIM, go to Settings -> About Phone -> Status -> My phone Number. If it displays unknown there, then your number is not stored on the SIM.

get IMEI use:

TelephonyManager tm =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
imei_no = tm.getDeviceId(); 



回答2:


Add permission

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

See this




回答3:


You have to use Telephony Manager;If at all you not found the contact no. of user; You can get Sim Serial Number of Sim Card and Imei No. of Android Device by using the same Telephony Manager Class...

Add permission:

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

Import:

import android.telephony.TelephonyManager;

Use the below code:

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

         // get IMEI
         imei = tm.getDeviceId();

         // get SimSerialNumber
         simSerialNumber = tm.getSimSerialNumber();


来源:https://stackoverflow.com/questions/16333816/get-mobile-number-on-gsm-mobile-using-android-code

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