国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android獲取聯系人姓名和電話代碼

2019-12-12 03:47:57
字體:
來源:轉載
供稿:網友

在開發中往往有要獲取聯系人列表的功能,但是這次卻不是獲取聯系人列表,而是在聯系人列表點擊單個聯系人,獲取單個聯系人的姓名和電話,并設置在指定的輸入框內,方便用戶的使用;以下是實現的代碼:

<?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>

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

/**  * 獲取聯系人電話  *   * @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以上系統權限的適配了;最后記得在清單文件中添加相應的權限:

最終效果如下:

源碼地址contactperson

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蚌埠市| 措勤县| 唐海县| 卓尼县| 天镇县| 定陶县| 海盐县| 江孜县| 花莲市| 库伦旗| 贡觉县| 慈利县| 怀集县| 郸城县| 冕宁县| 钟祥市| 永川市| 泸州市| 江都市| 承德县| 炎陵县| 普定县| 于田县| 襄汾县| 肥西县| 承德市| 阜康市| 乌恰县| 乌什县| 剑阁县| 祁阳县| 咸丰县| 舟山市| 武鸣县| 昆山市| 封开县| 益阳市| 汝阳县| 玉溪市| 白水县| 高密市|