Android开发学习
Android开发中,需要读取手机中的通讯录。
新建布局文件:activity_main.xml,代码内容:(显示的列表)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/list_userinfo" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
新建布局文件:list_item.xml,代码内容:(显示的列表内容)
<?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="wrap_content" android:layout_marginBottom="0dip" android:layout_marginTop="0dip" android:gravity="center" android:orientation="horizontal" > <LinearLayout android:layout_width="70dip" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/user_photo" android:layout_width="70dip" android:layout_height="70dip" android:background="@drawable/ic_launcher" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="70dip" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/user_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="8dip" android:layout_weight="1" android:ellipsize="marquee" android:singleLine="true" android:text="zhdc" android:textSize="15dip" /> <TextView android:id="@+id/user_tel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dip" android:layout_weight="1" android:ellipsize="marquee" android:singleLine="true" android:text="15887805454" android:textSize="20dip" /> </LinearLayout> </LinearLayout>
实现的MainActivity.java代码如下:(读取通讯录信息并显示成列表)
package com.zhengdecai.readaddbook; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.Contacts.Photo; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; /** * 读取通讯录信息 * * @author 郑德才 * */ public class MainActivity extends Activity { ListView mListView = null; MyAdapter myAdapter = null; Context mContext = null; private List<UserInfo> lUserInfo; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; mListView = (ListView) findViewById(R.id.list_userinfo); lUserInfo = new ArrayList<UserInfo>(); getUserInfo(); myAdapter = new MyAdapter(this, lUserInfo); mListView.setAdapter(myAdapter); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { // 调用系统方法拨打电话 Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri .parse("tel:" + lUserInfo.get(position).getUserTel())); startActivity(dialIntent); } }); } private void getUserInfo() { getPhoneContacts(); getSIMContacts(); } /** 获取库Phon表字段 **/ private static final String[] PHONES_PROJECTION = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID, Phone.CONTACT_ID }; /** 联系人显示名称 **/ private static final int PHONES_DISPLAY_NAME_INDEX = 0; /** 电话号码 **/ private static final int PHONES_NUMBER_INDEX = 1; /** 头像ID **/ private static final int PHONES_PHOTO_ID_INDEX = 2; /** 联系人的ID **/ private static final int PHONES_CONTACT_ID_INDEX = 3; /** 得到手机通讯录联系人信息 **/ private void getPhoneContacts() { ContentResolver resolver = mContext.getContentResolver(); // 获取手机联系人 Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null); if (phoneCursor != null) { while (phoneCursor.moveToNext()) { // 得到手机号码 String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX); // 当手机号码为空的或者为空字段 跳过当前循环 if (TextUtils.isEmpty(phoneNumber)) continue; // 得到联系人名称 String contactName = phoneCursor .getString(PHONES_DISPLAY_NAME_INDEX); // 得到联系人ID Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX); // 得到联系人头像ID Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX); // 得到联系人头像Bitamp Bitmap contactPhoto = null; // photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的 if (photoid > 0) { Uri uri = ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, contactid); InputStream input = ContactsContract.Contacts .openContactPhotoInputStream(resolver, uri); contactPhoto = BitmapFactory.decodeStream(input); } else { contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); } UserInfo userInfo = new UserInfo(); userInfo.setUserId(contactid); userInfo.setUserName(contactName); userInfo.setUserTel(phoneNumber); lUserInfo.add(userInfo); } phoneCursor.close(); } } /** 得到手机SIM卡联系人人信息 **/ private void getSIMContacts() { ContentResolver resolver = mContext.getContentResolver(); // 获取Sims卡联系人 Uri uri = Uri.parse("content://icc/adn"); Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null, null); if (phoneCursor != null) { while (phoneCursor.moveToNext()) { // 得到手机号码 String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX); // 当手机号码为空的或者为空字段 跳过当前循环 if (TextUtils.isEmpty(phoneNumber)) continue; // 得到联系人名称 String contactName = phoneCursor .getString(PHONES_DISPLAY_NAME_INDEX); // Sim卡中没有联系人头像 UserInfo userInfo = new UserInfo(); Long contactid = 0L; userInfo.setUserId(contactid); userInfo.setUserName(contactName); userInfo.setUserTel(phoneNumber); lUserInfo.add(userInfo); } phoneCursor.close(); } } class MyAdapter extends BaseAdapter { /* * private String aimage; private String atitle; private String * asummary; private String adatetime; private String alink; */ private List<UserInfo> alist; private LayoutInflater mInflater; private Map<Integer, View> viewMap = new HashMap<Integer, View>(); public MyAdapter(Activity activity, List<UserInfo> list) { super(); alist = list; mInflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { // TODO Auto-generated method stub return alist.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return alist.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View v = viewMap.get(parent); if (v == null) { UserInfo user = alist.get(position); v = mInflater.inflate(R.layout.list_item, null); TextView user_Name = (TextView) v.findViewById(R.id.user_name); TextView user_Tel = (TextView) v.findViewById(R.id.user_tel); user_Name.setText(user.getUserName()); user_Tel.setText(user.getUserTel()); final Bitmap userPhoto = user.getUserPhoto(); final ImageView user_Photo = (ImageView) v .findViewById(R.id.user_photo); if (userPhoto != null) { user_Photo.setBackgroundDrawable(new BitmapDrawable( userPhoto)); } viewMap.put(position, v); } return v; } } }
实现的UserInfo.java代码如下:(通讯录使用的实体)
package com.zhengdecai.readaddbook; import android.graphics.Bitmap; public class UserInfo { public Long userId; public String userName; public String userTel; public Bitmap userPhoto; public Long getUserId() { return userId; } public String getUserName() { return userName; } public String getUserTel() { return userTel; } public Bitmap getUserPhoto() { return userPhoto; } public void setUserId(Long userId) { this.userId = userId; } public void setUserName(String userName) { this.userName = userName; } public void setUserTel(String userTel) { this.userTel = userTel; } public void setUserPhoto(Bitmap userPhoto) { this.userPhoto = userPhoto; } }
需要增加读取权限:
<!-- 读取联系人权限 -->
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<!-- 拨打电话权限 -->
<uses-permission android:name="android.permission.CALL_PHONE"/>
实现运行效果图:
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。