谁有php获取用户手机通讯里的代码
谁有php获取用户手机通讯里的代码
在AndroidManifest.xml 中添加权限
<manifest xmlns:android="http://schemas.
package="com.palmv.getcontacts">
<!-- 读取联系人权限 -->
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<!-- 拨打电话权限 -->
<uses-permission android:name="android.permission.CALL_PHONE"/>
新增一个实体类:ContactEntity
package com.palmv.getcontacts;
import android.graphics.Bitmap;
/**
* @author:chendd
* @date:2016/7/14
* @description:
*/
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) {
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;
}
}
在activity中直接调用获取通讯录
package com.palmv.getcontacts;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getPhoneContacts();
}
/** 联系人名称 **/
private ArrayList<ContactEntity> mContacts = new ArrayList<ContactEntity>();
private static final String[] PHONES_PROJECTION = new String[] {
,
,
,
};
/** 联系人显示名称 **/
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(,
PHONES_PROJECTION, null, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
// 得到手机号码
String phoneNumber = phoneCursor
.getString(PHONES_NUMBER_INDEX);
// 当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
System.out.println("======================");
System.out.println("---手机号码:"+phoneNumber);
// 得到联系人名称
String contactName = phoneCursor
.getString(PHONES_DISPLAY_NAME_INDEX);
System.out.println("---联系人:"+contactName);
// 得到联系人ID
Long contactid = phoneCursor
.getLong(PHONES_CONTACT_ID_INDEX);
System.out.println("---联系人id:"+contactid);
// 得到联系人头像ID
Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);
System.out.println("---联系人头像:"+photoid);
// 得到联系人头像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);
System.err.println("---联系人有头像:"+photoid);
} else {
contactPhoto = BitmapFactory.decodeResource(
getResources(), R.mipmap.ic_launcher);
}
ContactEntity mContact = new ContactEntity(contactName,
phoneNumber, contactPhoto);
mContacts.add(mContact);
}
phoneCursor.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}