如何使用ContentProviderOperation插入新联系人

时间:2019-06-01 10:54:18

标签: android

我的表单如下图所示,我想通过单击添加按钮并为此使用ContentProviderOperation来添加新联系人

我该怎么做?非常感谢!! My Form

这是我的onlick方法

btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            edtName = (EditText) findViewById(R.id.edtName);
            edtPhone = (EditText) findViewById(R.id.edtPhone);
            edtEmail = (EditText) findViewById(R.id.edtEmail);

        }
    });

1 个答案:

答案 0 :(得分:0)

添加联系人之前,您必须先对清单文件启用权限。

Android清单:

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

添加权限后。我创建了一个添加联系人的活动。

AddPhoneContactActivity.java文件

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.test.example.R;

public class AddPhoneContactActivity extends AppCompatActivity {

private EditText displayNameEditor;

private EditText phoneNumberEditor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_addcontact);

    setTitle("dev2qa.com - Android Add Phone Contact Example.");

    displayNameEditor = (EditText)findViewById(R.id.add_phone_contact_display_name);

    phoneNumberEditor = (EditText)findViewById(R.id.add_phone_contact_number);

    // Initialize phone type dropdown spinner.
    final Spinner phoneTypeSpinner = (Spinner)findViewById(R.id.add_phone_contact_type);
    String phoneTypeArr[] = {"Mobile", "Home", "Work"};
    ArrayAdapter<String> phoneTypeSpinnerAdaptor = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, phoneTypeArr);
    phoneTypeSpinner.setAdapter(phoneTypeSpinnerAdaptor);

    // Click this button to save user input phone contact info.
    Button savePhoneContactButton = (Button)findViewById(R.id.add_phone_contact_save_button);
    savePhoneContactButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Get android phone contact content provider uri.
            //Uri addContactsUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            // Below uri can avoid java.lang.UnsupportedOperationException: URI: content://com.android.contacts/data/phones error.
            Uri addContactsUri = ContactsContract.Data.CONTENT_URI;

            // Add an empty contact and get the generated id.
            long rowContactId = getRawContactId();

            // Add contact name data.
            String displayName = displayNameEditor.getText().toString();
            insertContactDisplayName(addContactsUri, rowContactId, displayName);

            // Add contact phone data.
            String phoneNumber = phoneNumberEditor.getText().toString();
            String phoneTypeStr = (String)phoneTypeSpinner.getSelectedItem();
            insertContactPhoneNumber(addContactsUri, rowContactId, phoneNumber, phoneTypeStr);

            Toast.makeText(getApplicationContext(),"New contact has been added, go back to previous page to see it in contacts list." , Toast.LENGTH_LONG).show();

            finish();
        }
    });
}

// This method will only insert an empty data to RawContacts.CONTENT_URI
// The purpose is to get a system generated raw contact id.
private long getRawContactId()
{
    // Inser an empty contact.
    ContentValues contentValues = new ContentValues();
    Uri rawContactUri = getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, contentValues);
    // Get the newly created contact raw id.
    long ret = ContentUris.parseId(rawContactUri);
    return ret;
}


// Insert newly created contact display name.
private void insertContactDisplayName(Uri addContactsUri, long rawContactId, String displayName)
{
    ContentValues contentValues = new ContentValues();

    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

    // Each contact must has an mime type to avoid java.lang.IllegalArgumentException: mimetype is required error.
    contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);

    // Put contact display name value.
    contentValues.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, displayName);

    getContentResolver().insert(addContactsUri, contentValues);

}

private void insertContactPhoneNumber(Uri addContactsUri, long rawContactId, String phoneNumber, String phoneTypeStr)
{
    // Create a ContentValues object.
    ContentValues contentValues = new ContentValues();

    // Each contact must has an id to avoid java.lang.IllegalArgumentException: raw_contact_id is required error.
    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

    // Each contact must has an mime type to avoid java.lang.IllegalArgumentException: mimetype is required error.
    contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);

    // Put phone number value.
    contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber);

    // Calculate phone type by user selection.
    int phoneContactType = ContactsContract.CommonDataKinds.Phone.TYPE_HOME;

    if("home".equalsIgnoreCase(phoneTypeStr))
    {
        phoneContactType = ContactsContract.CommonDataKinds.Phone.TYPE_HOME;
    }else if("mobile".equalsIgnoreCase(phoneTypeStr))
    {
        phoneContactType = ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
    }else if("work".equalsIgnoreCase(phoneTypeStr))
    {
        phoneContactType = ContactsContract.CommonDataKinds.Phone.TYPE_WORK;
    }
    // Put phone type value.
    contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, phoneContactType);

    // Insert new contact data into phone contact list.
    getContentResolver().insert(addContactsUri, contentValues);

}

// ListPhoneContactsActivity use this method to start this activity.
public static void start(Context context)
{
    Intent intent = new Intent(context, AddPhoneContactActivity.class);
    context.startActivity(intent);
}

}

activity_addcontact.xml

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="1dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:width="0dp"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Display Name : "
        android:gravity="right"/>

    <EditText
        android:id="@+id/add_phone_contact_display_name"
        android:layout_weight="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:width="0dp"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Phone Number : "
        android:gravity="right" />

    <EditText
        android:id="@+id/add_phone_contact_number"
        android:layout_weight="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:width="0dp"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Phone Type : "
        android:gravity="right" />

    <Spinner
        android:id="@+id/add_phone_contact_type"
        android:layout_weight="1.3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:spinnerMode="dropdown"/>

</LinearLayout>

<Button
    android:id="@+id/add_phone_contact_save_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Save Phone Contact"/>