欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android ContentProvider獲取手機(jī)聯(lián)系人實(shí)例

 更新時(shí)間:2017年02月13日 17:15:15   作者:Smile_muse  
這篇文章主要介紹了Android ContentProvider獲取手機(jī)聯(lián)系人實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在做項(xiàng)目的時(shí)候,因?yàn)橐玫轿覀冏詣荧@取聯(lián)系人的姓名和電話,就想到了ContentProvider分享數(shù)據(jù)的功能,這樣做既節(jié)省了時(shí)間,也減少了我們輸入錯(cuò)誤號碼的幾率,所以,想在這里把小demo分享給大家,方便以后要用的時(shí)候可以看看

我們先看下獲取所有聯(lián)系人的方式,把所有聯(lián)系人展示在listView上

public void getLinkMan(View view){
    //獲取聯(lián)系人
    Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");
    cursor = cr.query(uri,null,null,null,null);
    while(cursor.moveToNext()){
      int id= cursor.getInt(cursor.getColumnIndex("_id"));
      String name= cursor.getString(cursor.getColumnIndex("display_name"));
      int number=cursor.getInt(cursor.getColumnIndex("number"));
      Log.i("test",id+" "+name);

      //繼續(xù)獲取相對應(yīng)的聯(lián)系人的數(shù)據(jù)(電話號碼)
      Uri uriData=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data");
      cursorData = cr.query(uriData,null,null,null,null);
      while(cursorData.moveToNext()){
        String data1= cursorData.getString(cursorData.getColumnIndex("data1"));
        String type= cursorData.getString(cursorData.getColumnIndex("mimetype"));
        if("vnd.android.cursor.item/phone_v2".equals(type)){
          Log.i("test","    "+data1+":"+type);
          Map<String,Object> map=new HashMap<>();
          map.put("id",id);
          map.put("name",name);
          map.put("number",number);
          list.add(map);
        }
      }
    }

    //實(shí)例化適配器
    simpleAdapter = new SimpleAdapter(this,list, R.layout.item_linkman,new String[]{"_id","name","number"},new int[]{R.id.tv_item_list_id,R.id.tv_item_list_name,R.id.tv_item_list_number});
    listView.setAdapter(simpleAdapter);
  }

在控制臺上輸出的效果如圖

這里寫圖片描述

在模擬器上的效果就是一個(gè)listView

這里寫圖片描述

然后我們著重來看一下獲取手機(jī)聯(lián)系人

1)跳轉(zhuǎn)到系統(tǒng)聯(lián)系人界面
2)選擇一個(gè)聯(lián)系人
3)獲取聯(lián)系人名、手機(jī)號碼
4)回調(diào)顯示名字和號碼

1)、跳轉(zhuǎn)到系統(tǒng)聯(lián)系人的界面代碼可以在Intent跳轉(zhuǎn)大全里面找

//跳轉(zhuǎn)到通訊錄界面
    Uri uri=Uri.parse("content://contacts/people");
    Intent intent=new Intent(Intent.ACTION_PICK,uri);
    startActivity(intent);

2)、選擇聯(lián)系人,回調(diào)過去

@Override 
  protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    switch (requestCode){ 
      case 0: 
        if(data==null) //判斷返回的數(shù)據(jù)
        { 
          return; 
        } 
        //獲取選擇的聯(lián)系人信息 
        Uri uri=data.getData(); 
        String contact[]=getPhoneContacts(uri); 
        Map<String,Object> map = new HashMap<String,Object>(); 
        map.put("name",contact[0]); 
        map.put("number",contact[1]); 
        list.clear(); 
        list.add(map); 
        adapter = new SimpleAdapter(this, list, R.layout.item_list,new String[]{"id","name","number"},new int[]{R.id.id,R.id.name,R.id.number}); 
        listView.setAdapter(adapter); 
        break; 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
  } 

3)、根據(jù)選擇人的id,去數(shù)據(jù)庫里面的表查詢數(shù)據(jù),返回聯(lián)系人的姓名和號碼,但是需要注意的是,我們的聯(lián)系人和和聯(lián)系人的手機(jī)號并不在一個(gè)表里,所以這時(shí)候我們需要對數(shù)據(jù)進(jìn)行一些處理

private String[] getPhoneContacts(Uri uri){
    String[] contact=new String[2];
    ContentResolver cr = getContentResolver();
    //取得聯(lián)系人中第一項(xiàng)的光標(biāo)
    Cursor cursor=cr.query(uri,null,null,null,null);
    if(cursor!=null)
    {
      cursor.moveToFirst();
      //取得聯(lián)系人姓名
      int nameFieldColumnIndex=cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
      contact[0]=cursor.getString(nameFieldColumnIndex);
      //取得電話號碼
      String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
      Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
          ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
      if(phone != null){
        phone.moveToFirst();
        contact[1] = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      }
      phone.close();
      cursor.close();
    }
    else
    {
      return null;
    }
    return contact;
  }

4)、然后添加權(quán)限,再把項(xiàng)目運(yùn)行一遍,就可以看到我們的效果圖了

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

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

相關(guān)文章

  • Android通過訪問網(wǎng)頁查看網(wǎng)頁源碼實(shí)例詳解

    Android通過訪問網(wǎng)頁查看網(wǎng)頁源碼實(shí)例詳解

    這篇文章主要介紹了Android通過訪問網(wǎng)頁查看網(wǎng)頁源碼的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android.bp語法和使用方法講解

    Android.bp語法和使用方法講解

    Android.bp是用來替換Android.mk的配置文件,下面這篇文章主要給大家介紹了關(guān)于Android.bp語法和使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • Android延時(shí)操作的三種方法

    Android延時(shí)操作的三種方法

    這篇文章主要為大家詳細(xì)介紹了Android延時(shí)操作的三種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • rxjava+retrofit實(shí)現(xiàn)多圖上傳實(shí)例代碼

    rxjava+retrofit實(shí)現(xiàn)多圖上傳實(shí)例代碼

    本篇文章主要介紹了rxjava+retrofit實(shí)現(xiàn)多圖上傳實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • Android解析JSON數(shù)據(jù)的方法分析

    Android解析JSON數(shù)據(jù)的方法分析

    這篇文章主要介紹了Android解析JSON數(shù)據(jù)的方法,結(jié)合實(shí)例形式演示了Android解析json格式數(shù)據(jù)的原理與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Android Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器函數(shù)詳解

    Android Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器函數(shù)詳解

    這篇文章主要為大家詳細(xì)介紹了Android Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器函數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-04-04
  • Android 第三方應(yīng)用接入微信平臺研究情況分享(二)

    Android 第三方應(yīng)用接入微信平臺研究情況分享(二)

    微信平臺開放后倒是挺火的,許多第三方應(yīng)用都想試下,這里把我的整個(gè)研究情況給出來,希望可以共同學(xué)習(xí),感興趣的朋友可以了解下
    2013-01-01
  • Android實(shí)現(xiàn)擲骰子效果

    Android實(shí)現(xiàn)擲骰子效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)擲骰子效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Android簡單使用PopupWindow的方法

    Android簡單使用PopupWindow的方法

    這篇文章主要為大家詳細(xì)介紹了Android簡單使用PopupWindow的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • 性能分析工具Systrace的使用及說明

    性能分析工具Systrace的使用及說明

    這篇文章主要介紹了性能分析工具Systrace的使用及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03

最新評論