Skip to content Skip to sidebar Skip to footer

How To Get Cell Phone Number In Android?

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

Solution 1:

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

try {
Intenti=newIntent(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)

protectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT && resultCode == RESULT_OK) {
UricontactUri= data.getData();
cursor = getContentResolver().query(contactUri, null, null, null, null);
cursor.moveToFirst();
StringPhoneNumber=     cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
StringContactName=     cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));

Solution 2:

You can try below code:

TelephonyManagertMgr= (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
Stringnumber= tMgr.getLine1Number();

And for this code to work you need Permission:

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

Post a Comment for "How To Get Cell Phone Number In Android?"