获取手机联系人

<span style="font-size: 12.6315793991089px;"><strong>留给自己看的 不做解释了 </strong></span>
 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />
    </ScrollView>

</RelativeLayout>

import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People.Phones;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts.Data;
import android.provider.ContactsContract.RawContacts;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		textView = (TextView) findViewById(R.id.tv);
		// 插入数据
		insertdata();
		// 获取联系人列表
		String string = getconts();

		textView.setText(string);

	}

	private void insertdata() {
		ContentResolver cr = getContentResolver();
		ContentValues values = new ContentValues();
		Uri uri = cr.insert(RawContacts.CONTENT_URI, values);
		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(android.provider.ContactsContract.Data.CONTENT_URI,
				values);
		// 插入电话信息
		values.clear();
		values.put(Phone.RAW_CONTACT_ID, raw_contact_id);
		values.put(Phone.NUMBER, "100000000");
		values.put(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
		uri = cr.insert(android.provider.ContactsContract.Data.CONTENT_URI,
				values);

	}

	private String getconts() {
		String name = null;
		String num = null, num1 = null;
		String emial = null, emial1 = null;
		int id = 0;
		StringBuffer sBuffer = new StringBuffer();
		ContentResolver cr = getContentResolver();

		Cursor cursor = cr.query(Contacts.CONTENT_URI, new String[] {
				Contacts._ID, Contacts.DISPLAY_NAME }, null, null, null);

		if (cursor != null) {
			while (cursor.moveToNext()) {
				id = cursor.getInt(cursor.getColumnIndex(Contacts._ID));
				Log.i("ltg", "联系人的id" + id);
				name = cursor.getString(cursor
						.getColumnIndex(Contacts.DISPLAY_NAME));
				Log.i("ltg", "联系人的名字" + name);
				Cursor cursor1 = cr.query(Phone.CONTENT_URI, new String[] {
						Phone.NUMBER, Phone.TYPE },
						Phone.CONTACT_ID + "=" + id, null, null);
				// sBuffer.append(id).append(" ").append(name);
				// String num = null, num1 = null;
				if (cursor1 != null) {
					while (cursor1.moveToNext()) {
						int type = cursor1.getInt(cursor1
								.getColumnIndex(Phone.TYPE));
						if (type == Phone.TYPE_HOME) {
							num = cursor1.getString(cursor1
									.getColumnIndex(Phone.NUMBER));
							num1 = "家庭电话" + num;
							Log.i("ltg", "联系人的num" + num1);
						}
						if (type == Phone.TYPE_MOBILE) {
							num = cursor1.getString(cursor1
									.getColumnIndex(Phone.NUMBER));
							num1 = "移动电话" + num;
							Log.i("ltg", "联系人的num" + num1);
						}
						// sBuffer.append(num1).append(" ");
					}
					// cursor1.close();
				}
				Cursor cursor2 = cr.query(Email.CONTENT_URI, new String[] {
						Email.DATA, Email.TYPE }, Email.CONTACT_ID + "=" + id,
						null, null);
				// String emial = null, emial1 = null;
				if (cursor2 != null) {
					while (cursor2.moveToNext()) {
						int type = cursor2.getInt(cursor2
								.getColumnIndex(Email.TYPE));
						if (type == Email.TYPE_WORK) {
							emial = cursor2.getString(cursor2
									.getColumnIndex(Email.DATA));
							emial1 = "工作邮箱" + emial;
							Log.i("ltg", "联系人的emial" + emial1);
						}

						// sBuffer.append(emial1).append("\n");
					}
					// cursor2.close();
				}
				sBuffer.append(id).append(" ").append(name).append(num1)
						.append(" ").append(emial1).append("\n");
			}
			// textView.setText(sBuffer.toString());
			cursor.close();

		}

		return sBuffer.toString();

	}
}
 <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.WRITE_CONTACTS"/>

你可能感兴趣的:(android)