首先给大家展示下运行效果图:
由于通讯录在手机里是以数据库贮存的 所以我们可以通过一个方法
1
2
|
context.getcontentresolver().query(phone.content_uri,
null , null , null , null );
|
来获得通讯录 ,这个方法返回一个游标的数据类型,通过movetonext()方法来获取所有的手机号码信息
当然读取手机通讯录需要权限 在adnroidmanifest文件中声明即可
由于我也实现了打电话的功能 所以也要声明权限
1
2
|
<uses-permission android:name= "android.permission.read_contacts" >
<uses-permission android:name= "android.permission.call_phone" ></uses-permission></uses-permission>
|
布局文件
activity_main.xml
1
2
3
4
|
<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" >
<listview android:id= "@+id/listview1" android:layout_width= "match_parent" android:layout_height= "wrap_content" >
</listview>
</relativelayout>
|
listview的布局文件:item.xml,在这里我设置的头像为默认的 当然也可以在手机数据库中读取联系人的icon
1
2
3
4
5
6
|
<!--{cke_protected}{c}%3c!%2d%2d%3fxml%20version%3d% 221.0 % 22 %20encoding%3d%22utf- 8 % 22 %3f%2d%2d%3e-->
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" >
<imageview android:id= "@+id/image" android:layout_width= "60dp" android:layout_height= "60dp" android:padding= "10dp" android:src= "@drawable/ic_launcher" >
<textview android:id= "@+id/name" android:paddingtop= "10dp" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_torightof= "@id/image" android:text= "name" >
<textview android:id= "@+id/number" android:paddingtop= "5dp" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_below= "@id/name" android:layout_torightof= "@id/image" android:text= "number" >
</textview></textview></imageview></relativelayout>
|
自己封装一个联系人信息的类 有两个变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.example.getphonenumber;
public class phoneinfo {
private string name;
private string number;
public phoneinfo(string name, string number) {
this .name = name;
this .number = number;
}
public string getname() {
return name;
}
public string getnumber() {
return number;
}
}
|
读取手机数据库中的通讯录
getphonenumberfrommobile.class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package com.example.getphonenumber;
import java.util.arraylist;
import java.util.list;
import android.content.context;
import android.database.cursor;
import android.provider.contactscontract.commondatakinds.phone;
public class getphonenumberfrommobile {
private list<phoneinfo> list;
public list<phoneinfo> getphonenumberfrommobile(context context) {
// todo auto-generated constructor stub
list = new arraylist<phoneinfo>();
cursor cursor = context.getcontentresolver().query(phone.content_uri,
null , 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));
phoneinfo phoneinfo = new phoneinfo(name, number);
list.add(phoneinfo);
}
return list;
}
}</phoneinfo></phoneinfo></phoneinfo>
|
自定义adapter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package com.example.getphonenumber;
import java.util.arraylist;
import java.util.list;
import android.content.context;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.textview;
public class myadapter extends baseadapter{
private list<phoneinfo> list;
private context context;
public myadapter(list<phoneinfo> 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;
}
}</phoneinfo></phoneinfo>
|
mainactivity中listview加载适配器 并为其添加点击监听事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package com.example.getphonenumber;
import java.util.arraylist;
import java.util.list;
import android.net.uri;
import android.os.bundle;
import android.app.activity;
import android.content.intent;
import android.view.menu;
import android.view.view;
import android.widget.adapterview;
import android.widget.adapterview.onitemclicklistener;
import android.widget.listview;
import android.widget.toast;
public class mainactivity extends activity implements onitemclicklistener {
private listview lv;
private myadapter adapter;
private getphonenumberfrommobile getphonenumberfrommobile;
private list<phoneinfo> list = new arraylist<phoneinfo>();
protected void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
lv = (listview) findviewbyid(r.id.listview1);
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);
}
}</phoneinfo></phoneinfo>
|