For one of my apps, I need the user to select one of his existing contacts or to create a new one. Picking one is clearly easy to do with the following code:
Try if you use Kotlin
fun Fragment.saveContact(name: String?, phone: String?) {
if (name != null && phone != null) {
val addContactIntent = Intent(Intent.ACTION_INSERT)
addContactIntent.type = ContactsContract.Contacts.CONTENT_TYPE
addContactIntent.putExtra(ContactsContract.Intents.Insert.NAME, name)
addContactIntent.putExtra(ContactsContract.Intents.Insert.PHONE, phone)
startActivity(addContactIntent)
}
}
Intent intent = new Intent(
ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,
Uri.parse("tel:" + phoneNumber));
intent.putExtra(ContactsContract.Intents.EXTRA_FORCE_CREATE, true);
startActivity(intent);
this code might help you.
Finally found a solution, I'm sharing it with you. That's only a fix for Android version above 4.0.3 and sup. It doesn't work on 4.0 to 4.0.2.
i = new Intent(Intent.ACTION_INSERT);
i.setType(Contacts.CONTENT_TYPE);
if (Integer.valueOf(Build.VERSION.SDK) > 14)
i.putExtra("finishActivityOnSaveCompleted", true); // Fix for 4.0.3 +
startActivityForResult(i, PICK_CONTACT_REQUEST);
In Xamarin.forms and Xamarin.Android c#.
Android.Content.Intent intent = new
Android.Content.Intent(Android.Content.Intent.ActionInsert);
intent.SetType(Android.Provider.ContactsContract.Contacts.ContentType);
intent.PutExtra(Android.Provider.ContactsContract.Intents.ExtraForceCreate,
true);
StartActivity(intent);
// Creates a new Intent to insert a contact
Intent intent = new Intent(Intents.Insert.ACTION);
// Sets the MIME type to match the Contacts Provider
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
If you already have details for the contact, such as a phone number or email address, you can insert them into the intent as extended data. For a key value, use the appropriate constant from Intents.Insert. The contacts app displays the data in its insert screen, allowing users to make further edits and additions.
private EditText mEmailAddress = (EditText) findViewById(R.id.email);
private EditText mPhoneNumber = (EditText) findViewById(R.id.phone);
/*
* Inserts new data into the Intent. This data is passed to the
* contacts app's Insert screen
*/
// Inserts an email address
intent.putExtra(Intents.Insert.EMAIL, mEmailAddress.getText())
/*
* In this example, sets the email type to be a work email.
* You can set other email types as necessary.
*/
.putExtra(Intents.Insert.EMAIL_TYPE, CommonDataKinds.Email.TYPE_WORK)
// Inserts a phone number
.putExtra(Intents.Insert.PHONE, mPhoneNumber.getText())
/*
* In this example, sets the phone type to be a work phone.
* You can set other phone types as necessary.
*/
.putExtra(Intents.Insert.PHONE_TYPE, Phone.TYPE_WORK);
Once you've created the Intent, send it by calling startActivity().
/* Sends the Intent
*/
startActivity(intent);
Note : import "intents" of "ContactsContract"
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.