标签:
读取联系人重要的权限: <uses-permission android:name="android.permission.READ_CONTACTS"/> //如果需要添加联系人,权限是write //代码 package com.example.readcontacts; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv_show = (TextView) findViewById(R.id.tv_show); ContentResolver cr = getContentResolver(); Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");// id主表 Cursor primarytable = cr.query(uri, null, null, null, null); while (primarytable.moveToNext()) { String contact_id = primarytable.getString(primarytable .getColumnIndex("contact_id")); if (contact_id != null) {//判断联系人主表该字段是否为null,因为手机删除的联系人并不是从数据库删除而是主表设置为了null // 查到了表中人的id后再查人的详细信息 Uri uridata = Uri.parse("content://com.android.contacts/data"); Cursor dataquery = cr.query(uridata, new String[]{"data1","mimetype"}, "raw_contact_id=?",new String[]{contact_id}, null); while (dataquery.moveToNext()) { String data1 = dataquery.getString(dataquery.getColumnIndex("data1")); String mimetype = dataquery.getString(dataquery .getColumnIndex("mimetype")); String name = null; if ("vnd.android.cursor.item/email_v2".equals(mimetype)) { name = "邮箱:"; } else if ("vnd.android.cursor.item/nickname".equals(mimetype)) { name = "昵称:"; } else if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) { name = "电话:"; } else if ("vnd.android.cursor.item/name".equals(mimetype)) { name = "姓名:"; } else if ("vnd.android.cursor.item/postal-address_v2".equals(mimetype)) { name = "地址:"; } System.out.println(name + data1); tv_show.append(name+":"+data1+"\n"); } dataquery.close();//读取完了每一个联系所有信息关闭一次联系内容表资源 } } primarytable.close();//关闭主表资源 } }
标签:
原文地址:http://www.cnblogs.com/lizhanqi/p/5653991.html