在安卓手机中使用HintRequest获取无国家代码的手机号,可以通过以下步骤实现:
implementation 'com.google.android.gms:play-services-auth:19.2.0'
import com.google.android.gms.auth.api.phone.HintRequest;
// ...
HintRequest hintRequest = new HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build();
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.credentials.Credential;
import com.google.android.gms.auth.api.credentials.CredentialsOptions;
import com.google.android.gms.auth.api.credentials.HintRequest;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Status;
// ...
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
.addApi(Auth.CREDENTIALS_API, new CredentialsOptions.Builder().forceEnableSaveDialog().build())
.build();
googleApiClient.connect();
HintRequest hintRequest = new HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build();
PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(googleApiClient, hintRequest);
startIntentSenderForResult(intent.getIntentSender(), REQUEST_CODE_HINT, null, 0, 0, 0);
在处理返回结果的地方,可以通过判断返回的resultCode来确定用户是否选择了手机号码。如果resultCode为RESULT_OK,可以通过Credential对象获取到用户选择的手机号码。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_HINT) {
if (resultCode == RESULT_OK) {
Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
String phoneNumber = credential.getId(); // 获取到用户选择的手机号码
// 处理手机号码
} else if (resultCode == RESULT_CANCELED) {
// 用户取消了选择手机号码
}
}
}
这样,你就可以在安卓手机中使用HintRequest获取无国家代码的手机号了。
领取专属 10元无门槛券
手把手带您无忧上云