本文主要是介绍安卓中使用ContentProvider获取和添加联系人,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一样,因为这是对通讯录进行操作,因此我们需要添加相应的权限。
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
代码如下所示:
package com.itfom.contentprovider;import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class MainActivity extends Activity {private static final String TAG = "ContactTest";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//实例化控件并绑定监听事件Button btn_get=(Button) findViewById(R.id.btn_get);Button btn_add=(Button) findViewById(R.id.btn_add);//设置监听事件//添加联系人btn_add.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {ContentValues values = new ContentValues();//首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactIdUri rawContactUri = MainActivity.this.getContentResolver().insert(RawContacts.CONTENT_URI, values);long rawContactId = ContentUris.parseId(rawContactUri);//往data表入姓名数据values.clear();values.put(Data.RAW_CONTACT_ID, rawContactId);values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);values.put(StructuredName.GIVEN_NAME, "zhangsan");MainActivity.this.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);//往data表入电话数据values.clear();values.put(android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId);values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);values.put(Phone.NUMBER, "5554");values.put(Phone.TYPE, Phone.TYPE_MOBILE);MainActivity.this.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);//往data表入Email数据values.clear();values.put(android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId);values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);values.put(Email.DATA, "xsc218@126.com");values.put(Email.TYPE, Email.TYPE_WORK);MainActivity.this.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);}});//获取联系人btn_get.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {ContentResolver contentResolver = MainActivity.this.getContentResolver();Uri uri = Uri.parse("content://com.android.contacts/contacts");Cursor cursor = contentResolver.query(uri, null, null, null, null);while(cursor.moveToNext()){// 获取联系人姓名StringBuilder sb = new StringBuilder();String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); sb.append("contactId=").append(contactId).append(",name=").append(name);//获取联系人手机号码Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); while(phones.moveToNext()){String phone = phones.getString(phones.getColumnIndex("data1")); sb.append(",phone=").append(phone);}//获取联系人emailCursor emails = contentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); while(emails.moveToNext()){String email = emails.getString(emails.getColumnIndex("data1")); sb.append(",email=").append(email);}Log.i(TAG, sb.toString());}}});}
}
这篇关于安卓中使用ContentProvider获取和添加联系人的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!