1. 程式人生 > >android 選擇特定聯絡人資料

android 選擇特定聯絡人資料

android讀取特定聯絡人是真的煩, 網上程式碼一大堆,但是拿過來又不好使,無奈,只有自己一點一點弄了(找資料+測試 = 一天時間)。
首先需要跳轉到聯絡人列表:

        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, PEOPLE_REQUEST_CODE);
}

接下來重新onActivityResult方法

if(resultCode == RESULT_OK){
            if(requestCode == PEOPLE_REQUEST_CODE){
                if(null == data){
                    return;
                }
                //獲得選取的聯絡人資訊
                Uri mUri = data.getData();
                String [] contact=getPhoneContacts(mUri);
                if
(contact != null){ Map<String,String> map = new HashMap<String,String>(); map.put(contact[0],contact[1]); contacts.add(map); adapter.notifyDataSetChanged(); } } }
private
String[] getPhoneContacts(Uri uri){ String [] contacts = new String[2]; String[] projectionNumber = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}; Cursor cursorNumber = getContentResolver().query(uri, projectionNumber, null, null, null); Log.e(TAG, "Cursor: "+cursorNumber.getPosition() ); // If the cursor returned is valid, get the phone number if (cursorNumber != null && cursorNumber.moveToFirst()) { int numberIndex = cursorNumber.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); Log.e(TAG, "getPhoneContacts: numberIndex"+"-->"+numberIndex ); String number = cursorNumber.getString(numberIndex); // Do something with the phone number number = number.replaceAll("-",""); number = number.replaceAll(" ",""); Log.e(TAG, "getPhoneContacts: number"+"-->"+number ); contacts[1] = number; cursorNumber.close(); }else{ cursorNumber.close(); showMissingPermissionDialog(); return null; } String[] projectionName = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}; Cursor cursorName = getContentResolver().query(uri, projectionName, null, null, null); if (cursorName != null && cursorName.moveToFirst()) { int nameIndex = cursorName.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); Log.e(TAG, "getPhoneContacts: nameIndex"+"-->"+nameIndex ); String name = cursorName.getString(nameIndex); Log.e(TAG, "getPhoneContacts: name"+"-->"+name ); // Do something with the phone name contacts[0] = name; cursorName.close(); }else{ cursorName.close(); showMissingPermissionDialog(); return null; } return contacts; } /** * 顯示提示資訊 * * @since 2.5.0 */ private void showMissingPermissionDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.notifyTitle); builder.setMessage(R.string.notifyContactMsg); // 拒絕, 退出應用 builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // 設定,管理許可權 builder.setPositiveButton(R.string.setting, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { startAppSettings(); } }); builder.setCancelable(false); builder.show(); } /** * 啟動應用的設定 * * @since 2.5.0 */ private void startAppSettings() { Intent intent = new Intent( Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse("package:" + getPackageName())); startActivityForResult(intent, SETTING_REQUESTCODE); }

這是我最後一點一點測試出來的結果,目前華為、小米測試可以獲取
最後附上資料連結