溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android應用中怎么獲取聯(lián)系人的姓名與電話

發(fā)布時間:2020-11-21 16:46:13 來源:億速云 閱讀:210 作者:Leah 欄目:移動開發(fā)

這期內(nèi)容當中小編將會給大家?guī)碛嘘PAndroid應用中怎么獲取聯(lián)系人的姓名與電話,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

實現(xiàn)代碼:

<&#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>

這個就是一個普通的布局文件代碼;

/**
  * 獲取聯(lián)系人電話
  * 
  * @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) {
   // 獲得聯(lián)系人的ID號
   int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
   String contactId = cursor.getString(idColumn);

   vo.name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

   // 獲得聯(lián)系人的電話號碼的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的聯(lián)系方式也添加到列表中,但是其實是沒有電話號碼,點擊返回的時候,就會獲取不到,如果沒有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;
  }
  }
 }

這里是獲取值的一個回調(diào),在這個回調(diào)中可以獲取到你想要的數(shù)據(jù);

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();

     }
    });
   }
  });

這里是點擊事件的處理,已經(jīng)做了android6.0及6.0以上系統(tǒng)權限的適配了;最后記得在清單文件中添加相應的權限:

最終效果如下:

Android應用中怎么獲取聯(lián)系人的姓名與電話

上述就是小編為大家分享的Android應用中怎么獲取聯(lián)系人的姓名與電話了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI