Android系统架构的最上层是应用程序层,其上包含各种与用户相关的应用程序。这些应用程序基本都是由Java语言编写的,包括系统桌面、电子邮件客户端、SMS短信、地图、电源管理、电话、联系人等系统应用程序,以及各种第三方应用程序。基于应用程序层,对手机联系人这一模块进行开发。
下面我将详细叙述获取手机联系人的步骤:
1. 首先,我们需要新建一个类PhoneInfo,用于通讯录的数据封装,定义字符串,并写好构造函数。
public class PhoneInfo {
private String name;
private String number;
private String sortKey;
private int id;
public PhoneInfo(String name, String number,String sortKey, int id) {
this.name = name;
this.number = number;
this.sortKey = sortKey;
this.id = id;
}
2.然后,主要是通过 context.getContentResolver()方法来获得通讯录 ,这个方法返回一个游标的数据类型,通过moveToNext()方法来获取所有的手机号码信息,将获得的手机号码信息放入列表中。还用到了sort_key()方法对获取到的通讯录信息进行了排序
GetPhoneNumberFromMobile {
private List list;
public List getPhoneNumberFromMobile(Context context) {
// TODO Auto-generated constructor stub
list = new ArrayList();
Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI,
new String[] { "display_name", "sort_key", "contact_id",
"data1" }, null, null, null);
// moveToNext方法返回的是一个boolean类型的数据
while (cursor.moveToNext()) {
//读取通讯录的姓名
String name = cursor.getString(cursor
.getColumnIndex(Phone.DISPLAY_NAME));
//读取通讯录的号码
String number = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
int Id = cursor.getInt(cursor.getColumnIndex(Phone.CONTACT_ID));
String Sortkey = getSortkey(cursor.getString(1));
PhoneInfo phoneInfo = new PhoneInfo(name, number,Sortkey,Id);
list.add(phoneInfo);
}
cursor.close();
return list;
}
private static String getSortkey(String sortKeyString) {
String key =sortKeyString.substring(0,1).toUpperCase();
if (key.matches("[A-Z]")){
return key;
}else
return "#";
}
}
3.实现基本功能后我们需要将获取的通讯录信息在客户端界面上展示,界面主要布局代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RetrievePhoneList1" >
<ListView
android:id="@+id/contents_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scrollbars="vertical"
android:visibility="visible" >
ListView>
RelativeLayout>
4.与界面绑定的activity代码如下:
public class RetrievePhoneList1 extends Activity implements OnItemClickListener {
private ListView lv;
private MyAdapter adapter;
private GetPhoneNumberFromMobile getPhoneNumberFromMobile;
private List list = new ArrayList();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rpl);
lv = (ListView) findViewById(R.id.contents_view);
getPhoneNumberFromMobile = new GetPhoneNumberFromMobile();
list = getPhoneNumberFromMobile.getPhoneNumberFromMobile(this);
adapter = new MyAdapter(list, this);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
// TODO Auto-generated method stub
String number = list.get(position).getNumber();
Intent intent = new Intent();
intent.setAction("android.intent.action.CALL");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
}
@Override
public void onBackPressed() { //清除缓存
list.clear();
super.onBackPressed();
return;
}
}
5.除此以外,还需要定义一个adapter用于对界面的数据传递。使用ViewHolder进行优化。
public class MyAdapter extends BaseAdapter{
private List list;
private Context context;
public MyAdapter(List list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.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
if(convertView==null){
ViewHolder viewHolder=new ViewHolder();
LayoutInflater inflater=LayoutInflater.from(context);
convertView=inflater.inflate(R.layout.item, null);
viewHolder.name=(TextView) convertView.findViewById(R.id.name);
viewHolder.number=(TextView) convertView.findViewById(R.id.number);
viewHolder.name.setText(list.get(position).getName());
viewHolder.number.setText(list.get(position).getNumber());
}
return convertView;
}
public class ViewHolder{
TextView name;
TextView number;
}
}
注意!读取手机通讯录需要权限 ,需要在AndroidManifest文件中申请以下权限。
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
最后,成功实现获取手机联系人功能。截图如下: