Phone Selector API fails to get phone numbers
Asked Answered
S

2

3

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.

Sculpturesque answered 13/2, 2018 at 7:28 Comment(1)
Adding specific errors, screenshots and implemented code goes a long way in helping viewers help you debug the issue. Right now it is hard to say what might be happening without knowing anything about the implementation.Prompt
B
4

I implemented this feature in my android application. Check this:

First, Open build.gradle inside your app module and add:

dependencies {
    ...

    //this is the main library to add for phone number detection
    implementation "com.google.android.gms:play-services-auth:16.0.1" 
}

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>
Bobbyebobbysocks answered 24/10, 2018 at 18:31 Comment(0)
A
2

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

Abana answered 6/10, 2019 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.