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

首頁 > 系統(tǒng) > Android > 正文

android獲取聯(lián)系人示例分享

2020-04-11 11:53:03
字體:
供稿:網(wǎng)友

復制代碼 代碼如下:

package com.homer.phone;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class phoneRead extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);

  showListView();
 }

 private void showListView(){
  ListView listView = new ListView(this);

  ArrayList<HashMap<String, String>> list = getPeopleInPhone2();
  SimpleAdapter adapter = new SimpleAdapter(
         this,
         list,
         android.R.layout.simple_list_item_2,
         new String[] {"peopleName", "phoneNum"},
         new int[]{android.R.id.text1, android.R.id.text2}
        );
  listView.setAdapter(adapter);

  setContentView(listView);
 }

 private ArrayList<HashMap<String, String>> getPeopleInPhone2(){
  ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);  // 獲取手機聯(lián)系人
  while (cursor.moveToNext()) {
   HashMap<String, String> map = new HashMap<String, String>();

   int indexPeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME);  // people name
   int indexPhoneNum = cursor.getColumnIndex(Phone.NUMBER);    // phone number

   String strPeopleName = cursor.getString(indexPeopleName);
   String strPhoneNum = cursor.getString(indexPhoneNum);

   map.put("peopleName", strPeopleName);
   map.put("phoneNum", strPhoneNum);
   list.add(map);
  }
        if(!cursor.isClosed()){
         cursor.close();
         cursor = null;
        }

        return list;
 }
}



記得在AndroidManifest.xml中加入android.permission.READ_CONTACTS這個permission
復制代碼 代碼如下:

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 六安市| 营山县| 石台县| 巢湖市| 长兴县| 神池县| 名山县| 张家口市| 长乐市| 新源县| 英吉沙县| 韩城市| 新龙县| 临朐县| 安泽县| 双柏县| 安吉县| 楚雄市| 乌兰浩特市| 章丘市| 奎屯市| 依安县| 马山县| 景泰县| 阳西县| 犍为县| 南和县| 辽中县| 涿州市| 辽源市| 祁门县| 延寿县| 玉田县| 洛川县| 宁海县| 山西省| 平顺县| 兴安盟| 石首市| 东阿县| 博爱县|