How can I get my phone number in Android? When I use:
如何在Android系统中获取我的电话号码?当我使用:
TelephonyManager tMgr =(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
and use:
和使用:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
it returns null, why?
它返回null,为什么?
6 个解决方案
#1
23
From the documentation:
从文档:
Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable.
返回第一行的电话号码字符串,例如GSM电话的MSISDN。如果不可用,返回null。
So you have done everything right, but there is no phone number stored.
所以你做的一切都是对的,但是没有电话号码存储。
If you get null
, you could display something to get the user to input the phone number on his/her own.
如果您获得null,您可以显示一些内容来让用户自己输入电话号码。
#2
11
If the function you called returns null, it means your phone number is not registered in your contact list.
如果您调用的函数返回null,则意味着您的电话号码没有在您的联系人列表中注册。
If instead of the phone number you just need an unique number, you may use the sim card's serial number:
如果你只需要一个唯一的号码而不是电话号码,你可以使用sim卡的序列号:
TelephonyManager telemamanger = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();
#3
9
As Answered here
在这里回答
Use below code :
使用下面的代码:
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
In AndroidManifest.xml, give the following permission:
在AndroidManifest。xml,请给出以下权限:
<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.
但是请记住,这个代码并不总是有效的,因为手机号码依赖于SIM卡和网络运营商/手机运营商。
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!
另外,试着查看手机——>设置——> About——>手机身份,如果你能在那里查看电话号码,从上面的代码中获取电话号码的概率会更高。如果您无法在设置中查看电话号码,那么您将无法通过此代码获得!
Suggested Workaround:
建议解决方案:
- Get the user's phone number as manual input from the user.
- 获取用户的电话号码作为用户的手动输入。
- Send a code to the user's mobile number via SMS.
- 通过SMS向用户的移动号码发送代码。
- Ask user to enter the code to confirm the phone number.
- 请用户输入密码确认电话号码。
- Save the number in sharedpreference.
- 在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.
在应用程序首次发布期间,将以上4个步骤作为一次活动。稍后,无论何时需要电话号码,请使用共享首选项中的可用值。
#4
2
private String getMyPhoneNumber(){
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}
private String getMy10DigitPhoneNumber(){
String s = getMyPhoneNumber();
return s.substring(2);
}
#5
0
Method 1:
方法1:
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
With below permission
使用下面的权限
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Method 2:
方法2:
There is another way you will be able to get your phone number, I haven't tested this on multiple devices but above code is not working every time.
还有另外一种方法,你可以得到你的电话号码,我没有在多台设备上测试过,但是上面的代码不是每次都能工作。
Try below code:
试试下面的代码:
String main_data[] = {"data1", "is_primary", "data3", "data2", "data1", "is_primary", "photo_uri", "mimetype"};
Object object = getContentResolver().query(Uri.withAppendedPath(android.provider.ContactsContract.Profile.CONTENT_URI, "data"),
main_data, "mimetype=?",
new String[]{"vnd.android.cursor.item/phone_v2"},
"is_primary DESC");
if (object != null) {
do {
if (!((Cursor) (object)).moveToNext())
break;
String s1 = ((Cursor) (object)).getString(4);
} while (true);
((Cursor) (object)).close();
}
You will need to add these two permissions.
您将需要添加这两个权限。
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
Hope this helps, Thanks!
希望这可以帮助,谢谢!
#6
0
Robi Code is work for me, just put if !null so that if phone number is null, user can fill the phone number by him/her self.
罗比代码是为我工作的,只需输入if !null,如果电话号码是空的,用户可以自己填写电话号码。
editTextPhoneNumber = (EditText) findViewById(R.id.editTextPhoneNumber);
TelephonyManager tMgr;
tMgr= (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
if (mPhoneNumber != null){
editTextPhoneNumber.setText(mPhoneNumber);
}
#1
23
From the documentation:
从文档:
Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable.
返回第一行的电话号码字符串,例如GSM电话的MSISDN。如果不可用,返回null。
So you have done everything right, but there is no phone number stored.
所以你做的一切都是对的,但是没有电话号码存储。
If you get null
, you could display something to get the user to input the phone number on his/her own.
如果您获得null,您可以显示一些内容来让用户自己输入电话号码。
#2
11
If the function you called returns null, it means your phone number is not registered in your contact list.
如果您调用的函数返回null,则意味着您的电话号码没有在您的联系人列表中注册。
If instead of the phone number you just need an unique number, you may use the sim card's serial number:
如果你只需要一个唯一的号码而不是电话号码,你可以使用sim卡的序列号:
TelephonyManager telemamanger = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();
#3
9
As Answered here
在这里回答
Use below code :
使用下面的代码:
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
In AndroidManifest.xml, give the following permission:
在AndroidManifest。xml,请给出以下权限:
<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.
但是请记住,这个代码并不总是有效的,因为手机号码依赖于SIM卡和网络运营商/手机运营商。
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!
另外,试着查看手机——>设置——> About——>手机身份,如果你能在那里查看电话号码,从上面的代码中获取电话号码的概率会更高。如果您无法在设置中查看电话号码,那么您将无法通过此代码获得!
Suggested Workaround:
建议解决方案:
- Get the user's phone number as manual input from the user.
- 获取用户的电话号码作为用户的手动输入。
- Send a code to the user's mobile number via SMS.
- 通过SMS向用户的移动号码发送代码。
- Ask user to enter the code to confirm the phone number.
- 请用户输入密码确认电话号码。
- Save the number in sharedpreference.
- 在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.
在应用程序首次发布期间,将以上4个步骤作为一次活动。稍后,无论何时需要电话号码,请使用共享首选项中的可用值。
#4
2
private String getMyPhoneNumber(){
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}
private String getMy10DigitPhoneNumber(){
String s = getMyPhoneNumber();
return s.substring(2);
}
#5
0
Method 1:
方法1:
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
With below permission
使用下面的权限
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Method 2:
方法2:
There is another way you will be able to get your phone number, I haven't tested this on multiple devices but above code is not working every time.
还有另外一种方法,你可以得到你的电话号码,我没有在多台设备上测试过,但是上面的代码不是每次都能工作。
Try below code:
试试下面的代码:
String main_data[] = {"data1", "is_primary", "data3", "data2", "data1", "is_primary", "photo_uri", "mimetype"};
Object object = getContentResolver().query(Uri.withAppendedPath(android.provider.ContactsContract.Profile.CONTENT_URI, "data"),
main_data, "mimetype=?",
new String[]{"vnd.android.cursor.item/phone_v2"},
"is_primary DESC");
if (object != null) {
do {
if (!((Cursor) (object)).moveToNext())
break;
String s1 = ((Cursor) (object)).getString(4);
} while (true);
((Cursor) (object)).close();
}
You will need to add these two permissions.
您将需要添加这两个权限。
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
Hope this helps, Thanks!
希望这可以帮助,谢谢!
#6
0
Robi Code is work for me, just put if !null so that if phone number is null, user can fill the phone number by him/her self.
罗比代码是为我工作的,只需输入if !null,如果电话号码是空的,用户可以自己填写电话号码。
editTextPhoneNumber = (EditText) findViewById(R.id.editTextPhoneNumber);
TelephonyManager tMgr;
tMgr= (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
if (mPhoneNumber != null){
editTextPhoneNumber.setText(mPhoneNumber);
}