Customized Query to access Contents of android phone

Cursor cursor = managedQuery(Uri, null, null, null, null);
  1. First parameter are used for what you want to access there are built-in Uri used to access desired contents.
  • Browser.BOOKMARKS_URI 
  • Browser.SEARCHES_URI
  • CallLog.CONTENT_URI
  • MediaStore.Images.Media.INTERNAL_CONTENT_URI
  • MediaStore.Images.Media.EXTERNAL_CONTENT_URI 
  • Settings.CONTENT_URI 
2. Second parameter is called projection. In this parameter of managQuery we want to retrieve how many columns .

For Images


String[] projection = { MediaStore.Images.Thumbnails._ID, MediaStore.Images.Media.DATA ,MediaStore.Images.Thumbnails.DATA}
Cursor cursor = managedQuery(Uri, projection, null, null, null);

For Contact

Uri contacts=Uri.parse("People.CONTENT_URI");
String[]​projection​=​new​String[]
​​​​​​​​​​​​{ContactsContract.Contacts._ID,
​​​​​​​​​​ ContactsContract.Contacts.DISPLAY_NAME,
​​​​​​​​​​​​​ ContactsContract.Contacts.HAS_PHONE_NUMBER};
​​​​​​​​ Cursor​c​=​managedQuery(contacts,​projection,
​​​​​​​​​​​​ null,​null,​null);

3. The Third and Fourth parameter are used for Filtering retrieved data. Its also called WHERE clause of SQL . Now we want to retrieved all sContacts names which are start from Alphabetic A and end with any string.
String Filter="ContactsContract.Contacts.DISPLAY_NAME​+​“"LIKE​ 'A' OR 'a',";
   Cursor​c​=​managedQuery(contacts,​projection,
​​​​​​​​​​​​ filter,​null,​null);
4. Now we used Third and fourth parameter together for filtering
String filterColumn=​ContactsContract.Contacts.DISPLAY_NAME​+​“​LIKE​?”,
String[] filterName={"'a' OR 'A' "};
Cursor​c​=​managedQuery(contacts,​projection, filterColumn,​filterName,​null);

​​​​​​​​​
5. Fifth parameter are used for Sorting retrieved record . Its work like SQL ORDER BY. You can arrange record in ascending and descending order
String sorting="ContactsContract.Contacts.DISPLAY_NAME​+​"DESC" "
Cursor​c​=​managedQuery(contacts,​projection, filterColumn,​filterName,​sorting);
​​​​​​​​​

Post a Comment

0 Comments