1.main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
<EditText
android:id="@+id/show"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:editable
="false"
android:cursorVisible
="false"
/>
<EditText
android:id="@+id/phone"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:editable
="false"
android:cursorVisible
="false"
/>
<Button
android:id="@+id/btnClick"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="查看联系人"
/>
</LinearLayout>

2.MainActivity.java

public class MainActivity extends Activity
{

protected static final String TAG = "Click.Button";
protected static final int PICK_CONTACT = 0;
private Button btnClic = null;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btnClic = (Button) findViewById(R.id.btnClick);
btnClic.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("vnd.android.cursor.item/phone");
startActivityForResult(intent, PICK_CONTACT);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case PICK_CONTACT:
if (resultCode == Activity.RESULT_OK)
{
// 获取返回的数据
Uri contactData = data.getData();
// 查询联系人信息
Cursor cursor = managedQuery(contactData, null, null, null,
null);
// 如果查询到指定的联系人
if (cursor.moveToFirst())
{
String contactID = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
// 获取联系人的姓名
// String name =
// cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String name = cursor
.getString(cursor
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String phoneNumber = "此联系人未输入电话号码";
// 根据联系人查询该联系人的详细信息
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ "=" + contactID, null, null);
// 取出第一行
if (phones.moveToFirst())
{
phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
//关闭游标
phones.close();
EditText show = (EditText)findViewById(R.id.show);
show.setText(name);
EditText phone = (EditText)findViewById(R.id.phone);
phone.setText(phoneNumber);
}
cursor.close();
}
break;
}

}
}

3.manifest.xml文件中添加此节点

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





更多相关文章

  1. android查询数据库获得手机里面所有的联系人
  2. Android中调用系统函数查找联系人
  3. Android读取联系人并按照拼音排序
  4. 解剖Android联系人之三,基于2.1
  5. Android 保存联系人,包括部门\职位\传真\地址\照片
  6. 打开android 系统联系人
  7. android 向系统通讯录添加一个联系人信息
  8. 读取联系人信息
  9. 使用Content Provider得到联系人信息

随机推荐

  1. GIMP 2018 年回顧:2.10 版及其後
  2. Java客户端Jedis 对Redis的几种调用方式
  3. Linux下使用Rinetd来实现端口转发
  4. Debian 10 Buster release party 重庆站
  5. MySQL的增强型语法高亮终端-MyCli
  6. HCIA-VRP基础及操作
  7. Linux系统性能监控工具Tsar
  8. MySQL多线程备份工具Mydumper详解
  9. Spring、Shiro和EHCache 缓存的使用案例
  10. 用httping测试WEB页面响应时间