最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android獲取聯(lián)系人姓名和電話代碼

 更新時(shí)間:2017年02月07日 11:37:48   作者:wangwo1991  
這篇文章主要為大家詳細(xì)介紹了Android獲取聯(lián)系人姓名和電話代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在開(kāi)發(fā)中往往有要獲取聯(lián)系人列表的功能,但是這次卻不是獲取聯(lián)系人列表,而是在聯(lián)系人列表點(diǎn)擊單個(gè)聯(lián)系人,獲取單個(gè)聯(lián)系人的姓名和電話,并設(shè)置在指定的輸入框內(nèi),方便用戶的使用;以下是實(shí)現(xiàn)的代碼:

<?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="點(diǎn)擊" />
 </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>

這個(gè)就是一個(gè)普通的布局文件代碼;

/**
  * 獲取聯(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號(hào)
   int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
   String contactId = cursor.getString(idColumn);

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

   // 獲得聯(lián)系人的電話號(hào)碼的cursor;
   Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
     ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);

   if (phones.moveToFirst()) {
    // 遍歷所有的電話號(hào)碼
    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;
 }

這里是主要功能的代碼,在這里要做一個(gè)try catch的動(dòng)作,因?yàn)锳ndroid手機(jī)的話會(huì)將微信還有qq的聯(lián)系方式也添加到列表中,但是其實(shí)是沒(méi)有電話號(hào)碼,點(diǎn)擊返回的時(shí)候,就會(huì)獲取不到,如果沒(méi)有try catch的就會(huì)報(bào)異常;

@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;
  }
  }
 }

這里是獲取值的一個(gè)回調(diào),在這個(gè)回調(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();

     }
    });
   }
  });

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

最終效果如下:

源碼地址contactperson

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android Studio 3.1.X中導(dǎo)入項(xiàng)目的正確方法分享

    Android Studio 3.1.X中導(dǎo)入項(xiàng)目的正確方法分享

    這篇文章主要給大家介紹了關(guān)于Android Studio 3.1.X中導(dǎo)入項(xiàng)目的正確方法,文中一步步將解決的方法以及可能遇到的問(wèn)題介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • Android將應(yīng)用調(diào)試log信息保存在SD卡的方法

    Android將應(yīng)用調(diào)試log信息保存在SD卡的方法

    Android將應(yīng)用調(diào)試log信息保存在SD卡的方法大家都知道嗎,下面腳本之家小編給大家分享Android將應(yīng)用調(diào)試log信息保存在SD卡的方法,感興趣的朋友參考下
    2016-04-04
  • Android中PopuWindow實(shí)現(xiàn)下拉列表實(shí)例

    Android中PopuWindow實(shí)現(xiàn)下拉列表實(shí)例

    本篇文章主要介紹了Android中PopuWindow實(shí)現(xiàn)下拉列表實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • android編程實(shí)現(xiàn)圖片庫(kù)的封裝方法

    android編程實(shí)現(xiàn)圖片庫(kù)的封裝方法

    這篇文章主要介紹了android編程實(shí)現(xiàn)圖片庫(kù)的封裝方法,涉及Android針對(duì)圖片的下載、保存、獲取及操作緩存圖片等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • Android實(shí)現(xiàn)拼多多地址選擇器

    Android實(shí)現(xiàn)拼多多地址選擇器

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)拼多多地址選擇器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android設(shè)置theme中可能遇到的坑

    Android設(shè)置theme中可能遇到的坑

    Theme是一套UI控件和Activity的樣式,下面這篇文章主要給大家介紹了關(guān)于Android設(shè)置theme中可能遇到的坑的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • Kotlin 編程三分鐘入門(mén)

    Kotlin 編程三分鐘入門(mén)

    一個(gè)多月以來(lái)Kotlin從入門(mén)到現(xiàn)在,堅(jiān)持用來(lái)開(kāi)發(fā)的切身感受。因?yàn)檎Z(yǔ)法與Java的區(qū)別挺大的一開(kāi)始很想放棄,如果不是因?yàn)轫?xiàng)目在使用,想必很少人會(huì)嘗試這樣一門(mén)小眾語(yǔ)言,但是習(xí)慣后會(huì)發(fā)現(xiàn)這些年究竟浪費(fèi)多少時(shí)間在寫(xiě)無(wú)用的Java代碼了
    2017-05-05
  • Android運(yùn)用BroadcastReceiver實(shí)現(xiàn)強(qiáng)制下線

    Android運(yùn)用BroadcastReceiver實(shí)現(xiàn)強(qiáng)制下線

    本篇文章主要介紹了Android運(yùn)用BroadcastReceiver實(shí)現(xiàn)強(qiáng)制下線,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android編程實(shí)現(xiàn)控件不同狀態(tài)文字顯示不同顏色的方法

    Android編程實(shí)現(xiàn)控件不同狀態(tài)文字顯示不同顏色的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)控件不同狀態(tài)文字顯示不同顏色的方法,涉及Android針對(duì)控件布局文件屬性設(shè)置及狀態(tài)判定等相關(guān)技巧,需要的朋友可以參考下
    2016-02-02
  • android將Bitmap對(duì)象保存到SD卡中的方法

    android將Bitmap對(duì)象保存到SD卡中的方法

    這篇文章主要介紹了android將Bitmap對(duì)象保存到SD卡中的方法,涉及Android讀寫(xiě)SD卡數(shù)據(jù)的方法,需要的朋友可以參考下
    2015-04-04

最新評(píng)論

纳雍县| 尚志市| 南投县| 麻阳| 瓦房店市| 灵台县| 泸溪县| 黔西| 石渠县| 安乡县| 信宜市| 梁河县| 井研县| 长丰县| 鄂尔多斯市| 宁国市| 游戏| 江永县| 黄梅县| 肇州县| 洛南县| 长乐市| 汨罗市| 南部县| 子洲县| 罗田县| 祁门县| 顺平县| 巫溪县| 民丰县| 金乡县| 娱乐| 阜平县| 北安市| 仁化县| 南郑县| 庆元县| 周口市| 霍城县| 大厂| 德州市|