Phone number detect android studio (JAVA) using Google Api
Demo:
Why we need this feature :
- No additional permission required
- Do not required to enter phone number manually
- No Google account is required
Step 1 : Add dependency
implementation 'com.google.android.gms:play-services-auth:19.0.0'
implementation 'com.google.android.gms:play-services-identity:17.0.0'
implementation 'com.google.android.gms:play-services-auth-api-phone:17.5.0'
implementation 'com.google.android.gms:play-services-base:17.5.0'
Step 2 : MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.google.android.gms.auth.api.credentials.Credential;
import com.google.android.gms.auth.api.credentials.Credentials;
import com.google.android.gms.auth.api.credentials.CredentialsApi;
import com.google.android.gms.auth.api.credentials.HintRequest;
public class MainActivity extends AppCompatActivity {
private static final int CREDENTIAL_PICKER_REQUEST =120 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getPhoneNumber();
findViewById(R.id.change_number).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getPhoneNumber();
}
});
}
private void getPhoneNumber(){
HintRequest hintRequest = new HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build();
PendingIntent intent = Credentials.getClient(MainActivity.this).getHintPickerIntent(hintRequest);
try
{
startIntentSenderForResult(intent.getIntentSender(), CREDENTIAL_PICKER_REQUEST, null, 0, 0, 0,new Bundle());
}
catch (IntentSender.SendIntentException e)
{
e.printStackTrace();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CREDENTIAL_PICKER_REQUEST && resultCode == RESULT_OK)
{
// Obtain the phone number from the result
Credential credentials = data.getParcelableExtra(Credential.EXTRA_KEY);
/* EditText.setText(credentials.getId().substring(3));*/ //get the selected phone number
//Do what ever you want to do with your selected phone number here
Toast.makeText(this, "MOB"+credentials.getId().substring(3), Toast.LENGTH_SHORT).show();
}
else if (requestCode == CREDENTIAL_PICKER_REQUEST && resultCode == CredentialsApi.ACTIVITY_RESULT_NO_HINTS_AVAILABLE)
{
// *** No phone numbers available ***
Toast.makeText(MainActivity.this, "No phone numbers found", Toast.LENGTH_LONG).show();
}
}
}
0 Comments