Home > Blockchain >  How to read all contact in java android API 32
How to read all contact in java android API 32

Time:04-21

How to read all contact in java android How to read all contact in java android API 32

CodePudding user response:

Like This

private void readContacts() {

    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

    JSONArray js = new JSONArray();

    while (phones.moveToNext()) {

        @SuppressLint("Range") String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        @SuppressLint("Range") String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        js.put(name   phoneNumber);
        String jsonStr = js.toString();


        JSONObject namee = new JSONObject();
        try {
            namee.put("names", jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();


        }
    }

    getSharedPreferences("number", MODE_PRIVATE).edit().putString("number", String.valueOf(js)).apply();
}
  • Related