MainActivity
package com.superxingyun.contentproviderdemo2;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContentResolver cr = getContentResolver();
//向联系人中插入一行数据
ContentValues values = new ContentValues();
Uri uri = cr.insert(RawContacts.CONTENT_URI, values);
//解析uri
Long raw_contact_id = ContentUris.parseId(uri);
values.clear();
//插入人名
values.put(StructuredName.RAW_CONTACT_ID, raw_contact_id);
values.put(StructuredName.DISPLAY_NAME, "劫");
values.put(StructuredName.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
uri = cr.insert(Data.CONTENT_URI, values);
//插入电话信息
values.clear();
values.put(Phone.RAW_CONTACT_ID, raw_contact_id);
values.put(Phone.NUMBER, "18989846103");
values.put(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
uri = cr.insert(Data.CONTENT_URI, values);
}
}
AndroidManifest.xnl
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.superxingyun.contentproviderdemo2">
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>