sandeep03edu

ManageContacts

Dec 25th, 2022
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. public class ManageContacts {
  2.  
  3.     public static JSONObject getContacts(){
  4.  
  5.         try {
  6.             JSONObject contacts = new JSONObject();
  7.             JSONArray list = new JSONArray();
  8.             Cursor cur = MyApplication.getAppContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  9.                     new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER},
  10.                     null, null,  ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
  11.  
  12.             while (cur.moveToNext()) {
  13.                 JSONObject contact = new JSONObject();
  14.                 String name = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));// for  number
  15.                 String num = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));// for name
  16.  
  17.                 contact.put("phoneNo", num);
  18.                 contact.put("name", name);
  19.                 list.put(contact);
  20.             }
  21.             contacts.put("contactsList", list);
  22.             cur.close();
  23.             return contacts;
  24.         } catch (JSONException e) {
  25.             e.printStackTrace();
  26.         }
  27.         return null;
  28.     }
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment