android 简单的读写联系人
一、读取联系人
(1).从raw_contact表读 contact_id
(2).从data表中读取data1
(3).从mimetypes读取mimetype
1.AndroidManifest.xml 添加content权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.writecontact" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.READ_CONTACTS"/> <uses-permission android:name="android.permission.WRITE_CONTACTS"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
2.MainActivity.java
package com.example.readcontact; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view) { //获取raw_contact表中的数据 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); //获取view_data视图中的数据 Uri datauri = Uri.parse("content://com.android.contacts/data"); ContentResolver resovler = getContentResolver(); Cursor cursor = resovler.query(uri, null, null, null, null); while(cursor.moveToNext()) { String id = cursor.getString(cursor.getColumnIndex("contact_id")); System.out.println("----------raw_contact start-----------"); System.out.println("id="+id); if(id!=null) { //注意 这里获取的事data_view视图中的数据 不是data表中的数据 Cursor dataCursor = resovler.query(datauri, null, "raw_contact_id=?", new String[]{id}, null); while(dataCursor.moveToNext()) { String data = dataCursor.getString(dataCursor.getColumnIndex("data1")); String mimetype = dataCursor.getString(dataCursor.getColumnIndex("mimetype")); System.out.println("data="+data+",mimetype"+mimetype); } dataCursor.close(); } System.out.println("---------raw_contact end----------"); } cursor.close(); } }
二、写联系人
1、同读取联系人一样添加权限
2、MainActivity.java
package com.example.writecontact; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view) { //获取raw_contact表中的数据 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); //获取view_data视图中的数据 Uri datauri = Uri.parse("content://com.android.contacts/data"); ContentResolver resovler = getContentResolver(); Cursor cursor = resovler.query(uri, new String[]{"_id"}, null, null, null); cursor.moveToLast(); int lastId = cursor.getInt(0); int contact_id = lastId+1; ContentValues values = new ContentValues(); values.put("contact_id", contact_id); //向 raw_contact表中添加数据 resovler.insert(uri, values); //向data表中添加phone数据 ContentValues phoneValues = new ContentValues(); phoneValues.put("data1", "77777"); phoneValues.put("mimetype", "vnd.android.cursor.item/phone_v2"); phoneValues.put("raw_contact_id", contact_id); //向 raw_contact表中添加数据 resovler.insert(datauri, phoneValues); //向data表中添加name数据 ContentValues nameValues = new ContentValues(); nameValues.put("data1", "zhaoqi"); nameValues.put("mimetype", "vnd.android.cursor.item/name"); nameValues.put("raw_contact_id", contact_id); //向 raw_contact表中添加数据 resovler.insert(datauri, nameValues); //向data表中添加phone数据 ContentValues emailValues = new ContentValues(); emailValues.put("data1", "zhaoqi@qq.com"); emailValues.put("mimetype", "vnd.android.cursor.item/email_v2"); emailValues.put("raw_contact_id", contact_id); //向 raw_contact表中添加数据 resovler.insert(datauri, emailValues); } }