Used the first part from accepted answer:
Intent i = new Intent(Intent.ACTION_INSERT);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);
if (Build.VERSION.SDK_INT > 14)
i.putExtra("finishActivityOnSaveCompleted", true); // Fix for 4.0.3 +
startActivityForResult(i, 1);
now on your result you can get phone number and also name,Since it's a been tricky and you should query two different tables which are connected by same ids.I'll post this part so it's easier for everybody:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
assert data != null;
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(Objects.requireNonNull(data.getData()), null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {//Has phoneNumber
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
while (pCur != null && pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.v("SteveMoretz", "NAME : " + name + " phoneNo : " + phoneNo);
}
if (pCur != null) {
pCur.close();
}
}
} else {
Toast.makeText(getApplicationContext(), "User canceled adding contacts", Toast.LENGTH_SHORT).show();
}
if (cursor != null) {
cursor.close();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Hope this will help somebody.
i = new Intent(Intent.ACTION_INSERT,Contacts.CONTENT_URI);
.OnActivityResult();
will return the URI of newly contact. – Miliary