简体   繁体   中英

Android insert contact with Intents.Insert not works

I'm trying to develop a app in this I need to access the contacts to add phone number with name. for this I'm using following code.

Intent intent = new Intent(Intents.Insert.ACTION);
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
intent.putExtra(Intents.Insert.NAME, "Silambarasan");
intent.putExtra(Intents.Insert.PHONE, 9876543210L);
intent.putExtra(Intents.Insert.PHONE_TYPE, Phone.TYPE_MOBILE);
startActivity(intent);

This code opens the add contact screen, but in the screen only name "Silambarasan" sent from this code. Phone "Number :9876543210L "is not passed.

Do I use correct way?

Thanks in advance.

Try this,

Intent addPersonIntent = new Intent(Intent.ACTION_INSERT);
                                        addPersonIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);

                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.NAME, "name");
                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.PHONE, "phone");
                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.EMAIL, "email");
                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.POSTAL, "address");

                                        startActivityForResult(addPersonIntent, CREATE_NEW);

From ContactsContract.Intents.Insert documentation

public static final String PHONE : Added in API level 5

The extra field for the contact phone number.

Type: String // Clears that you need to pass String value for this extra

Constant Value: "phone"


Simply pass number as String into extra that will work.

So change intent.putExtra(Intents.Insert.PHONE, 9876543210L);

with

 intent.putExtra(Intents.Insert.PHONE, "9876543210");

Why don't you just pass it like this?

intent.putExtra("nameForThisExtra", "Silambarasan")
intent.putExtra("nameForThisExtra", "9876543210L");
intent.putExtra("nameForThisExtra", Phone.TYPE_MOBILE);

and call it in the ohter activity like this:

String name = getIntent().getStringExtra("nameForThisExtra");
String number = getIntent().getStringExtra("nameForThisExtra");

That should work.

The first parameter is the name you want to give this Extra, the second parameter the value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM