Get contact data 연락처 정보 가져오기
Android 2019. 10. 16. 20:28 |반응형
You can retrieve the contact data.
연락처 정보를 모두 받아 올 수 있다.
<AndroidManifest.xml>
1 2 | <uses-permission android:name="android.permission.READ_CONTACTS"/> <uses-permission android:name="android.permission.WRITE_CONTACTS"/> |
<MainActivity.java>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | public class MainActivity extends AppCompatActivity { private final int MY_PERMISSION_REQUEST_CONTACTS = 1001; private final int MY_ACTIVITY_REQUEST_CODE = 2001; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String[] permissionRequests = { Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS }; checkPermissions(permissionRequests); textView = findViewById(R.id.textView); Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { chooseContacts(); } }); } public void checkPermissions(String[] permissionRequests) { final ArrayList<String> permissionRequestList = new ArrayList<String>(); for (final String request : permissionRequests) { if (ContextCompat.checkSelfPermission(this, request) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, request)) { // Redundant in this case permissionRequestList.add(request); } else { permissionRequestList.add(request); } } } if (!permissionRequestList.isEmpty()) { final String[] results = new String[permissionRequestList.size()]; permissionRequestList.toArray(results); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("info"); String msg = "This app won't work properly unless you grant below permissions."; for (String str : results) msg += ("\n- "+ str); builder.setMessage(msg); builder.setIcon(android.R.drawable.ic_dialog_info); builder.setNeutralButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ActivityCompat.requestPermissions(MainActivity.this, results, MY_PERMISSION_REQUEST_CONTACTS); } }); AlertDialog dialog = builder.create(); dialog.show(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case MY_PERMISSION_REQUEST_CONTACTS: { for (int i = 0; i < grantResults.length; i++) { if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, permissions[i] + " permission granted.", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, permissions[i] + " permission denied.", Toast.LENGTH_SHORT).show(); } } } } } public void chooseContacts() { Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(contactPickerIntent, MY_ACTIVITY_REQUEST_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { if (resultCode == RESULT_OK) { if (requestCode == MY_ACTIVITY_REQUEST_CODE) { try { Uri contactsUri = data.getData(); String id = contactsUri.getLastPathSegment(); getContacts(id); } catch (Exception e) { e.printStackTrace(); } } } } public void getContacts(String id) { Cursor cursor; String name; String phone; try { cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[] {id}, null); if (cursor.moveToFirst()) { name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); textView.setText("Name: " + name + '\n' + "Phone: " + phone + '\n'); String columns[] = cursor.getColumnNames(); for (String column : columns) { int index = cursor.getColumnIndex(column); String columnOutput = ("#" + index + " -> [" + column + "] = " + cursor.getString(index)); textView.append('\n' + columnOutput); } cursor.close(); } } catch (Exception e) { e.printStackTrace(); } } } |
Run the app.
Choose contact.
Display contact data.
반응형
'Android' 카테고리의 다른 글
Take a photo with a camera app and save the full-size photo (0) | 2019.10.18 |
---|---|
Take a photo with a camera app and get the thumbnail (0) | 2019.10.18 |
Get multiple sensor data 여러가지 센서 데이터 받아 오기 (0) | 2019.10.15 |
Character encoding simple check 간단한 한글 인코딩 확인 (0) | 2019.10.12 |
Data request to the web server 웹서버에 데이터 요청하고 응답 받기 (0) | 2019.10.12 |