获取联系人之动态获取权限

获取联系人之动态获取权限

联系人数据是用户比较隐私的数据,在去获取的时候需要动态申请权限,当然用户已经授权过的就不用再次申请直接获取联系人数据就行,下面是一个相应的Demo

//首先AndroidManifest.xml文件里需要添加相应的权限配置,这是必须的要求
<uses-permission android:name="android.permission.READ_CONTACTS"/>

//以下是获取权限以及读取联系人信息的代码
package com.study.readcontactdemo;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

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

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

    public void loadContacts() {
        ListView listView = findViewById(R.id.listView);
        adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,contactsList);
        listView.setAdapter(adapter);
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},1);
        } else {
            readContacts();
        }
    }

    public void readContacts() {
        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,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));
                contactsList.add(displayName + "\n" + number);
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {

        }
        finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 1 && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            readContacts();
        } else {
            Toast.makeText(this,"对不起,请您开启读取联系人权限",Toast.LENGTH_SHORT);
        }
    }
}

当然以上代码还包括了一项新的内容就是访问其他程序中的数据,也就是跨程序访问数据,这是通过内容提供器实现的,代码如下

public void readContacts() {
        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,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));
                contactsList.add(displayName + "\n" + number);
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {

        }
        finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

总结,以上内容使用到了权限动态申请,还用到了通过内容提供器获取其他程序共享的数据,在获取联系人信息的时候有没有发现和sqlite数据库查询很相像,同样得到了Cursor对象,然后通过Cursor对象获取通讯录信息。

说点什么

avatar
  Subscribe  
提醒