How to get phone number Android 10  Programmatically in android studio

Assalam o alaikom

How are you guys.

Dear developers. using this code you can show sim card number. when you put in your own phone. I am sharing you image of this functionality. How it will be show your own contacts number. okay.




build.Gradle Library in App 
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.google.android.gms:play-services-auth:19.0.0'
}
library sync your project and Implement this code.

public class MainActivity extends AppCompatActivity {


String str_PhoneNumber;
Button btn_contactNumber;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
btn_contactNumber = findViewById(R.id.id_contactNumber);
btn_contactNumber.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(LTESignalDetection_ActivityRecordNew.this)
.addConnectionCallbacks(LTESignalDetection_ActivityRecordNew.this)
.addOnConnectionFailedListener(LTESignalDetection_ActivityRecordNew.this)
.addApi(Auth.CREDENTIALS_API)
.build();

if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}

HintRequest hintRequest = new HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build();

PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(mGoogleApiClient, hintRequest);
try {
startIntentSenderForResult(intent.getIntentSender(), 1008, null, 0, 0, 0, null);
} catch (IntentSender.SendIntentException e) {
Log.e("", "Could not start hint picker Intent", e);
}

}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {
case 1008:
if (resultCode == RESULT_OK) {
Credential cred = data.getParcelableExtra(Credential.EXTRA_KEY);
// cred.getId====: ====+919*******
Log.e("anees", cred.getId());
str_PhoneNumber = cred.getId();


} else {
// Sim Card not found!
Log.e("cred.getId", "1008 else");

return;
}


break;
}
}
} XML file
main_activity

<RelativeLayout
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"
android:background="@color/colorActivityBackground"
tools:context=".LTESignalDetection_ActivityRecordNew">
<Button
android:id="@+id/id_contactNumber"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:background="@drawable/new_recording_button_background"
android:layout_gravity="center"
android:drawablePadding="5dp"
android:text="My Phone Number"
android:layout_marginTop="7dp"/>
</RelativeLayout>
Enjoy!!!

Post a Comment

Previous Post Next Post