脉脉中注册时有一个,我已开启权限:
这个原理:
1、如果通讯录为空,脉脉就不让你往下进行, 2、如果没有开启权限,脉脉就不让你往下进行, 3、如果开启权限且通讯录为空,脉脉就不让你往下进行, 4、如果开启权限且通讯录不为空,脉脉才能让你往下进行。读取通讯录权限
1
2
3
4
|
<!-- 读取联系人权限 -->
<uses-permission android:name=
"android.permission.READ_CONTACTS"
>
<!-- 拨打电话权限 -->
<uses-permission android:name=
"android.permission.CALL_PHONE"
></uses-permission></uses-permission>
|
关键代码
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
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/** 联系人名称 **/
private
ArrayList<contactentity> mContacts =
new
ArrayList<contactentity>();
private
static
final
String[] PHONES_PROJECTION =
new
String[] {
Phone.DISPLAY_NAME, Phone.NUMBER, Phone.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 = getContentResolver();
try
{
// 获取手机联系人
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);
}
ContactEntity mContact =
new
ContactEntity(contactName,
phoneNumber, contactPhoto);
mContacts.add(mContact);
}
phoneCursor.close();
}
}
catch
(Exception e) {
e.printStackTrace();
}
}</contactentity></contactentity>
|
ContactEntity实体类代码:
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
|
package
com.yqy.yqy_testtxl;
import
android.graphics.Bitmap;
/**
* 联系人信息
* @author YQY
*
*/
public
class
ContactEntity {
/** 联系人名称 **/
private
String name;
/** 联系人号码 **/
private
String number;
/** 联系人头像 **/
private
Bitmap photo;
@Override
public
String toString() {
return
ContactEntity [name= + name + , number= + number
+ , photo= + photo + ];
}
public
ContactEntity(String name, String number, Bitmap photo) {
super
();
this
.name = name;
this
.number = number;
this
.photo = photo;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
String getNumber() {
return
number;
}
public
void
setNumber(String number) {
this
.number = number;
}
public
Bitmap getPhoto() {
return
photo;
}
public
void
setPhoto(Bitmap photo) {
this
.photo = photo;
}
}
|
显示到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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
private
void
initList() {
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(
new
MyAdapter());
}
class
MyAdapter
extends
BaseAdapter {
@Override
public
int
getCount() {
if
(mContacts !=
null
&& mContacts.size() >
0
) {
return
mContacts.size();
}
else
{
return
0
;
}
}
@Override
public
Object getItem(
int
position) {
if
(mContacts !=
null
&& mContacts.size() >
0
) {
return
mContacts.get(position);
}
else
{
return
null
;
}
}
@Override
public
long
getItemId(
int
arg0) {
// TODO Auto-generated method stub
return
arg0;
}
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
ViewHolder holder =
null
;
if
(convertView ==
null
) {
holder =
new
ViewHolder();
convertView = LayoutInflater.from(MainActivity.
this
).inflate(
R.layout.item_contact,
null
);
holder.name = (TextView) convertView.findViewById(R.id.tv_name);
holder.number = (TextView) convertView
.findViewById(R.id.tv_number);
holder.photo = (ImageView) convertView
.findViewById(R.id.iv_photo);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
ContactEntity contact = mContacts.get(position);
holder.name.setText(contact.getName()+);
holder.number.setText(contact.getNumber()+);
holder.photo.setImageBitmap(contact.getPhoto());
return
convertView;
}
class
ViewHolder {
TextView name;
TextView number;
ImageView photo;
}
}
|
MainActivity全部代码:
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
package
com.yqy.yqy_testtxl;
import
java.io.InputStream;
import
java.util.ArrayList;
import
android.app.Activity;
import
android.content.ContentResolver;
import
android.content.ContentUris;
import
android.database.Cursor;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.net.Uri;
import
android.os.Bundle;
import
android.provider.ContactsContract;
import
android.provider.ContactsContract.CommonDataKinds.Phone;
import
android.text.TextUtils;
import
android.util.Log;
import
android.view.LayoutInflater;
import
android.view.Menu;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
import
android.widget.ImageView;
import
android.widget.ListView;
import
android.widget.TextView;
public
class
MainActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getPhoneContacts();
Log.e(YQY, mContacts.size() + -------------mContacts---------);
initList();
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return
true
;
}
/** 联系人名称 **/
private
ArrayList<contactentity> mContacts =
new
ArrayList<contactentity>();
private
static
final
String[] PHONES_PROJECTION =
new
String[] {
Phone.DISPLAY_NAME, Phone.NUMBER, Phone.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 = getContentResolver();
try
{
// 获取手机联系人
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);
}
ContactEntity mContact =
new
ContactEntity(contactName,
phoneNumber, contactPhoto);
mContacts.add(mContact);
}
phoneCursor.close();
}
}
catch
(Exception e) {
e.printStackTrace();
}
}
private
void
initList() {
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(
new
MyAdapter());
}
class
MyAdapter
extends
BaseAdapter {
@Override
public
int
getCount() {
if
(mContacts !=
null
&& mContacts.size() >
0
) {
return
mContacts.size();
}
else
{
return
0
;
}
}
@Override
public
Object getItem(
int
position) {
if
(mContacts !=
null
&& mContacts.size() >
0
) {
return
mContacts.get(position);
}
else
{
return
null
;
}
}
@Override
public
long
getItemId(
int
arg0) {
// TODO Auto-generated method stub
return
arg0;
}
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
ViewHolder holder =
null
;
if
(convertView ==
null
) {
holder =
new
ViewHolder();
convertView = LayoutInflater.from(MainActivity.
this
).inflate(
R.layout.item_contact,
null
);
holder.name = (TextView) convertView.findViewById(R.id.tv_name);
holder.number = (TextView) convertView
.findViewById(R.id.tv_number);
holder.photo = (ImageView) convertView
.findViewById(R.id.iv_photo);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
ContactEntity contact = mContacts.get(position);
holder.name.setText(contact.getName()+);
holder.number.setText(contact.getNumber()+);
holder.photo.setImageBitmap(contact.getPhoto());
return
convertView;
}
class
ViewHolder {
TextView name;
TextView number;
ImageView photo;
}
}
}
</contactentity></contactentity>
|
Android学习交流群:523487222
(欢迎关注,第一时间推送博文信息)
点击链接加入群【Android学习群】
转自:http://www.2cto.com/kf/201505/398455.html