| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2113 人关注过本帖
标题:谁有php获取用户手机通讯里的代码
只看楼主 加入收藏
guang2356447
Rank: 2
等 级:论坛游民
帖 子:434
专家分:31
注 册:2007-7-10
结帖率:65.85%
收藏
已结贴  问题点数:10 回复次数:1 
谁有php获取用户手机通讯里的代码
谁有php获取用户手机通讯里的代码
搜索更多相关主题的帖子: 获取 用户 通讯 手机 代码 
2019-10-26 22:20
driver55
Rank: 2
等 级:论坛游民
帖 子:1
专家分:10
注 册:2019-10-28
收藏
得分:10 
在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();
        }
    }
}
2019-10-28 19:50
快速回复:谁有php获取用户手机通讯里的代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024943 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved