Reputation: 56
I'm making an application in which user can add users from the application into his contacts.
Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, bean.getMobileNo());
intent.putExtra(ContactsContract.Intents.Insert.NAME, bean.getName());
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, bean.getEmailID());
startActivity(intent);
so adding it is not the issue the issue when it goes to add contact screen and if the user presses back button the contact is getting saved even if the user doesn't want to save the contact.
I want to do this using Intent only not through app. Is there any solution for this or is it device specific?
Upvotes: 3
Views: 2980
Reputation: 4335
Try this,
// Declare
static final int PICK_CONTACT = 1;
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
//code
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
null, null);
phones.moveToFirst();
cNumber = phones.getString(phones.getColumnIndex("data1"));
System.out.println("number is:" + cNumber);
}
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
}
break;
}
}
This may helps you.
Upvotes: 1
Reputation: 37768
If I understand your post properly, the issue is that regardless if the user wants to add a contact or not, the contact information gets added anyway.
As I noticed, you are already calling startActivity(intent)
, which saves the contact. Only run startActivity(intent)
if the user has already approved/verified that he wants to add the contact. I suggest using a Dialog for the confirmation. If the user accepts, run startActivity(intent)
, if not, then simply disregard the action. Like so:
if (true){ // user accepts
Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, bean.getMobileNo());
intent.putExtra(ContactsContract.Intents.Insert.NAME, bean.getName());
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, bean.getEmailID());
startActivity(intent);
} else {
// your code here.
}
I think this post might also be useful for you. Cheers! :D
Upvotes: 0
Reputation: 442
Try this
/** * Open the add-contact screen with pre-filled info */
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.NAME, bean.getName());
intent.putExtra(ContactsContract.Intents.Insert.PHONE, bean.getMobileNo());
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, bean.getEmailID());
context.startActivity(intent);
Upvotes: 5