当前位置: 移动技术网 > IT编程>移动开发>Android > android获取联系人示例分享

android获取联系人示例分享

2019年07月24日  | 移动技术网IT编程  | 我要评论

上海第二工业大学分数线,中国新歌声网上直播,郑艺飞

复制代码 代码如下:

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);  // 获取手机联系人
  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" />

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网