//权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
//依赖
compile 'io.reactivex.rxjava2:rxjava:2.1.7'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.3@aar'
//MainActivity布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="@+id/lv_contacts"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
//条目布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/phone_lv_img"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_margin="6dp"
android:src="@mipmap/ic_launcher"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingTop="4dp"
android:orientation="vertical"
android:layout_toRightOf="@+id/phone_lv_img">
<TextView
android:id="@+id/phone_lv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:text="我是"/>
<TextView
android:id="@+id/phone_lv_num"
android:layout_marginTop="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:text="132151531"/>
</LinearLayout>
<Button
android:id="@+id/phone_lv_add_friend"
android:layout_width="40dp"
android:layout_height="28dp"
android:layout_alignBottom="@+id/phone_lv_img"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:focusable="false"
android:text="添加"
android:textSize="14dp" />
</RelativeLayout>
//MainActivity----------------------------------------------------
package com.example.tongxunlu;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.Toast;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;
import java.util.ArrayList;
import java.util.List;
import io.reactivex.functions.Consumer;
public class MainActivity extends AppCompatActivity {
private ListView lv;
private ProgressDialog progressDialog;
private List<UserPhoneInfoBean> UserPhoneInfoList =new ArrayList<>();
private PhoneNumAdapter phoneNumLvAdapter;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = findViewById(R.id.lv_contacts);
requestPermissions();
//设置适配器
phoneNumLvAdapter = new PhoneNumAdapter(this,UserPhoneInfoList);
lv.setAdapter(phoneNumLvAdapter);
}
//6.0动态权限 Rxpremission 获取权限
private void requestPermissions() {
RxPermissions rxPermission = new RxPermissions(MainActivity.this);
rxPermission
.requestEach(Manifest.permission.READ_CONTACTS)
.subscribe(new Consumer<Permission>() {
@Override
public void accept(Permission permission) throws Exception {
if (permission.granted) {
showProgressDialog();
// 用户已经同意该权限
//T.showShort(getActivity(), "用户已经同意该权限");
//开启线程 调用getContacts() 查询通讯录
startThread();
} else if (permission.shouldShowRequestPermissionRationale) {
// 用户拒绝了该权限,没有选中『不再询问』(Never ask again),那么下次再次启动时,还会提示请求权限的对话框
//T.showShort(getActivity(), "拒绝该权限,下次再次启动时提示");
} else {
// 用户拒绝了该权限,并且选中『不再询问』
//T.showShort(getActivity(), "拒绝了该权限");
}
}
});
}
/**
* 开启线程 调用getContacts() 查询通讯录
*/
private void startThread() {
new Thread(){
@Override
public void run() {
getContacts();
}
}.start();
}
/**
* 获取通讯录集合
*/
private void getContacts() {
//联系人的Uri,也就是content://com.android.contacts/contacts
Uri uri = ContactsContract.Contacts.CONTENT_URI;
//指定获取_id和display_name两列数据,display_name即为姓名
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
//根据Uri查询相应的ContentProvider,cursor为获取到的数据集
ContentResolver contentResolver = MainActivity.this.getContentResolver();
Cursor cursor = contentResolver.query(uri, projection, null, null, null);
//String[] arr = new String[cursor.getCount()];
int i = 0;
if (cursor != null && cursor.moveToFirst()) {
do {
Long id = cursor.getLong(0);
//获取姓名
String name = cursor.getString(1);
//指定获取NUMBER这一列数据
String[] phoneProjection = new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER
};
//根据联系人的ID获取此人的电话号码
Cursor phonesCusor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
phoneProjection,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id,
null,
null);
//因为每个联系人可能有多个电话号码,所以需要遍历
if (phonesCusor != null && phonesCusor.moveToFirst()) {
/*do {
String num = phonesCusor.getString(0);
arr[i] += " , 电话号码:" + num;
}while (phonesCusor.moveToNext());*/
String num = phonesCusor.getString(0);
//arr[i] += " , 电话号码:" + num;
UserPhoneInfoBean userPhoneInfoBean = new UserPhoneInfoBean(name,num);
UserPhoneInfoList.add(userPhoneInfoBean);
while (phonesCusor.moveToNext()){
i++;
String num1 = phonesCusor.getString(0);
//arr[i] = id + " , 姓名:" + name + " , 电话号码:" + num1;
UserPhoneInfoBean userPhoneInfoBean1 = new UserPhoneInfoBean(name,num1);
UserPhoneInfoList.add(userPhoneInfoBean1);
}
}
i++;
} while (cursor.moveToNext());
}
Message m = Message.obtain();
m.what=1;
handler.sendMessage(m);
}
/**
* 开启加载提示
*/
private void showProgressDialog(){
progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setTitle("手机通讯录");
progressDialog.setMessage("努力加载中");
progressDialog.show();
}
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==1){
phoneNumLvAdapter.notifyDataSetChanged();
progressDialog.dismiss();
Toast.makeText(MainActivity.this,"获取成功",Toast.LENGTH_SHORT).show();
}
}
};
}
//适配器-------------------------------------------
package com.example.tongxunlu;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List
public class PhoneNumAdapter extends BaseAdapter{
private Context context;
private List<UserPhoneInfoBean> list=new ArrayList<>();
public PhoneNumAdapter(Context context, List<UserPhoneInfoBean> list) {
this.context = context;
this.list = list;
}
//接口回调
private PhoneFriendOnClick onClick;
public interface PhoneFriendOnClick{
void addFriend(UserPhoneInfoBean userPhoneInfoBean);
}
public void setPhoneFriendOnClick(PhoneFriendOnClick onClick){
this.onClick=onClick;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if(convertView==null){
vh=new ViewHolder();
convertView=View.inflate(context, R.layout.item_phonenum,null);
vh.name=convertView.findViewById(R.id.phone_lv_name);
vh.num=convertView.findViewById(R.id.phone_lv_num);
vh.addFriend=convertView.findViewById(R.id.phone_lv_add_friend);
convertView.setTag(vh);
}else{
vh= (ViewHolder) convertView.getTag();
}
final UserPhoneInfoBean userPhoneInfoBean = list.get(position);
vh.name.setText(userPhoneInfoBean.getName());
vh.num.setText(userPhoneInfoBean.getPhoneNum());
vh.addFriend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClick.addFriend(userPhoneInfoBean);
}
});
return convertView;
}
class ViewHolder{
TextView name;
TextView num;
Button addFriend;
}
}
Bean类------------------------------------------------------
package com.example.tongxunlu;
public class UserPhoneInfoBean {
private String name;
private String phoneNum;
public UserPhoneInfoBean(String name, String phoneNum) {
this.name = name;
this.phoneNum = phoneNum;
}
public UserPhoneInfoBean() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
@Override
public String toString() {
return "UserPhoneInfoBean{" +
"name='" + name + '\'' +
", phoneNum='" + phoneNum + '\'' +
'}';
}
}