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:
I've gathered all kinds of Intents that I've found for adding a contact. Here's the result in a single function:
@JvmStatic
fun prepareCreateContactIntent(context: Context, contactName: String? = null, phoneNumber: String? = null): Intent? {
var intent = Intent(Intents.Insert.ACTION)
intent.type = ContactsContract.RawContacts.CONTENT_TYPE
val packageManager = context.packageManager
var resolveActivity: ResolveInfo? = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY or PackageManager.GET_RESOLVED_FILTER)
if (resolveActivity == null) {
intent = Intent(Intent.ACTION_INSERT).setType(ContactsContract.Contacts.CONTENT_TYPE)
resolveActivity = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY or PackageManager.GET_RESOLVED_FILTER)
}
if (resolveActivity == null) {
intent = Intent(Intents.SHOW_OR_CREATE_CONTACT, if (phoneNumber == null) Uri.parse("tel:") else Uri.parse("tel:$phoneNumber"))
intent.putExtra(Intents.Insert.NAME, contactName)
resolveActivity = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY or PackageManager.GET_RESOLVED_FILTER)
}
intent.putExtra(Intents.Insert.NAME, contactName)
intent.putExtra(Intents.Insert.PHONE, phoneNumber)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
return if (resolveActivity == null) null else intent
}
If it returns null, it means there is no app that can handle it, and so you should add it yourself, or show something to the user.
You can choose whether you want to add the contact automatically, or open the add contact activity with pre-filled data:
/**
* Open the add-contact screen with pre-filled info
*
* @param context
* Activity context
* @param person
* {@link Person} to add to contacts list
*/
public static void addAsContactConfirmed(final Context context, final Person person) {
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.NAME, person.name);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, person.mobile);
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, person.email);
context.startActivity(intent);
}
/**
* Automatically add a contact into someone's contacts list
*
* @param context
* Activity context
* @param person
* {@link Person} to add to contacts list
*/
public static void addAsContactAutomatic(final Context context, final Person person) {
String displayName = person.name;
String mobileNumber = person.mobile;
String email = person.email;
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());
// Names
if (displayName != null) {
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
displayName).build());
}
// Mobile Number
if (mobileNumber != null) {
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, mobileNumber)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build());
}
// Email
if (email != null) {
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK).build());
}
// Asking the Contact provider to create a new contact
try {
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(context, "Contact " + displayName + " added.", Toast.LENGTH_SHORT)
.show();
}
int INSERT_CONTACT_REQUEST=2;
i = new Intent(Intent.ACTION_INSERT,Contacts.CONTENT_URI);
startActivityForResult(i, INSERT_CONTACT_REQUEST);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method stub
if(requestCode == INSERT_CONTACT_REQUEST)
{
if (resultCode == RESULT_OK)
{
Toast.makeText().show(getApplicationContext(),"Added_Succesfully",Toast.LENGTH_SHORT);
}else if(resultCode == RESULT_CANCELED)
{
Toast.makeText().show(getApplicationContext(),"Contacts Adding Error",Toast.LENGTH_SHORT);
}
}
}