获取手机通讯录
来源:互联网 发布:sql简单查询语句 编辑:程序博客网 时间:2024/05/17 22:49
首先是运行结果
由于通讯录在手机里是以数据库贮存的 所以我们可以通过一个方法
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
5
6
<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>
1
2
3
4
5
6
7
8
9
10
<!--{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
16
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
28
29
30
31
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>
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
51
52
53
54
55
56
57
58
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>
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
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>
阅读全文
0 0
- 获取手机通讯录
- 获取手机通讯录
- Android 获取手机通讯录
- 获取手机通讯录
- iOS 获取手机通讯录
- 获取手机通讯录信息
- 获取手机通讯录联系人
- 获取手机通讯录
- 获取手机通讯录
- 获取手机通讯录
- 获取手机通讯录 iOS
- iOS获取手机通讯录
- android获取手机通讯录
- 获取手机通讯录联系人
- 获取手机通讯录
- 获取手机通讯录
- android获取手机通讯录
- 获取手机通讯录
- Spring Cloud微服务
- CountDownLatch介绍
- 基于canvas的图像无机物检测
- 制作OpenSUSE13.2的openstack镜像
- 167. Two Sum II
- 获取手机通讯录
- liunx 安装 wget
- 组播MAC地址
- C/C++ 使用delete删除指针需小心 “野指针”
- 从程序员到CTO的Java技术路线图
- 评价视频解析网站三个重要因素:稳定反应快覆盖面广
- ISCSI
- android 开启自动旋转屏幕的实现代码
- trie图模板