添加依赖 compile 'com.mylhyl:acp:1.1.7'
Acp.getInstance(this).request(new AcpOptions.Builder().setPermissions( Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE ).build(), new AcpListener() { @Override public void onGranted() { } @Override public void onDenied(Listpermissions) { Toast.makeText(LoginActivity.this, "获取权限失败!", Toast.LENGTH_SHORT).show(); finish(); } });
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
public class LoginActivity extends AppCompatActivity implements View.OnClickListener { /** 获取库Phone表字段 **/ private static final String[] PHONES_PROJECTION = new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Photo.PHOTO_ID, ContactsContract.CommonDataKinds.Phone.CONTACT_ID }; /** 联系人显示名称 **/ private static final int PHONES_DISPLAY_NAME = 0; /** 电话号码 **/ private static final int PHONES_NUMBER = 1; /** 头像ID **/ private static final int PHONES_PHOTO_ID = 2; /** 联系人的ID **/ private static final int PHONES_CONTACT_ID = 3; /** 联系人名称 **/ private ArrayListmContactsName = new ArrayList (); /** 联系人头像 **/ private ArrayList mContactsNumber = new ArrayList (); /** 联系人头像 **/ private ArrayList mContactsImg = new ArrayList (); private MyPhoneAdapter mAdapter; private ListView listview; private Button btn; private List lists=new ArrayList (); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); Acp.getInstance(this).request(new AcpOptions.Builder().setPermissions( Manifest.permission.CAMERA, Manifest.permission.VIBRATE, Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.INTERNET).build(), new AcpListener() { @Override public void onGranted() { } @Override public void onDenied(List permissions) { Toast.makeText(LoginActivity.this, "获取权限失败!", Toast.LENGTH_SHORT).show(); finish(); } }); initView(); } private void initView() { listview=(ListView) findViewById(R.id.listview); btn=(Button) findViewById(R.id.btn); btn.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn: getPhoneContacts(); break; } } private void getPhoneContacts() { // rely=(RelativeLayout) findViewById(R.id.relationId); ContentResolver resolver = getContentResolver(); List list=new ArrayList (); // 获取手机联系人 Cursor phoneCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null); // 不为空 if (phoneCursor != null) { while (phoneCursor.moveToNext()) { ContactVoBean cv=new ContactVoBean(); // 得到手机号码 String phoneNumber = phoneCursor.getString(PHONES_NUMBER); // 当手机号码为空的或者为空字段 跳过当前循环 if (TextUtils.isEmpty(phoneNumber)) continue; // 得到联系人名 String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME); // 得到联系人ID Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID); // 得到联系人头像ID Long imgid = phoneCursor.getLong(PHONES_PHOTO_ID); // 得到联系人头像Bitamp Bitmap contactPhoto = null; // imgid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的 if (imgid > 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.mipmap.ic_launcher); } cv.setName(contactName); //Log.i("11111", "name-----"+contactName); cv.setMoble(phoneNumber); cv.setImg(contactPhoto); //Log.i("11111", "img----"+contactPhoto); list.add(cv); } phoneCursor.close(); lists.clear(); lists.addAll(list); } mAdapter=new MyPhoneAdapter(this, lists); listview.setAdapter(mAdapter); } }
android:layout_height="wrap_content"
android:padding="8dp">
android:layout_width="50dp"
android:layout_height="50dp"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image"
android:layout_alignParentRight="true"
android:singleLine="true"
android:layout_marginLeft="20dp"
android:text="张三"
android:textSize="20dp" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image"
android:layout_below="@+id/name"
android:layout_alignParentRight="true"
android:layout_marginLeft="20dp"
android:text="13888888888"
android:textSize="15dp" />
import android.graphics.Bitmap; /** * Created by 14178 on 2018/10/22. * 手机联系人实体类 */ public class ContactVoBean { private long id; private String name; private String moble; private Bitmap img; public ContactVoBean() { super(); } public ContactVoBean(long id, String name, String moble, Bitmap img) { super(); this.id = id; this.name = name; this.moble = moble; this.img = img; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMoble() { return moble; } public void setMoble(String moble) { this.moble = moble; } public Bitmap getImg() { return img; } public void setImg(Bitmap img) { this.img = img; } @Override public String toString() { return "ContactVo [id=" + id + ", name=" + name + ", moble=" + moble + ", img=" + img + "]"; } }
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.jiuhong.mall.Bean.ContactVoBean; import com.jiuhong.mall.R; import java.util.List; /** * Created by 14178 on 2018/10/22. */ public class MyPhoneAdapter extends BaseAdapter { private Context mContext; private ListmData; public MyPhoneAdapter(Context mContext, List mData) { super(); this.mContext = mContext; this.mData = mData; } @Override public int getCount() { return mData!=null?mData.size():0; } @Override public Object getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return mData.get(position).getId(); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder mHolder = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate( R.layout.item_phone_list, null); mHolder = new ViewHolder(); mHolder.name = (TextView) convertView.findViewById(R.id.name); mHolder.moble = (TextView) convertView.findViewById(R.id.moble); mHolder.imgV = (ImageView) convertView.findViewById(R.id.image); convertView.setTag(mHolder); } else { mHolder = (ViewHolder) convertView.getTag(); mHolder.imgV.setImageResource(R.mipmap.ic_launcher); } ContactVoBean cv = mData.get(position); mHolder.name.setText(cv.getName()); mHolder.moble.setText(cv.getMoble()); mHolder.imgV.setImageBitmap(cv.getImg()); return convertView; } class ViewHolder { TextView name, moble; ImageView imgV; } }