iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android获取联系人姓名和电话代码
  • 633
分享到

Android获取联系人姓名和电话代码

姓名电话联系Android 2022-06-06 04:06:27 633人浏览 安东尼
摘要

在开发中往往有要获取联系人列表的功能,但是这次却不是获取联系人列表,而是在联系人列表点击单个联系人,获取单个联系人的姓名和电话,并设置在指定的输入框内,方便用户的使用;以下是实

开发中往往有要获取联系人列表的功能,但是这次却不是获取联系人列表,而是在联系人列表点击单个联系人,获取单个联系人的姓名和电话,并设置在指定的输入框内,方便用户的使用;以下是实现的代码:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="Http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="40dp"
  android:orientation="horizontal" >
  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="bottom"
   android:paddingLeft="10dp"
   android:text="姓名:"
   android:textColor="@android:color/black"
   android:textSize="13sp" />
  <EditText
   android:id="@+id/et_name"
   android:layout_width="200dp"
   android:layout_height="fill_parent"
   android:layout_marginLeft="10dp" />
  <Button
   android:id="@+id/btn1"
   android:layout_width="wrap_content"
   android:layout_height="40dp"
   android:text="点击" />
 </LinearLayout>
 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="40dp"
  android:orientation="horizontal" >
  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="bottom"
   android:paddingLeft="10dp"
   android:text="电话:"
   android:textColor="@android:color/black"
   android:textSize="13sp" />
  <EditText
   android:id="@+id/et_phone"
   android:layout_width="200dp"
   android:layout_height="fill_parent"
   android:layout_marginLeft="10dp"
   android:inputType="phone" />
 </LinearLayout>
</LinearLayout>

这个就是一个普通的布局文件代码;



 private ContactBen getContactPhone(Cursor cursor, Context context) {
  ContactBen vo = new ContactBen();
  int phoneColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
  int phoneNum = 0;
  try {
   phoneNum = cursor.getInt(phoneColumn);
  } catch (Exception e) {
   return null;
  }
  // String phoneResult = "";
  if (phoneNum > 0) {
   // 获得联系人的ID号
   int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
   String contactId = cursor.getString(idColumn);
   vo.name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
   // 获得联系人的电话号码的cursor;
   Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
     ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
   if (phones.moveToFirst()) {
    // 遍历所有的电话号码
    for (; !phones.isAfterLast(); phones.moveToNext()) {
     int index = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
     int typeindex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
     int phone_type = phones.getInt(typeindex);
     String phoneNumber = phones.getString(index);
     switch (phone_type) {
     case 2:
      vo.phone = phoneNumber;
      break;
     }
    }
    if (!phones.isClosed()) {
     phones.close();
    }
   }
  }
  return vo;
 }

这里是主要功能的代码,在这里要做一个try catch的动作,因为Android手机的话会将微信还有qq的联系方式也添加到列表中,但是其实是没有电话号码,点击返回的时候,就会获取不到,如果没有try catch的就会报异常;


@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  switch (requestCode) {
  case (1): {
   if (resultCode == Activity.RESULT_OK) {
    if (data != null) {
     Uri contactData = data.getData();
     @SuppressWarnings("deprecation")
     Cursor c = MainActivity.this.managedQuery(contactData, null, null, null, null);
     c.moveToFirst();
     ContactBen contactPhone = getContactPhone(c, MainActivity.this);
     if (contactPhone == null) {
      contactPhone = new ContactBen();
     }
     et_name.setText("" + contactPhone.name);
     et_phone.setText("" + contactPhone.phone);
    }
   }
   break;
  }
  }
 }

这里是获取值的一个回调,在这个回调中可以获取到你想要的数据;


findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    requestPermission(new String[] { Manifest.permission.READ_CONTACTS }, new PermissionHandler() {
     @Override
     public void onGranted() {
      Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
      startActivityForResult(intent, 1);
     }
     @Override
     public void onDenied() {
      super.onDenied();
     }
    });
   }
  });

这里是点击事件的处理,已经做了android6.0及6.0以上系统权限的适配了;最后记得在清单文件中添加相应的权限:

最终效果如下:

源码地址:contactperson

您可能感兴趣的文章:Android ContentProvider实现获取手机联系人功能Android获取手机通讯录、sim卡联系人及调用拨号界面方法Android 获取手机联系人实例代码详解android获取联系人示例分享Android编程操作联系人的方法(查询,获取,添加等)Android获取联系人头像的方法Android获取手机联系人信息Android获取手机联系人电话号码并返回结果Android实现获取联系人电话号码功能android如何获取联系人所有信息


--结束END--

本文标题: Android获取联系人姓名和电话代码

本文链接: https://www.lsjlt.com/news/22346.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作