Android读取手机联系人

package com.example.contactstest;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;

public class MainActivity extends Activity {

	private static final String LINE_SEPARATOR = System.getProperty("line.separator");// 换行
	private TextView contacts_show;

	private Cursor dataCursor;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		contacts_show = (TextView) findViewById(R.id.contacts_show);

		search();
	}

	private void search() {
		final ContentResolver resolver = getApplicationContext().getContentResolver();
		final Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
		final Uri dataUri = Uri.parse("content://com.android.contacts/data");
		final Cursor cursor = resolver.query(uri, null, null, null, null);
		while (cursor.moveToNext()) {
			// 得到联系人id
			String id = cursor.getString(cursor.getColumnIndex("contact_id"));

			if (id != null) {
				// 输出结果
				contacts_show.append("contacts_id=" + id + LINE_SEPARATOR);

				dataCursor = resolver.query(dataUri, null, "raw_contact_id=?",
						new String[] { id }, null);
				while (dataCursor.moveToNext()) {
					String data1 = dataCursor.getString(dataCursor
							.getColumnIndex("data1"));
					String mimetype = dataCursor.getString(dataCursor
							.getColumnIndex("mimetype"));
					contacts_show.append("data1=" + data1 + LINE_SEPARATOR);
					contacts_show.append("mimetype=" + mimetype + LINE_SEPARATOR);
					contacts_show.append(LINE_SEPARATOR);
				}
				dataCursor.close();
			}
		}
		cursor.close();
	}

	// 查找
	private void find() {
		/**
		 * Uri的得到:
		 * 
		 * 联系人号码 Uri uri =
		 * Uri.parse("content://com.android.contacts/data/phones"); 所有联系人 Uri
		 * uri = Uri.parse("content://contacts/people"); Uri uri =
		 * ContactsContract.Contacts.CONTENT_URI; Uri uri =
		 * Uri.parse("content://icc/adn");SIM卡
		 * 
		 */

		// SIM卡Uri
		Uri SIMUri = Uri.parse("content://icc/adn");
		// 所有联系人Uri
		Uri personUri = Uri.parse("content://contacts/people");
		// 联系人手机号Uri
		Uri phoneUri = Uri.parse("content://com.android.contacts/data/phones");

		// 查询数据
		ContentResolver resolver = getApplicationContext().getContentResolver();

		/**
		 * 三种条件的Cursor对象
		 */
		// 联系人
		Cursor personCursor = resolver.query(personUri, null, null, null, null);
		// 联系人手机号
		Cursor phoneCursor = resolver.query(phoneUri, null, null, null, null);
		// SIM
		Cursor SIMCursor = resolver.query(SIMUri, null, null, null, null);
		// Cursor cursor = resolver.query(uri,null,null,null,"personid asc");

		// 取出SIM所有联系人
		while (SIMCursor.moveToNext()) {

			/**
			 * cursor.getColumnIndex("name"):得到给定的列名称,如果列名称不存在,则为1
			 * 
			 * 手机联系人 cursor.getColumnIndex("name")
			 * cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)
			 * phoneCursor
			 * .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
			 */
			String phone = SIMCursor.getString(1);
			// 得到联系人姓名
			String name = SIMCursor.getString(SIMCursor.getColumnIndex("name"));
			contacts_show.append(name + ":" + phone + LINE_SEPARATOR);

		}
		// 取出 手机 所有联系人
		while (phoneCursor.moveToNext() && personCursor.moveToNext()) {

			String phone = phoneCursor
					.getString(phoneCursor
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
			String phoneName = personCursor.getString(personCursor
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

			contacts_show.append(phoneName + ":" + phone + LINE_SEPARATOR);
		}
	}

}

阅读更多
上一篇AndroidDatabinding的使用进阶之注解(四)
下一篇Java代码优化
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭