如何在Android中以编程方式添加联系人?

whlutmcx  于 2023-03-06  发布在  Android
关注(0)|答案(3)|浏览(165)

我已经尝试过了,但是没有添加联系人!

ContentResolver cr = this.getContentResolver();
ContentValues cv = new ContentValues();
cv.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, "sai1");
cv.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "9640954335");
cv.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
cr.insert(ContactsContract.RawContacts.CONTENT_URI, cv);
pjngdqdw

pjngdqdw1#

尝试使用以下代码

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        int rawContactInsertIndex = ops.size();

        Log.i("Line38", "Here");
           ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)  
                        .withValue(RawContacts.ACCOUNT_TYPE, AccountManager.KEY_ACCOUNT_TYPE)          
                        .withValue(RawContacts.ACCOUNT_NAME, AccountManager.KEY_ACCOUNT_NAME)          
                        .build());

        ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)      
                        .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)      
                        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)      
                        .withValue(StructuredName.DISPLAY_NAME, "u232786seee")
                        .withValue(StructuredName.IN_VISIBLE_GROUP,true)
                        .build());

        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
        .withValue(ContactsContract.Data.MIMETYPE,
                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,"23232343434")
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, "4343")
        .build());

        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
        .withValue(ContactsContract.Data.MIMETYPE,
                ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Email.DATA, "")
        .withValue(ContactsContract.CommonDataKinds.Email.TYPE, "")
        .build());

        //Log.i("Line43", Data.CONTENT_URI.toString()+" - "+rawContactInsertIndex);

        try {
                getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        } catch (OperationApplicationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }

并在清单文件中添加以下权限

<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
fsi0uk1n

fsi0uk1n2#

其他选项....

public void insert() {
    Intent intent = new Intent(
            ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,
            ContactsContract.Contacts.CONTENT_URI);
    intent.setData(Uri.parse("tel:911"));//specify your number here
    intent.putExtra(ContactsContract.Intents.Insert.COMPANY, "Emergency USA");
    startActivity(intent);
    Toast.makeText(this, "Record inserted", Toast.LENGTH_SHORT).show();
}
mitkmikd

mitkmikd3#

我知道这个问题已经解决了,但是我在修改我正在编写的代码时遇到了麻烦,所以这里有另一个更“基本”的选择

public void addContact(View view)
    {
        String name = editName.getText().toString();
        String number = editNumber.getText().toString();

        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()
        );

        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, name)
                    .build()
            );

        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,
                    number
                )
                .withValue(
                    ContactsContract.CommonDataKinds.Phone.TYPE,
                    ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE
                )
                .build()
            );

        try
        {
            getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        }
        catch (OperationApplicationException e)
        {}
        catch (RemoteException e)
        {}
    }

现在我必须使用的导入:

// To Android App
import android.app.*;
import android.os.*;
import java.util.*;
import android.view.*;
import android.widget.*;

// Only Add Contact
import android.content.*;
import android.provider.*;

相关问题