In my application I need to give user opportinity to see/edit all available fields of ContactsContract.
How should I get/see all available fields - including any custom ones?
First get all of the names of people that exist in your phone:
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC");
while (phones.moveToNext()) {
name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
namelist.add(name);
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
number_list.add(phoneNumber);
detaillist.put(name, phoneNumber);
}
phones.close();
}
Now get contact id and get other information like phone number, email id, address, organization by using contact id:
protected void get_UserContactId(String item_clicked)
{
System.out.println("i m in fincution contact id");
ContentResolver localContentResolver = this.getContentResolver();
Cursor contactLookupCursor =
localContentResolver.query(
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(item_clicked)),
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},
null,
null,
null);
try {
while(contactLookupCursor.moveToNext()){
contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
user_contact_id = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
System.out.println("contatc id id"+user_contact_id);
}
}
finally {
contactLookupCursor.close();
}
}
protected void get_UserEmail(String item_clicked)
{
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + user_contact_id, null, null);
while (emails.moveToNext()) {
// This would allow you get several email addresses
user_email_id = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String user_email_type=emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
System.out.println("the email type"+user_email_type);
System.out.println("email address might be"+emailAddress);
contact_attribute_type.add(EMAIL_TYPE[(Integer.parseInt(user_email_type))-1]);
contact_attribute_value.add(user_email_id);
}
emails.close();
}
protected void get_UserNumber(String item_clicked) {
// TODO Auto-generated method stub
final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED,
ContactsContract.Contacts.TIMES_CONTACTED,
ContactsContract.Contacts.CONTACT_PRESENCE,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
};
String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" +item_clicked+ "\" )";
Cursor c = this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
this.startManagingCursor(c);
if (c.moveToNext())
{
String id = c.getString(0);
ArrayList<String> phones = new ArrayList<String>();
Cursor pCur = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
while (pCur.moveToNext())
{
phones.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
user_number=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String number_type=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
System.out.println("the number type---"+number_type);
System.out.println("user no. in share is"+user_number);
contact_attribute_type.add(PHONE_TYPE[(Integer.parseInt(number_type))-1]);
contact_attribute_value.add(user_number);
// Log.i("", name_to_search+ " has the following phone number "+ pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
pCur.close();
}
}
protected String get_UserAddress() {
// TODO Auto-generated method stub
try {
System.out.println(" i m in user address");
Cursor address = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + client_contact_id, null, null);
while (address.moveToNext()) {
// This would allow you get several email addresses
user_home_address = address.getString(
address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DATA));
String user_address_type=address.getString(
address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
System.out.println("the user address type"+user_address_type);
System.out.println(" address might be"+user_home_address);
contact_attribute_type.add(ADDRESS_TYPE[(Integer.parseInt(user_address_type))-1]);
contact_attribute_value.add(user_home_address);
}
address.close();
}
catch(Exception e)
{
System.out.println("this exception due to website"+e);
}
return(user_home_address);
}
protected void get_UserWebsite() {
// TODO Auto-generated method stub
try{
Cursor websiteNameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,new String[] {Website.URL}, ContactsContract.Data.CONTACT_ID + " = " + user_contact_id + " AND ContactsContract.Data.MIMETYPE = '"
+ ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE
+ "'",null,null);
websiteNameCursor.moveToNext();
user_website=(websiteNameCursor.getString(websiteNameCursor.getColumnIndex(Website.URL)));
System.out.println("this is my website"+(websiteNameCursor.getString(websiteNameCursor.getColumnIndex(Website.URL))));
contact_attribute_type.add("Website");
contact_attribute_value.add(user_website);
}
catch(Exception e)
{
user_website=null;
System.out.println("this website is"+user_website);
System.out.println("this exception in website"+e);
}
}
protected void get_UserOrganization() {
// TODO Auto-generated method stub
try{
Cursor organizationNameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,new String[] {Organization.COMPANY}, ContactsContract.Data.CONTACT_ID + " = " + user_contact_id + " AND ContactsContract.Data.MIMETYPE = '"
+ ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE
+ "'",null,null);
organizationNameCursor.moveToNext();
user_company=organizationNameCursor.getString(organizationNameCursor.getColumnIndex(Organization.COMPANY));
// String user_company_type=organizationNameCursor.getString(organizationNameCursor.getColumnIndex(Organization.TYPE));
// System.out.println("the company type "+user_company_type);
System.out.println("this is my organization"+user_company);
contact_attribute_type.add("Company");
contact_attribute_value.add(user_company);
}
catch(Exception e)
{
user_company=null;
System.out.println("user company name is"+user_company);
System.out.println("this exception in org"+e);
}
}
}
By doing this you can get all the fields of contacts-contract.
This logs the name and value of all columns in all contact rows. Tested on Galaxy S6 with native contacts app. I needed it to find the column name that is used for custom SMS notification sound (sec_custom_alert).
private void enumerateColumns() {
Cursor cursor = getApplicationContext().getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
for(int i=0; i<cursor.getColumnCount();i++)
{
Log.d("myActivity", cursor.getColumnName(i) + " : " + cursor.getString(i));
}
} while (cursor.moveToNext());
cursor.close();
}
}
Related
My task is to find data on the first entered telephone number.
Do so but fails due to the different format in the database.
Maybe there's another way or how to do the query in one format
private DataContact getDataContact1(String numberPhone) {
DataContact dataContact = new DataContact();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor == null) {
return dataContact;
}
countContact = cursor.getCount();
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//numbers
Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId + " AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE + " AND " +
ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + numberPhone + "%'", null, null);
if (phones == null) {
continue;
}
if (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
dataContact.setNumberFormat(number);
dataContact.setNumber(FormatConstants.replaceOnlyNumbers(number));
dataContact.setName(name);
return dataContact;
}
phones.close();
}
cursor.close();
return dataContact;
}
Hello i am working on a contact directory,i have made a custom call logs,Noe i want to display the contact details when one of the contact is clicked,I have first fetched contact_id from the contact number,and then by searching over the internet i got a function to get all details from contact id,I have put it in my code,But its not working,not giving me results,
public void getDetials(String contactID) {
Uri myPhoneUri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
Uri.encode(contactID));
// Query the table
#SuppressWarnings("deprecation")
Cursor phoneCursor = managedQuery(myPhoneUri, null, null, null, null);
// Get the phone numbers from the contact
for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor
.moveToNext()) {
// Get a phone number
String phoneNumber = phoneCursor
.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
//get name
String phonename = phoneCursor
.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
//get email
String phoneemail = phoneCursor
.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.DATA));
//get image uri..!!
String img_uri = phoneCursor
.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
System.out
.println("=============my phone number ,is==================="
+ phoneNumber + "======in call info=="+"\n =========name is===="+phonename+"=================email is========"+phoneemail);
System.out.println("======myimgae url fopr the contact is============="+img_uri);
}
}
Here is method to read all phone numbers and emails for specified contact:
public void getContactDetails(int contactId) {
Log.d("Details", "---");
Log.d("Details", "Contact : " + contactId);
final Cursor phoneCursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
},
Data.CONTACT_ID + "=?",
new String[] {String.valueOf(contactId)}, null);
try {
final int idxAvatarUri = phoneCursor.getColumnIndexOrThrow(
ContactsContract.CommonDataKinds.Phone.PHOTO_URI);
final int idxName = phoneCursor.getColumnIndexOrThrow(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
final int idxPhone = phoneCursor.getColumnIndexOrThrow(
ContactsContract.CommonDataKinds.Phone.NUMBER);
while (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(idxPhone);
String name = phoneCursor.getString(idxName);
String avatarUri = phoneCursor.getString(idxAvatarUri);
Log.d("Details", "Phone number: " + phoneNumber);
Log.d("Details", "Name: " + name);
Log.d("Details", "Avatar URI: " + avatarUri);
}
} finally {
phoneCursor.close();
}
final Cursor emailCursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Email.ADDRESS,
},
Data.CONTACT_ID + "=?",
new String[] {String.valueOf(contactId)}, null);
try {
final int idxAddress = emailCursor.getColumnIndexOrThrow(
ContactsContract.CommonDataKinds.Email.ADDRESS);
while (emailCursor.moveToNext()) {
String address = emailCursor.getString(idxAddress);
Log.d("Details", "Email: " + address);
}
} finally {
emailCursor.close();
}
}
Please note that single contact can hold multiple phone numbers and emails.
Here is how to get all contacts details:
final Cursor cur = getContentResolver().query(Data.CONTENT_URI,
new String[]{Data.CONTACT_ID}, null, null, null);
try {
final int idxId = cur.getColumnIndex(Data.CONTACT_ID);
while (cur.moveToNext()) {
final int id = cur.getInt(idxId);
getContactDetails(id);
}
} finally {
cur.close();
}
Don't forget to add permission:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
I have made an app in which I need all the contacts available in the phone-book. I display these numbers in a list.The app works fine but some times the app force closes because the cursor returns null.This does not happens always but it happens some times.Now how do I handle this ????
Code
public static JSONArray getAllContactList(Context context) {
Cursor c = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
alAllContacts = new ArrayList<ContactModel>();
while (!(c == null) && c.moveToNext()) {
String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String number = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (number.equalsIgnoreCase("1")) {
// Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
// ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = '" + id + "'", null, null);
Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
while (phones.moveToNext()) {
String contactName = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String contactNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactNumber = contactNumber.replace("+", "");
if (contactNumber.length() > 10) {
contactNumber = contactNumber.substring(2);
}
// contactNumber.replace("+91", "");
alAllContacts.add(new ContactModel(contactName, contactNumber));
//
}
}
}
c.close();
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < alAllContacts.size(); i++) {
jsonArray.put(alAllContacts.get(i).getJSONObject());
}
return jsonArray;
}
the logcat says that i am getting null pointer at this line
while (phones.moveToNext()) {
Also some times i get force close because by dialog is running,So is my code for showing the progress bar correct
public static void showProgress(Context context, String msg, boolean isVisible) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(context);
progressDialog.setMessage(msg);
progressDialog.setCancelable(false);
}
if (isVisible) {
progressDialog.show();
} else if (isVisible == false) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
I use this code for getting all contacts and it does the job.
private static void backupContacts(Context context) {
String[] pCurProjection = new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.LABEL,
ContactsContract.CommonDataKinds.Phone.IS_PRIMARY
};
String contactSelection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
String[] contactProjection = new String[]{
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
Cursor contactCursor = context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
contactProjection,
contactSelection,
null,
ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
int contactId;
String contactName;
if (null != contactCursor) {
while (contactCursor.moveToNext()) {
contactId = contactCursor.getInt(
contactCursor.getColumnIndex(ContactsContract.Contacts._ID));
contactName = contactCursor.getString(
contactCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
System.out.println(contactId + ": " + contactName);
Cursor pCur = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
pCurProjection,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{String.valueOf(contactId)},
null);
if (null != pCur) {
while (pCur.moveToNext()) {
int id = pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone._ID);
String number = (pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
String type = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));//this line returns Null but it does not effect on the code(Does not Throws NullPointerException). if you found fix for it please let me know
int isPrimary = pCur.getInt(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.IS_PRIMARY));
System.out.println(id + ": " + number + "\n" + type + "\n" + isPrimary);
}
pCur.close();
}
}
contactCursor.close();
}
}
if you have any question let me know :)
I'm displaying all contact list,I have used AsyncTask for that I wanna show Progress Update dialog so user can understand how much completion time left.I have following code for that but not getting clear,How to show progressUpdate dialog.
Code
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Cursor cur;
ContentResolver cr;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cr = getContentResolver();
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,
null);
GetContact contact = new GetContact();
contact.execute(cur);
}
class GetContact extends AsyncTask<Cursor, Void, Void> {
private ProgressDialog progress;
#Override
protected Void doInBackground(Cursor... params) {
// TODO Auto-generated method stub
System.out.println("value of param==================="
+ params.length);
readContacts();
return null;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progress = ProgressDialog
.show(MainActivity.this, "", "Loding.....");
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progress.dismiss();
}
}
public void readContacts() {
// ContentResolver cr = getContentResolver();
// Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
// null, null, null);
System.out.println("value of cursor=================================="
+ cur.getCount());
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out
.println("name : ==================================="
+ name + ", ID : " + id);
// get the phone number
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
String phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out
.println("phone++++++++++++++++++++++++++++++++++"
+ phone);
}
pCur.close();
// get email and type
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
System.out.println("Email " + email + " Email Type : "
+ emailType);
}
emailCur.close();
// Get note.......
String noteWhere = ContactsContract.Data.CONTACT_ID
+ " = ? AND " + ContactsContract.Data.MIMETYPE
+ " = ?";
String[] noteWhereParams = new String[] {
id,
ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE };
Cursor noteCur = cr.query(
ContactsContract.Data.CONTENT_URI, null, noteWhere,
noteWhereParams, null);
if (noteCur.moveToFirst()) {
String note = noteCur
.getString(noteCur
.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
System.out.println("Note " + note);
}
noteCur.close();
// Get Postal Address....
String addrWhere = ContactsContract.Data.CONTACT_ID
+ " = ? AND " + ContactsContract.Data.MIMETYPE
+ " = ?";
String[] addrWhereParams = new String[] {
id,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };
Cursor addrCur = cr.query(
ContactsContract.Data.CONTENT_URI, null, null,
null, null);
while (addrCur.moveToNext()) {
String poBox = addrCur
.getString(addrCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
String street = addrCur
.getString(addrCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String city = addrCur
.getString(addrCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String state = addrCur
.getString(addrCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String postalCode = addrCur
.getString(addrCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String country = addrCur
.getString(addrCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
String type = addrCur
.getString(addrCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
// Do something with these....
}
addrCur.close();
// Get Instant Messenger.........
String imWhere = ContactsContract.Data.CONTACT_ID
+ " = ? AND " + ContactsContract.Data.MIMETYPE
+ " = ?";
String[] imWhereParams = new String[] {
id,
ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE };
Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI,
null, imWhere, imWhereParams, null);
if (imCur.moveToFirst()) {
String imName = imCur
.getString(imCur
.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
String imType;
imType = imCur
.getString(imCur
.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
}
imCur.close();
// Get Organizations.........
String orgWhere = ContactsContract.Data.CONTACT_ID
+ " = ? AND " + ContactsContract.Data.MIMETYPE
+ " = ?";
String[] orgWhereParams = new String[] {
id,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE };
Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI,
null, orgWhere, orgWhereParams, null);
if (orgCur.moveToFirst()) {
String orgName = orgCur
.getString(orgCur
.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
String title = orgCur
.getString(orgCur
.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
}
orgCur.close();
}
}
}
}
}
1) You have to override onProgressUpdate() of AsyncTask. There you can update your progresDialog.
2) Move readContacts() into GetContact class
3) You have to call publishProgress() somewhere in your readContacts() loop to notify that progress was updated and onProgressUpdate() will be called.
I want to know if its possible to fetch contacts which exist in SIM card or phonebook only. Right now I am using the following code to fetch contacts and it fetches all the contacts even my gmail and Facebook Contacts.
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, Phone.DISPLAY_NAME + " ASC");
if (cursor.getCount() > 0)
{
while (cursor.moveToNext())
{
PhoneBookUserEntity user = new PhoneBookUserEntity();
// Pick out the ID, and the Display name of the
// contact from the current row of the cursor
user.setId(cursor.getString(cursor.getColumnIndex(BaseColumns._ID)));
user.setPhoneBookName(cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME)));
if(user.getphonebookname().length() > 4)
username = user.getphonebookname();//.substring(0,4);
else
username = user.getphonebookname();//.substring(0,1);
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
// if (Boolean.parseBoolean(hasPhone)) {
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ user.getId(), null, null);
while (phones.moveToNext()) {
user.sePhoneNumber(phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
phones.close();
//}
// user.sePhoneNumber(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + user.getId(), null, null);
while (emails.moveToNext()) {
// This would allow you get several email addresses
user.setEmailAddress(emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
}
emails.close();
user.setImageURI(getPhotoUri(user.getId()));
SearchContactsActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
_progressDialog.setMessage("Copying over your local phone book. Retrieving contact information for \n"+ username.toUpperCase());
}
});
arraylist.add(user);
}
}
cursor.close();
For Sim contact only you can use below code
private void allSIMContact()
{
try
{
String ClsSimPhonename = null;
String ClsSimphoneNo = null;
Uri simUri = Uri.parse("content://icc/adn");
Cursor cursorSim = this.getContentResolver().query(simUri,null,null,null,null);
Log.i("PhoneContact", "total: "+cursorSim.getCount());
while (cursorSim.moveToNext())
{
ClsSimPhonename =cursorSim.getString(cursorSim.getColumnIndex("name"));
ClsSimphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
ClsSimphoneNo.replaceAll("\\D","");
ClsSimphoneNo.replaceAll("&", "");
ClsSimPhonename=ClsSimPhonename.replace("|","");
Log.i("PhoneContact", "name: "+ClsSimPhonename+" phone: "+ClsSimphoneNo);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
Cursor cursor = getContacts();
getBundleValues2();
String displayName = "";
while (cursor.moveToNext()) {
//taking id name and phone number from contacts using content provider
String displayid = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
displayName = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String displayphnno = "";
ContentResolver cr = getContentResolver();
if (Integer.parseInt(cursor.getString(cursor
.getColumnIndex(ContactsContract.Data.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { displayid }, null);
while (pCur.moveToNext()) {
displayphnno = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
+ "\n";
ContactProviderViewGroup v6;
//using view group appending it to an activity
if (colorFlag) {
v6 = new ContactProviderViewGroup(this, displayName,
displayphnno, passedid, colorFlag);
colorFlag = false;
} else {
v6 = new ContactProviderViewGroup(this, displayName,
displayphnno, passedid, colorFlag);
colorFlag=true;
}
LL1.addView(v6);
}
pCur.close();
}
}
}
private void getBundleValues2() {
Intent i = getIntent();
Bundle myBundle = i.getExtras();
if (myBundle != null) {
passedid = myBundle.getInt("gid");
}
}
private Cursor getContacts() {
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER };
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ ("1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs,
sortOrder);
}