我尝试使用2种方法来检索我的电话号码,但是它们都不起作用。我用过:
我确实获得了网络名称,国家(地区)iso和IMEI,但是每当我尝试返回Number时,它什么都不会返回。
我还为这些添加了所有必需的权限!我的清单如下:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
使用 TelephonyManager 的代码:
TelephonyManager phoneMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
phoneMgr.getLine1Number()
使用 SubscriptionManager 的代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
List<SubscriptionInfo> subscription = SubscriptionManager.from(getApplicationContext()).getActiveSubscriptionInfoList();
for (int i = 0; i < subscription.size(); i++) {
SubscriptionInfo info = subscription.get(i);
Log.e("TAG", "number " + info.getNumber());
Log.e("TAG", "network name : " + info.getCarrierName());
Log.e("TAG", "country iso " + info.getCountryIso());
}
}
在两次尝试中我一无所获!
还有其他获取电话号码的方法吗,或者我做错了什么?
答案 0 :(得分:2)
如今,TelephonyManager
对我们没有帮助。对此,未经许可的Play服务API就是一个很好的解决方案。
此依赖关系对此很有用
implementation 'com.google.android.gms:play-services-auth:16.0.1'
现在在您的 Activity.java 中:
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Auth.CREDENTIALS_API)
.build();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
在此之后,请输入电话号码:
HintRequest hintRequest = new HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build();
PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(mGoogleApiClient, hintRequest);
try {
startIntentSenderForResult(intent.getIntentSender(), 1008, null, 0, 0, 0, null);
} catch (IntentSender.SendIntentException e) {
Log.e("", "Could not start hint picker Intent", e);
}
现在您需要像这样处理onActivityResult
中的响应:
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1008:
if (resultCode == RESULT_OK) {
Credential cred = data.getParcelableExtra(Credential.EXTRA_KEY);
// cred.getId====: ====+919*******
Log.e("cred.getId", cred.getId());
userMob = cred.getId();
} else {
// Sim Card not found!
Log.e("cred.getId", "1008 else");
return;
}
break;
}
}
答案 1 :(得分:2)
通过使用以下代码,您可以获取当前设备的电话号码,并在选择电话号码后激活活动结果。
等级:
implementation 'com.google.android.gms:play-services-auth:17.0.0'
implementation 'com.google.android.gms:play-services-auth-api-phone:17.0.0'
设置Google API客户端:
//set google api client for hint request
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.enableAutoManage(this, this)
.addApi(Auth.CREDENTIALS_API)
.build();
获取可用号码:
public void getHintPhoneNumber() {
HintRequest hintRequest =
new HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build();
PendingIntent mIntent = Auth.CredentialsApi.getHintPickerIntent(mGoogleApiClient, hintRequest);
try {
startIntentSenderForResult(mIntent.getIntentSender(), RESOLVE_HINT, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
在onActivityResult中获取选定的号码:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Result if we want hint number
if (requestCode == RESOLVE_HINT) {
if (resultCode == Activity.RESULT_OK) {
Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
// credential.getId(); <-- will need to process phone number string
inputMobileNumber.setText(credential.getId());
}
}
}
参考: https://androidwave.com/automatic-sms-verification-android/
答案 2 :(得分:1)
您是否已授予READ_PHONE_STATE权限?
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
答案 3 :(得分:1)
getLine1Number()应该可以解决问题,但是根据SIM卡的不同,它可能无法始终获取电话号码
我建议,如果您没有从 getLine1Number()获取该号码,请让用户手动输入。 (这是iOS用户必须要做的事情
编辑:
此外,从Android 10开始,您不应该使用IMEI,否则您将无权获取该信息
答案 4 :(得分:1)
我发现@bhoomika的答案很有用,但现在不推荐使用 GoogleApiClient 。因此,您可以改为使用CredentialsClient 。
下面是我用来触发电话号码提示对话框的方法(此方法在助手类中)。
$ python dopt.py abc.txt
usage: dopt.py [-h] [--files [FILES]]
dopt.py: error: unrecognized arguments: abc.txt
以下是我对相应的onActivityResult的处理(我的活动代码在Kotlin中)
public void requestPhoneNumberHint(Activity currentActivity) {
HintRequest hintRequest = new HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build();
CredentialsClient credentialsClient = Credentials.getClient(currentActivity);
PendingIntent intent = credentialsClient.getHintPickerIntent(hintRequest);
try {
uiListener.getCurrentActivity().startIntentSenderForResult(intent.getIntentSender(),
RESOLVE_PHONE_NUMBER_HINT, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}