1. 程式人生 > >Android開發獲取通訊錄

Android開發獲取通訊錄

獲取手機聯絡人,點選彈出選項

1、新增許可權


    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"
/>
<uses-permission android:name="android.permission.CALL_PHONE" />

2、實體類

/**
 * Created by Administrator on 2017/5/17.
 * 聯絡人
 */
public class ContactsInfo {

    public int _id;
    public String name;
    public String phone;

    public ContactsInfo() {
        super();
    }

    public
ContactsInfo(int _id, String name, String phone) { super(); this._id = _id; this.name = name; this.phone = phone; } @Override public String toString() { return "PersonInfo [_id=" + _id + ", name=" + name + ", phone=" + phone + "]"
; } }

3、工具類

public class ContactsMsgUtils {

    /**
     * 獲取聯絡人
     */
    public static List<ContactsInfo> getContactsInfos(Context context) {
        ContentResolver resolver = context.getContentResolver();
        List<ContactsInfo> infos = new ArrayList<ContactsInfo>();
        // 獲取聯絡人資料 訪問聯絡人的內容提供者
        // ContactsContract.AUTHORITY com.android.contacts 授權
        // 該內容提供者操作是需要讀寫許可權
        // matcher.addURI(ContactsContract.AUTHORITY, "raw_contacts",
        // RAW_CONTACTS);
        // matcher.addURI(ContactsContract.AUTHORITY, "raw_contacts/#/data",
        // RAW_CONTACTS_DATA);
        Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
        Cursor cursor1 = resolver.query(uri, new String[] { "_id" }, null,
                null, null);
        while (cursor1.moveToNext()) {
            int _id = cursor1.getInt(0);
            ContactsInfo info = new ContactsInfo();
            uri = Uri.parse("content://com.android.contacts/raw_contacts/"
                    + _id + "/data");
            Cursor cursor2 = resolver.query(uri, new String[] { "data1",
                    "mimetype" }, null, null, null);
            while (cursor2.moveToNext()) {
                String data1 = cursor2.getString(0);
                String mimetype = cursor2.getString(1);
                if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {// 號碼
                    info.phone = data1;
                } else if ("vnd.android.cursor.item/name".equals(mimetype)) {// 姓名
                    info.name = data1;
                }
            }
            cursor2.close();
            if (info.name!=null){
                infos.add(info);
            }
        }
        cursor1.close();
        return infos;
    }
}

4、Activity

public class SecondActivity extends AppCompatActivity {

    @BindView(R.id.rv_second)
    RecyclerView rvSecond;

    private ContactsAdapter adapter;
    private List<ContactsInfo> mDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        ButterKnife.bind(this);
        ContactsMsgUtils contactsMsgUtils = new ContactsMsgUtils();
        mDatas = contactsMsgUtils.getContactsInfos(this);
        adapter = new ContactsAdapter(this,mDatas);
        LinearLayoutManager myLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rvSecond.setLayoutManager(myLayoutManager);
        rvSecond.setAdapter(adapter);
        initEvent();
    }

    private void initEvent() {
        adapter.setOnItemClickLitener(new ContactsAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                ContactsInfo info = mDatas.get(position);
                final String phone = info.phone;
                String[] items = new String[]{"撥號", "傳送簡訊"};
                new AlertDialog.Builder(SecondActivity.this).setTitle("操作")
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                switch (which) {
                                    case 0:
                                        // 撥號 許可權
                                        startActivity(new Intent(
                                                Intent.ACTION_CALL, Uri
                                                .parse("tel:" + phone)));
                                        break;
                                    case 1:
                                        // 傳送簡訊
                                        startActivity(new Intent(
                                                Intent.ACTION_SENDTO, Uri
                                                .parse("sms:" + phone)));
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }).show();
            }
        });
    }
}