Android笔记: 获取手机联系人列表

举报
程思扬 发表于 2022/01/12 22:37:06 2022/01/12
【摘要】 下面直接贴代码 1.先写一个实体类,来放名字和号码 public class PhoneDto { private String name; //联系人姓名 ...

在这里插入图片描述下面直接贴代码

1.先写一个实体类,来放名字和号码

    public class PhoneDto {
        private String name;        //联系人姓名
        private String telPhone;    //电话号码
     
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public String getTelPhone() {
            return telPhone;
        }
     
        public void setTelPhone(String telPhone) {
            this.telPhone = telPhone;
        }
     
        public PhoneDto() {
        }
     
        public PhoneDto(String name, String telPhone) {
            this.name = name;
            this.telPhone = telPhone;
        }
    }

  
 
  • 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

2.写我们获取联系人的工具类

    public class PhoneUtil {
     
        // 号码
        public final static String NUM = ContactsContract.CommonDataKinds.Phone.NUMBER;
        // 联系人姓名
        public final static String NAME = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
     
        //上下文对象
        private Context context;
        //联系人提供者的uri
        private Uri phoneUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
     
        public PhoneUtil(Context context){
            this.context = context;
        }
     
        //获取所有联系人
        public List<PhoneDto> getPhone(){
            List<PhoneDto> phoneDtos = new ArrayList<>();
            ContentResolver cr = context.getContentResolver();
            Cursor cursor = cr.query(phoneUri,new String[]{NUM,NAME},null,null,null);
            while (cursor.moveToNext()){
                PhoneDto phoneDto = new PhoneDto(cursor.getString(cursor.getColumnIndex(NAME)),cursor.getString(cursor.getColumnIndex(NUM)));
                phoneDtos.add(phoneDto);
            }
            return phoneDtos;
        }
    }

  
 
  • 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

3.接下来贴主页面布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.test.content.MainActivity">
     
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/lv_main_list"></ListView>
     
    </LinearLayout>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

4.该贴主Activity代码了

    public class MainActivity extends AppCompatActivity {
     
     
        private List<PhoneDto> phoneDtos;
        private ListView lv_main_list;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            check();
        }
     
        /**
         * 检查权限
         */
        private void check() {
            //判断是否有权限
            if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS)
                    != PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_CONTACTS},201);
            }else{
                initViews();
            }
        }
     
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            if(requestCode==201){
                initViews();
            }else{
                return;
            }
        }
     
        private void initViews() {
            PhoneUtil phoneUtil = new PhoneUtil(this);
            phoneDtos = phoneUtil.getPhone();
            lv_main_list = (ListView) findViewById(R.id.lv_main_list);
            MyAdapter myAdapter = new MyAdapter();
            lv_main_list.setAdapter(myAdapter);
            //给listview增加点击事件
            /*lv_main_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //拨打电话
                    Intent intent = new Intent();
                    intent.setAction("android.intent.action.CALL");
                    intent.addCategory(Intent.CATEGORY_DEFAULT);
                    intent.setData(Uri.parse("tel:"+phoneDtos.get(position).getTelPhone()));
                    startActivity(intent);
                }
            });*/
        }
        //自定义适配器
        private class MyAdapter extends BaseAdapter {
     
            @Override
            public int getCount() {
                return phoneDtos.size();
            }
     
            @Override
            public Object getItem(int position) {
                return phoneDtos.get(position);
            }
     
            @Override
            public long getItemId(int position) {
                return position;
            }
     
            @SuppressLint("NewApi")
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                PhoneDto phoneDto = phoneDtos.get(position);
                LinearLayout linearLayout = new LinearLayout(MainActivity.this);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
                layoutParams.weight = 1;
                TextView tv_name = new TextView(MainActivity.this);
                tv_name.setId(View.generateViewId());
                tv_name.setLayoutParams(layoutParams);
                tv_name.setText(phoneDto.getName());
                TextView tv_num = new TextView(MainActivity.this);
                tv_num.setId(View.generateViewId());
                tv_num.setLayoutParams(layoutParams);
                tv_num.setText(phoneDto.getTelPhone());
                linearLayout.addView(tv_name);
                linearLayout.addView(tv_num);
                return linearLayout;
            }
        }
    }


  
 
  • 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

5.好了这样的话就已经完成了,大家有什么问题可以在下方留言。

文章来源: chengsy.blog.csdn.net,作者:程思扬,版权归原作者所有,如需转载,请联系作者。

原文链接:chengsy.blog.csdn.net/article/details/101702143

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。