Android应用中怎么获取联系人的姓名与电话

发布时间:2020-11-21 16:46:13 作者:Leah
来源:亿速云 阅读:207

这期内容当中小编将会给大家带来有关Android应用中怎么获取联系人的姓名与电话,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

实现代码:

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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>

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

/**
  * 获取联系人电话
  * 
  * @param cursor
  * @param context
  * @return
  */
 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以上系统权限的适配了;最后记得在清单文件中添加相应的权限:

最终效果如下:

Android应用中怎么获取联系人的姓名与电话

上述就是小编为大家分享的Android应用中怎么获取联系人的姓名与电话了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 【更新】爬取姓名大全的名字
  2. 【更新】比较智能的爬取姓名

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android roi

上一篇:Android项目中如何实现自定义相机预览界面

下一篇:StringBuffer与StringBuilder如何在Android项目中使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》