Andriod 联系人读取


在activity_main.xml里面,用到LinearLayout

<ListView
        android:id="@+id/contacts_view"
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="8dp"></ListView>


MainActivity.java里面

    public ArrayAdapter<String> adapter;
    public List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView = (ListView)findViewById(R.id.contacts_view);
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);
        //没有权限时,调用requestPermission方法,弹出权限申请对话框 ,回调OnRequestPermissionRelust函数
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 1);
        }
        //有权限时,调用自定义函数,读取系统联系人
       else {
            readContacts();
        }
    }

下面重写OnRequestPermissionResult函数

    @Override
    //调用requestPermissions()后,系统弹出权限申请的对话框,选择后回调到下面这个函数,授权结果会封装到grantResults
    //grant授权
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    readContacts();
                } else {
                    Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }

自定义的函数readContacts(),使用ContentProvider类共享系统联系人

getContentResolver()  外界的程序通过ContentResolver接口可以访问ContentProvider提供的数据,在Activity当中通过getContentResolver()可以得到当前应用的ContentResolver实例

Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,String sortOrder) 通过Uri进行查询

private void readContacts() {
        Cursor cursor = null;
        try {
            //cursor指针 query询问 contract协议 kinds种类
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    list.add(displayName + '\n' + number);
                }
                //notify公布
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }



在Manifest.xml里面,要加上自定义的权限

<uses-permission android:name="android.permission.READ_CONTACTS"/>


在第一行代码里面Manifest会爆红是因为引入的包有问题,应该引入 “”android.Manifest”这个包。


运行截图如下



展开阅读全文

没有更多推荐了,返回首页