欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 获取手机联系人实例代码详解

程序员文章站 2023-12-17 09:43:40
我的风格,废话不多说了,直接给大家贴代码了。 具体代码如下所示: package com.org.demo.demo; import com.org.wang...

我的风格,废话不多说了,直接给大家贴代码了。

具体代码如下所示:

package com.org.demo.demo;
import com.org.wangfeng.r;
import android.app.activity;
import android.content.intent;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.widget.button;
import android.widget.edittext;
public class getphoneactivity extends activity {
 private edittext edittext;
 private button button;
 @override
 protected void oncreate(bundle savedinstancestate) {
  // todo auto-generated method stub
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.getphoneactivity);
  edittext = (edittext) findviewbyid(r.id.et_getphone);
  button = (button) findviewbyid(r.id.bb_getphone);
  button.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view arg0) {
    // todo auto-generated method stub
    intent intent = new intent(getphoneactivity.this,
      contactactivity.class);
    startactivityforresult(intent, 1);
   }
  });
 }
 @override
 protected void onactivityresult(int requestcode, int resultcode, intent data) {
  // system.out.println("resultcode:" + resultcode);
  // system.out.println("requestcode:" + requestcode);
  log.d("jiejie", "被调用了");
  if (resultcode == activity.result_ok) {
   string phone = data.getstringextra("phone");
   log.d("jiejie", "_______________"+phone);
   phone = phone.replaceall("-", "").replaceall(" ", "");// 替换-和空格

   edittext.settext(phone);// 把电话号码设置给输入框
  }
  super.onactivityresult(requestcode, resultcode, data);
 }
}
package com.org.demo.demo;
import java.util.arraylist;
import java.util.hashmap;
import com.org.wangfeng.r;
import android.app.activity;
import android.content.intent;
import android.database.cursor;
import android.net.uri;
import android.os.bundle;
import android.view.view;
import android.widget.adapterview;
import android.widget.listview;
import android.widget.simpleadapter;
import android.widget.adapterview.onitemclicklistener;
import android.widget.textview;
public class contactactivity extends activity {
 private listview lvlist;
 private arraylist<hashmap<string, string>> readcontact;
 private textview back;
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_contact);
  back =(textview)findviewbyid(r.id.back);
  back.setonclicklistener(new view.onclicklistener() {   
   @override
   public void onclick(view arg0) {
    // todo auto-generated method stub
    finish();
   }
  });
  lvlist = (listview) findviewbyid(r.id.lv_list);
  readcontact = readcontact();
  // system.out.println(readcontact);
  lvlist.setadapter(new simpleadapter(this, readcontact,
    r.layout.contact_list_item, new string[] { "name", "phone" },
    new int[] { r.id.tv_name, r.id.tv_phone }));
  lvlist.setonitemclicklistener(new onitemclicklistener() {
   @override
   public void onitemclick(adapterview<?> parent, view view,
     int position, long id) {
    string phone = readcontact.get(position).get("phone");// 读取当前item的电话号码
    intent intent = new intent();
    intent.putextra("phone", phone);
    setresult(activity.result_ok, intent);// 将数据放在intent中返回给上一个页面

    finish();
   }
  });
 }
 private arraylist<hashmap<string, string>> readcontact() {
  // 首先,从raw_contacts中读取联系人的id("contact_id")
  // 其次, 根据contact_id从data表中查询出相应的电话号码和联系人名称
  // 然后,根据mimetype来区分哪个是联系人,哪个是电话号码
  uri rawcontactsuri = uri
    .parse("content://com.android.contacts/raw_contacts");
  uri datauri = uri.parse("content://com.android.contacts/data");
  arraylist<hashmap<string, string>> list = new arraylist<hashmap<string, string>>();
  // 从raw_contacts中读取联系人的id("contact_id")
  cursor rawcontactscursor = getcontentresolver().query(rawcontactsuri,
    new string[] { "contact_id" }, null, null, null);
  if (rawcontactscursor != null) {
   while (rawcontactscursor.movetonext()) {
    string contactid = rawcontactscursor.getstring(0);
    // system.out.println(contactid);
    // 根据contact_id从data表中查询出相应的电话号码和联系人名称, 实际上查询的是视图view_data
    cursor datacursor = getcontentresolver().query(datauri,
      new string[] { "data1", "mimetype" }, "contact_id=?",
      new string[] { contactid }, null);
    if (datacursor != null) {
     hashmap<string, string> map = new hashmap<string, string>();
     while (datacursor.movetonext()) {
      string data1 = datacursor.getstring(0);
      string mimetype = datacursor.getstring(1);
      // system.out.println(contactid + ";" + data1 + ";"
      // + mimetype);
      if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
       map.put("phone", data1);
      } else if ("vnd.android.cursor.item/name"
        .equals(mimetype)) {
       map.put("name", data1);
      }
     }
     list.add(map);
     datacursor.close();
    }
   }
   rawcontactscursor.close();
  }
  return list;
 }
}

本段代码到此结束,代码比较简单,并附有注释,希望对大家学习android获取手机联系人相关知识有所帮助。

上一篇:

下一篇: