Android手机的通讯录

Android手机的通讯录

Android手机的通讯录联系人全部都存在系统的数据库中,如果须要获得通讯里联系人的信息就须要访问系统的数据库,才能将信息拿出来。

contacts 表

  • _id :表的ID,主要用于其它表通过contacts 表中的ID可以查到相应的数据。
  • display_name: 联系人名称
  • photo_id:头像的ID,如果没有设置联系人头像,这个字段就为空
  • times_contacted:通话记录的次数
  • last_time_contacted: 最后的通话时间
  • lookup :是一个持久化的储存 因为用户可能会改名子 但是它改不了lookup

简单实例

要先添加权限

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

这个简单实例只是把数据显示在控制台上。

package com.zsz.develop.getmyphonenumber;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;

/**
 * Created by shengzhong on 2015/8/15.
 */
public class GetNumber {

public static String getNumber(Context context){
    /**得到手机通讯录联系人信息**/
    ContentResolver resolver= context.getContentResolver();
    // 获取手机联系人
   Cursor phoneCursor= resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    if (phoneCursor!=null){

        String phoneNumber;
        String phoneName;
        while (phoneCursor.moveToNext()){
            //得到手机号码和名字
             phoneNumber=phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            phoneName=phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

            System.out.println("联系人:"+phoneName +"     电话号码:"+phoneNumber);
        }


    }


    return null;
}
}

把通讯录显示在页面上

MainActivity中的代码:主要加载页面信息

package com.zsz.develop.getmyphonenumber;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import java.util.List;


public class MainActivity extends ActionBarActivity {
private ListView lv;
private MyAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GetNumber.getNumber(this);

    lv= (ListView) findViewById(R.id.lv);
    adapter=new MyAdapter(GetNumber.lists,this);
    lv.setAdapter(adapter);
}
}

GetNumber中的代码主要获得通讯录信息

package com.zsz.develop.getmyphonenumber;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shengzhong on 2015/8/15.
 */
public class GetNumber {

public static List<PhoneInfo> lists=new ArrayList<PhoneInfo>();

public static String getNumber(Context context){
    /**得到手机通讯录联系人信息**/
    ContentResolver resolver= context.getContentResolver();
    // 获取手机联系人
   Cursor phoneCursor= resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    if (phoneCursor!=null){

        String phoneNumber;
        String phoneName;
        while (phoneCursor.moveToNext()){
            //得到手机号码和名字
             phoneNumber=phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            phoneName=phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            PhoneInfo phoneInfo=new PhoneInfo(phoneName,phoneNumber);
            lists.add(phoneInfo);
            System.out.println("联系人:"+phoneName +"     电话号码:"+phoneNumber);
        }
    }
    return null;
}
}

PhoneInfo对数据进行封装

package com.zsz.develop.getmyphonenumber;

/**
 * Created by shengzhong on 2015/8/16.
 */
public class PhoneInfo {

   private String phoneName;
private String phoneNumber;

public PhoneInfo(String phoneName,String phoneNumber){
    setPhoneName(phoneName);
    setPhoneNumber(phoneNumber);
}


public String getPhoneName() {
    return phoneName;
}

public void setPhoneName(String phoneName) {
    this.phoneName = phoneName;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}
}

MyAdapter对listview进行适配。

package com.zsz.develop.getmyphonenumber;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by shengzhong on 2015/8/16.
 */
public class MyAdapter extends BaseAdapter {
private List<PhoneInfo> lists;
private Context context;
private LayoutInflater layoutInflater;
private LinearLayout layout;

public MyAdapter(List<PhoneInfo> lists, Context context){
    this.lists=lists;
    this.context=context;
}



@Override
public int getCount() {
    return lists.size();
}

@Override
public Object getItem(int i) {
    return lists.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    layoutInflater=LayoutInflater.from(context);
    layout= (LinearLayout) layoutInflater.inflate(R.layout.call,null);
    TextView tvName= (TextView) layout.findViewById(R.id.tvName);
    TextView tvNumber= (TextView) layout.findViewById(R.id.tvNumber);
    tvName.setText(lists.get(i).getPhoneName());
    tvNumber.setText(lists.get(i).getPhoneNumber());

    return layout;
}
}

call的layout。在main中有一个list

<?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="wrap_content"
    android:orientation="horizontal"
    >
    <ImageView
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:src="@drawable/jiamin"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvName"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvNumber"
            />


    </LinearLayout>


</LinearLayout>

</LinearLayout>

listView优化

使用holder新建textview来操作。

重用了convertView,很大程度上的减少了内存的消耗。通过判断convertView是否为null,是的话就需要产生一个视图出来,然后给这个视图数据,最后将这个视图返回给底层,呈献给用户。
特点:如果当前的convertView为null,则通过LayoutInflat产生一个view。

其中converView会反复使用,目的是为了提高ListView效率。

package com.zsz.develop.getmyphonenumber;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by shengzhong on 2015/8/16.
 */
public class MyAdapter extends BaseAdapter {
private List<PhoneInfo> lists;
private Context context;
private LayoutInflater layoutInflater;
private LinearLayout layout;

public MyAdapter(List<PhoneInfo> lists, Context context){
    this.lists=lists;
    this.context=context;
}



@Override
public int getCount() {
    return lists.size();
}

@Override
public Object getItem(int i) {
    return lists.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
//        layoutInflater=LayoutInflater.from(context);
//        layout= (LinearLayout) layoutInflater.inflate(R.layout.call,null);
//        TextView tvName= (TextView) layout.findViewById(R.id.tvName);
//        TextView tvNumber= (TextView) layout.findViewById(R.id.tvNumber);
//        tvName.setText(lists.get(i).getPhoneName());
//        tvNumber.setText(lists.get(i).getPhoneNumber());
//
    ViewHolder holder;
    if (view==null){
        view=LayoutInflater.from(context).inflate(R.layout.call,null);
        holder=new ViewHolder();
        holder.tvName= (TextView) view.findViewById(R.id.tvName);
        holder.tvNumber= (TextView) view.findViewById(R.id.tvNumber);
        holder.tvName.setText(lists.get(i).getPhoneName());
        holder.tvNumber.setText(lists.get(i).getPhoneNumber());
        view.setTag(holder);

    }else {
        holder= (ViewHolder) view.getTag();
        holder.tvName.setText(lists.get(i).getPhoneName());
        holder.tvNumber.setText(lists.get(i).getPhoneNumber());

    }

    return view;
}

private static class ViewHolder{

    TextView tvName;
    TextView tvNumber;
}
}