I access the contacts list this way:
CursorLoader oCursorLoader = new CursorLoader(MyContext, ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
Cursor oCursor = oCursorLoader.loadInBackground();
int contactId = oCursor.getColumnIndex(ContactsContract.Contacts._ID);
int name = oCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
oCursor.moveToFirst();
if(oCursor.isAfterLast()==false) {
do {
String sId = oCursor.getString(contactId);
String phName = oCursor.getString(name);
...more code...
} while (oCursor.moveToNext());
}
I want to save the ID for a contact in a database to do some processing later on. For example, I want to save latest contacts called.
My question is:
Does the ID for a contact remains the same even if the contact is modified?
For example, if I save in the database the id "44" por user "Boss", does that ID remains the same even if the device is rebooted, or contact is modified (for example its name or phone number or email address)?
Related
I've been trying to retrieve SMS information but have only been to retrieve everything BUT the display name of contacts. strDisplayNameonly shows the numerical value of a particular contact and not the name itself. All other string version of data shows their corresponding information correctly (aside from "date"). I've tried other implementations of getting the display name from other answers, but doing so causes the app to crash when trying to test.
Uri smsData = Uri.parse("content://sms/" + folderName);
String[] id = new String[]
{
"_id", "person", "address",
"body", "date"
};
ContentResolver contentResolver = getContentResolver();
Cursor smsCursor = contentResolver.query(smsData, null, null, null, null);
// Retrieve index of data
int indexContactID = smsCursor.getColumnIndex(id[0]);
int indexDisplayName = smsCursor.getColumnIndex(id[1]);
int indexPhoneNumber = smsCursor.getColumnIndex(id[2]);
int indexMsg = smsCursor.getColumnIndex(id[3]);
int indexDate = smsCursor.getColumnIndex(id[4]);
if (indexMsg < 0 || !smsCursor.moveToFirst())
return;
mCustomAdapter.clear();
while (smsCursor.moveToNext())
{
// Retrieve string version of data
String strContactID = smsCursor.getString(indexContactID);
String strDisplayName = smsCursor.getString(indexDisplayName);
String strContactNumber = smsCursor.getString(indexPhoneNumber);
String strMsg = smsCursor.getString(indexMsg);
String strDate = smsCursor.getString(indexDate);
String[] textMessage = new String[]
{
strContactID, strDisplayName, strContactNumber,
strMsg, strDate
};
// Place collected data into custom adapter
mCustomAdapter.add(textMessage);
}
smsCursor.close();
Who told you "person" is the contact's display-name?
According to the docs:
PERSON
The ID of the sender of the conversation, if present.
This is actually a "sender-id" this is never a contact-id, as an sms can be sent from a contact, a non-contact phone number, a non-phone-number (like GOOGLE or FACEBOOK) and from a group containing two or more of the above types.
If you'd like to get check if this is a contact, and if it is, get the contact's name, you need to pass two steps:
Use the canonical-address table to translate between id and phone-number (address). You can copy and use Android's helper class for this RecipientIdCache
In case the address is a single phone number, use the PHONE_LOOKUP table to look for a contact with that number, this will help: https://stackoverflow.com/a/7967182/819355
I am creating an app in which I want to show my app icon in front of every contact if contact is associated with my app like in WhatsApp. I have searched a lot but didn't find any appropriate solution.
Fetch all the contacts from your phone like this.
public void getNumber(ContentResolver cr)
{
mItems = new ArrayList<String>();
Cursor phones = cr.query(Phone.CONTENT_URI, null, null, null, Phone.DISPLAY_NAME + " ASC");
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumberString = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumberString.replace(" ", "");
contactName.add(name);
contactNumber.add(phoneNumberString);
mItems.add(name);
}
phones.close();
}
now send all these contacts to your server, compare each mobile number in your server database, and list only the contacts that are found in your server.
You need to maintain your own database, when registering user, save his number and while comparing, compare the fetched number to the register member's number in your own database
When i ask for some field on the contact i need to ask each field alone
for example if i need to ask the id and the name i need to write it
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID ));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
But in this case i need to write long code that make an access to each of the contact field and check if this field is fill up.
Is there some way to avoid this and get all the contact info at once ?
try something like
for (String colName:cur.getColumnNames())
{
String a = colName;
String b = cur.getString(cur.getColumnIndex(colName));
//now you have the name of the column in variable a, and the data in variable b
}
Can somebody help me on how to get all contacts per account? Meaning, I want to put a condition which will determine if the contact is from the phone (created by user) or from google and some other sync sources because as of now I was getting all contacts and its the combination of all sync sources e.g. local contacts, google or even yahoo contacts ?
Can somebody help me on how to get all contacts per account?
You can use next snippet to retrieve contacts for a particular account type:
String where = RawContacts.ACCOUNT_TYPE+ "=?";
String[] args = { accountType };
Cursor contacts = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, where, args, null);
int numberIndex = contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int displayNameIndex = contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
for (contacts.moveToFirst(); !contacts.isAfterLast(); contacts.moveToNext()) {
String number = contacts.getString(numberIndex);
String displayName = contacts.getString(displayNameIndex);
// do something with account contacts
}
contacts.close();
To filter plain phone contacts (not connected to any account) you can use:
String where = RawContacts.ACCOUNT_TYPE+ " IS NULL";
I'm trying to get a random contact from a phone's contact list, but without noticeably slowing down the phone. That means that I can't just grab all the contacts and stick them into an array and pick a random one from that array. I'd like to be able to get a random contact without having to get all of the contacts first.
Is this possible, and if so, how would I go about doing it?
Updated to use non-deprecated code. Query based on this answer: How to read contacts on Android 2.0
Cursor managedCursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
Then it's just a matter of getting the size of the Cursor:
int size = managedCursor.getCount();
get a random one, read it and check if it has phone numbers. If not, choose another one:
boolean found = false;
Random rnd = new Random();
while(!found) {
int index = rnd.nextInt(size);
managedCursor.moveToPosition(index);
String name = managedCursor.getString(people.getColumnIndex(PhoneLookup.DISPLAY_NAME));
found = Boolean.parseBoolean(managedCursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (found) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.d("Phone found:", phoneNumber);
}
phones.close();
}
}
I don't see how you could pick a random one otherwise. And this should not slow the phone down unless it's a really large contacts list.
Now it checks for the presence of phone numbers and reads all of them if found. If not, it chooses another entry.