I need help in phone selector API (Eg. Tez/PhonePe/Bhim) . Referred this blog https://android-developers.googleblog.com/2017/10/effective-phone-number-verification.html. When am I trying to get phone numbers, which results empty dialog and disabled immediately. Please suggest me like how to get phone number without permissions.
I implemented this feature in my android application. Check this:
First, Open build.gradle inside your app module and add:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
//this is the main library to add for phone number detection
implementation "com.google.android.gms:play-services-auth:16.0.1"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Second, NumberDetectActivity.java:
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentSender;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
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.HintRequest;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
public class NumberDetectActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
Button getNumber;
TextView textView;
String mobNumber;
private final static int RESOLVE_HINT = 1011;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_number_detect);
getNumber = (Button) findViewById(R.id.btn_get);
textView = (TextView) findViewById(R.id.phone);
getNumber.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
requestPhoneNumber();
}
});
}
protected void requestPhoneNumber() {
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Auth.CREDENTIALS_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
googleApiClient.connect();
HintRequest hintRequest = new HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build();
PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(googleApiClient, hintRequest);
try {
startIntentSenderForResult(intent.getIntentSender(), RESOLVE_HINT, null, 0, 0, 0, null);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESOLVE_HINT) {
if (resultCode == RESULT_OK) {
Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
if (credential != null) {
mobNumber = credential.getId();
textView.setText(mobNumber);
} else {
textView.setText("No phone number available");
}
}
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
third, activity_number_detect.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NumberDetectActivity">
<Button
android:id="@+id/btn_get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PRESS"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/phone"
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
The HintRequest
api provided by google has not fully accomplished its feature and is quite buggy , Its works fine in google devices as said by developers:
OEM issue that their pixel or nexus phone working well
https://issuetracker.google.com/issues/77884951 .
https://github.com/googlesamples/android-credentials/issues/27
来源:https://stackoverflow.com/questions/48761521/phone-selector-api-fails-to-get-phone-numbers