3

我正在使用此代码获取手机号码。但问题是我收到的号码附有国家代码。我想要一个没有国家代码的号码(只有手机号码)。我不能减少数字的长度,因为所有国家都有不同的国家代码长度。

那么我能为此做些什么。

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

这个代码用于设置手机号码。

@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) {
            if (data != null) {
                Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
                // credential.getId();  <-- will need to process phone number string
                Log.v("wkdcbgwc",credential.getId());
                etMobile.setText(credential.getId());
            }

        }
    }
}

我不知道该怎么做。

4

3 回答 3

2

我知道我回答有点晚了,但这可能会帮助其他人寻求相同问题的答案。只需在 onActivityResult 中替换此代码

if (data != null) {
   Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
   if (credential.getId().startsWith("+")) {
      if (credential.getId().length() == 13) {
         etMobile.setText(credential.getId().substring(3));
      } else if (credential.getId().length() == 14) {
           etMobile.setText(credential.getId().substring(4));
      }
   } else {
       etMobile.setText(credential.getId());
   }
}
于 2020-04-24T16:00:44.477 回答
0

如果你想分开国家代码和手机号码:-

@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) {
            if (data != null) {
                Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);

                if (credential.getId().startsWith("+")) {
                    switch(credential.getId().length()){
                        case 12:
                            inputCountryCode.setText(credential.getId().substring(1,2));
                            inputMobileNumber.setText(credential.getId().substring(2));
                            break;
                        case 13:
                            inputCountryCode.setText(credential.getId().substring(1,3));
                            inputMobileNumber.setText(credential.getId().substring(3));
                            break;
                        case 14:
                            inputCountryCode.setText(credential.getId().substring(1,4));
                            inputMobileNumber.setText(credential.getId().substring(4));
                            break;
                        default:
                            Toast.makeText(this, "Please enter your phone number manually!",
                                    Toast.LENGTH_LONG).show();
                            break;
                    }

                } else {
                    inputMobileNumber.setText(credential.getId());
                }
            }

        }
    }
}
于 2021-09-03T11:51:13.433 回答
0

将您的 onActivityResult 替换为:-

  @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESOLVE_HINT) {
        if (resultCode == Activity.RESULT_OK) {
            if (data != null) {
                Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);

                if (credential.getId().startsWith("+")) {
                    switch(credential.getId().length()){
                        case 12:
                            etMobile.setText(credential.getId().substring(2));
                            break;
                        case 13:
                            etMobile.setText(credential.getId().substring(3));
                            break;
                        case 14:
                            etMobile.setText(credential.getId().substring(4));
                            break;
                        default:
                             Toast.makeText(this, "Please enter your phone number manually!",
                                    Toast.LENGTH_LONG).show();
                            break;
                    }

                } else {
                    etMobile.setText(credential.getId());
                }
            }

        }
    }
}
于 2021-09-03T11:40:22.430 回答