0

How to get cell phone number in android ? Here is code. but it's not working

TelephonyManager tm = (TelephonyManager)this.getSystemService(this.TELEPHONY_SERVICE);
String mobileno = tm.getLine1Number();

can anyone help me to solve this.

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
Dharmesh Dhameliya
  • 174
  • 1
  • 3
  • 10
  • 1
    Possible duplicate of [Get mobile Number on GSM Mobile using android code](http://stackoverflow.com/questions/16333816/get-mobile-number-on-gsm-mobile-using-android-code) – Dhaval Parmar Mar 05 '16 at 05:59
  • @DhawalSodhaParmar that's rather poor choice for marking duplicate. [The top voted one](http://stackoverflow.com/questions/2480288/programmatically-obtain-the-phone-number-of-the-android-phone?lq=1) have much better explanations. – user1643723 Mar 05 '16 at 06:04
  • Define "not working." – Kevin Krumwiede Mar 05 '16 at 06:15

2 Answers2

1

You can use these lines of code to fetch cell phone numbers from your phone contacts picker.

try {
Intent i = new     Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(i, PICK_CONTACT);
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.d("TAG", "Pick Contact ERROR" + e.toString());
}

and use OnActivityResult to get contacts and save it anywhere you want (Like this)

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT && resultCode == RESULT_OK) {
Uri contactUri = data.getData();
cursor = getContentResolver().query(contactUri, null, null, null, null);
cursor.moveToFirst();
String PhoneNumber =     cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String ContactName =     cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
Vishal Chugh
  • 123
  • 1
  • 10
0

You can try below code:

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

And for this code to work you need Permission:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
Android Geek
  • 8,956
  • 2
  • 21
  • 35