在Androidmanifest注册获取权限<uses-permission android:name="android.permission.READ_CONTACTS"/>

源码如下:

    欢迎加入QQ交流3群:317874559

package com.example.intentphone;import android.app.Activity;import android.content.CursorLoader;import android.content.Intent;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;import android.provider.ContactsContract;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {EditText ed1;EditText ed2;Button bt1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ed1 = (EditText) findViewById(R.id.ed1);ed2 = (EditText) findViewById(R.id.ed2);bt1 = (Button) findViewById(R.id.bt1);// 启动系统应用程序组件bt1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub// 创建Intent对象Intent intent = new Intent();// 设置Intent的Action属性intent.setAction(Intent.ACTION_GET_CONTENT);// 显示指定Intent的数据类型intent.setType("vnd.android.cursor.item/phone");// 启动系统Activity,请求码为零startActivityForResult(intent, 0);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);if (requestCode == 0) {if (resultCode == Activity.RESULT_OK) {// 获取返回的数据Uri contactData = data.getData();CursorLoader cursorLoader = new CursorLoader(this, contactData,null, null, null, null);// 查询联系人信息Cursor cursor = cursorLoader.loadInBackground();// 如果查询到指定的联系人if (cursor.moveToFirst()) {String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));// 获取指定的联系人查询该联系人的信息String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));String phoneNumbere = "此联系人暂时未输入电话号码";Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ "=" + contactId, null, null);if (phones.moveToFirst()) {// 取出电话号码phoneNumbere = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));}//关闭游标phones.close();//显示联系人与联系电话ed1.setText(name);ed2.setText(phoneNumbere);}}}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

<LinearLayout 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:orientation="vertical"    tools:context=".MainActivity" >    <EditText        android:id="@+id/ed1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:cursorVisible="false"        android:editable="false" />    <EditText        android:id="@+id/ed2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:cursorVisible="false"        android:editable="false" />    <Button        android:id="@+id/bt1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="查看联系人" /></LinearLayout>


更多相关文章

  1. android中GPS信息的获取
  2. android 判断横竖屏的方法
  3. Android中获取时间
  4. Android:简单联网获取网页代码
  5. Android自定义对话框的使用
  6. Android(安卓)ftrace
  7. android屏幕截图
  8. Android(安卓)获取版本号
  9. Android自定义对话框的使用

随机推荐

  1. WebKit – WebKit For Android
  2. Ubuntu 16.04环境下使用Clion 2019.1.4 g
  3. How to decompile Google Android(安卓).
  4. Android遍历文件Listfile返回值为null问
  5. Ue4.20 安卓开发配置及Android(安卓)Stud
  6. android之style样式-EditText样式
  7. 关于Android(安卓)LOG系统
  8. Android(安卓)uevent
  9. Java(Android)线程池
  10. [Android]在Manifest中注册广播监听器