您的位置:首页 > 移动开发 > Android开发

android调用系统通讯录,并返回联系人号码和称呼

2014-10-08 17:42 477 查看
这是第一篇博客,原因: 经常在网上找一些简单的demo来做或者直接用,但网上的资源乱七八糟,有的博主也不会给全,搞的很含糊,实在不知道怎么说。

为了能帮助更多的程序猿与程序媛,以后将会陆续写出很多很实用的文章,至少它能看清楚,它能拿来用 !

第一步:在配置文件里加入权限,因为是要读取联系人的。

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


第二步:layout文件,注意在Button 上已经添加点击事件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText   
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:id="@+id/mobileTel"  
        />  
    <Button
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="submit"  
        android:onClick="getTelClick" 
        />  
        
    </LinearLayout>


第三步:。

package com.example.test;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
	// 声明文本框
	private EditText et_mobile;
	// 声明姓名,电话
	private String username, usernumber;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 根据Id获得文本框
		et_mobile = (EditText) this.findViewById(R.id.mobileTel);
	}

	public void getTelClick(View v) {

		startActivityForResult(new Intent(Intent.ACTION_PICK,
				ContactsContract.Contacts.CONTENT_URI), 0);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			// ContentProvider展示数据类似一个单个数据库表
			// ContentResolver实例带的方法可实现找到指定的ContentProvider并获取到ContentProvider的数据
			ContentResolver reContentResolverol = getContentResolver();
			// URI,每个ContentProvider定义一个唯一的公开的URI,用于指定到它的数据集
			Uri contactData = data.getData();
			// 查询就是输入URI等参数,其中URI是必须的,其他是可选的,如果系统能找到URI对应的ContentProvider将返回一个Cursor对象.
			Cursor cursor = managedQuery(contactData, null, null, null, null);
			cursor.moveToFirst();
			// 获得DATA表中的名字
			username = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			// 条件为联系人ID
			String contactId = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts._ID));
			// 获得DATA表中的电话号码,条件为联系人ID,因为手机号码可能会有多个
			Cursor phone = reContentResolverol.query(
					ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
					ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
							+ contactId, null, null);
			while (phone.moveToNext()) {
				usernumber = phone
						.getString(phone
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				et_mobile.setText(usernumber + " (" + username + ")");
			}

		}
	}

}


注释已经很清楚了,不懂的欢迎留言讨论,最后附图一张。



转载注明出处:http://blog.csdn.net/u011368551/article/details/39896477
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: