2016-03-05 50 views
0

如何在android中获取手机号码? 这里是代码。但它不起作用如何在android中获取手机号码?

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

任何人都可以帮助我解决这个问题。

+1

可能的复制(HTTP [采用了android代码获取有关GSM移动手机号码]://计算器。 com/questions/16333816/get-mobile-number-on-gsm-mobile-using-android-code) –

+0

@DhawalSodhaParmar这是标记重复的较差选择。 [顶投票](http://stackoverflow.com/questions/2480288/programmatically-obtain-the-phone-number-of-the-android-phone?lq=1)有更好的解释。 – user1643723

+0

定义“不工作”。 –

回答

0

你可以试试下面的代码:

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

而这段代码工作,你需要权限:

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

我已经尝试过,但它不起作用 –

+0

@DharmeshDhameliya你会得到什么? –

+0

@DharmeshDhameliya如果你得到空,你可以显示一些东西让用户输入他/她自己的电话号码。 –

1

您可以使用这些代码行从您的手机通讯录取回手机号码选择器。

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()); 
} 

,并使用OnActivityResult获得接触并保存任何你想要的(像这样)的

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))); 
+0

你的回答很明显。但我想编号编号。 –

+0

你能告诉我你想从联系人选择器中选择联系人吗?并更多地解释你的问题。 –

相关问题