Before 2022/Android

안드로이드 주소록에서 전화번호 가져오기

Eljoe 2019. 7. 11. 15:29
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setData(ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, 0);

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  if (resultCode == RESULT_OK) {
    Cursor cursor = null;

    if (data != null) {
      cursor = getContentResolver().query(data.getData(),
      new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
      ContactsContract.CommonDataKinds.Phone.NUMBER}, null, null, null);
    }

    if (cursor != null) {
      cursor.moveToFirst();

      Log.e("cursor", "name : " + cursor.getString(0));
      Log.e("cursor", "number : " + cursor.getString(1));

      cursor.close();
    }
  }
  super.onActivityResult(requestCode, resultCode, data);
}