I have an AutoCompleteTextView and i want it to autocomplete contact names on input. The problem is that Contacts.People has been deprecated. The new documentation says to use ContactsContract but i can't find any proper tutorial. Can anyone give me a sample code or point me to an appropriate tutorial.
Thanks.
Android Contact API For 2.0
Granting Access
Before an application can query the contact records access must be granted through the AndroidManifest.xml file stored in the root of the project. Add the following uses-permission belows the uses-sdk statement.
<uses-permission android:name="android.permission.READ_CONTACTS" />
Querying The Android Contact Database
Retrieving Contact Details
Basic contact information stored in Contacts table with detailed information stored in individual tables for normalization. In Android 2.0 to query the base contact records the URI to query is stored in ContactsContract.Contacts.CONTENT_URI.
package higherpass.TestContacts;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
public class TestContacts extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
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) {
//Query phone here. Covered next
}
}
}
}
}
This application starts off as any other Android application. First create a ContentResolver instance in cr. Then use the ContentResolver instance to query the database and return a Cursor with the contacts list. The query is performed against the URI stored in ContactsContract.Contacts.CONTENT_URI. Next check if the cursor contains records and if so loop through them. The record ID field is stored in the id variable. This will be used as a where parameter later. Also the display name field is stored in the string name.
Read more about Working With Android Contacts
Related
I need to retrieve all data for a single contact which was chosen by a user (Action.Pick). I want to use LOOKUP KEY as suggested by developer.android.com. Can I do it by a simple query without creating Loader etc. as suggested by the documentation ?
I wrote this method for my application:
public static Cursor getContactCursorByLookUpKey(Context context,String lookUpKey)
{
ContentResolver contentResolver = context.getContentResolver();
Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI,lookUpKey);
return contentResolver.query(lookupUri,null,null,null,null);
}
To get the data simply read data from the cursor. For example :
Cursor data = ContactManager.getContactCursorByLookUpKey(ContactDetailsActivity.this,lookUpKey);
String name = data.getString(data.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String photoPath = data.getString(data.getColumnIndex(ContactsContract.Contacts.PHOTO_URI));
You can query the Contacts provider using ContentResolver directly, if you have the READ_CONTACTS permission. You will receive a cursor with multiple rows of raw contact details (phones, emails, etc.). Iterate over the cursor to read them and don't forget to close the cursor.
I'm backing up and restoring contacts, I'm able to do it, but while aggregation of rawcontacts for linked contacts(joined contacts) using ContactsContract.AggregationExceptions
If i join 3 contacts, say Contact A (master) and Contact B (linked) and C (linked)
I do the backup ,and restore,for the newly created id's with correct mapping, I'm updating the ContactsContract.AggregationExceptions.CONTENT_URI with following code:
private boolean aggregateContactLinks() {
ContentResolver cr = context.getContentResolver();
int listCount = linkList.size();
ContentValues values = new ContentValues(3);
Log.i(tag, "aggregating contacts");
for (int i=0; i<listCount; i++) {
values.put(AggregationExceptions.RAW_CONTACT_ID1,
linkList.get(i).newId1);
values.put(AggregationExceptions.RAW_CONTACT_ID2,
linkList.get(i).newId2);
values.put(AggregationExceptions.TYPE,
AggregationExceptions.TYPE_KEEP_TOGETHER);
Log.i(tag," new master id(id1) :"+linkList.get(i).newId+"\nlinkid2 :"+newId2);
Log.i(tag,
"result :"
+ cr.update(AggregationExceptions.CONTENT_URI,
values, null, null));
Log.i(tag, "\nupdated one contact");
}
values.clear();
return true;
}
output , on phone i can see the linked content but in display name of contact is c instead of a
before backup
after backup, deleting the contacts and after restore
can anyone know where exactly im going wrong,thanks in advance
If you want to tell Android contact provider that it should use the specific raw contact name for aggregated contact (not display names of linked contacts B and C but the display name of master contact A), you may use RawContactsColumn.NAME_VERIFIED. Simply set it to "1" for your master raw contact after updating aggregation exceptions.
I found this trick in the standard Contacts application's
code:
// Mark the original contact as "name verified" to make sure that the contact
// display name does not change as a result of the join
if (verifiedNameRawContactId != -1) {
Builder builder = ContentProviderOperation.newUpdate(
ContentUris.withAppendedId(RawContacts.CONTENT_URI, verifiedNameRawContactId));
builder.withValue(RawContacts.NAME_VERIFIED, 1);
operations.add(builder.build());
}
RawContactsColumn interface is protected, so you should use "name_verified" as column name. Because of the fact that RawContactsColumn isn't open, maybe you should also check if "name_verified" column exists before updating to avoid crashes.
I am using android sample sync-adapter example code to sync my application's contacts database with the phone-book and I need to update my contacts database with the Contact_Id allotted to them as soon as they are inserted by the sync-adapter.
If you're using the ContentResolver.insert operation, the id is embedded in the returned URI. You can get it this way:
Uri resultUri = ContentResolver.insert(ContactsContract.RawContacts.CONTENT_URI, contentValues);
if (rawContactId != null) {
String rawContactId = String.valueOf(ContentUris.parseId(resultUri);
}
I am trying to get the selected contact number form the phone book when a button is clicked.Could any me please tell me how to get the contact number to the edit text.
Thanks in advance
// using Content Provider you can do it
There are some helper methods, particularly ContentUris.withAppendedId() and Uri.withAppendedPath(), that make it easy to append an ID to a URI. Both are static methods that return a Uri object with the ID added. So, for example, if you were looking for record 23 in the database of people contacts, you might construct a query as follows:
import android.provider.Contacts.People;
import android.content.ContentUris;
import android.net.Uri;
import android.database.Cursor;
// Use the ContentUris method to produce the base URI for the contact with _ID == 23.
Uri myPerson = ContentUris.withAppendedId(People.CONTENT_URI, 23);
// Alternatively, use the Uri method to produce the base URI.
// It takes a string rather than an integer.
Uri myPerson = Uri.withAppendedPath(People.CONTENT_URI, "23");
// Then query for this specific record:
Cursor cur = managedQuery(myPerson, null, null, null, null);
refer this
Is there an accepted/received way of checking to see if a person (phone number) is in the Contact List?
I'm hoping there's something I can call like this:
bool bInContactList = InContactList("1415922353");
It is not as simple as you want but you can query a Contacts content provider for the contact associated with a phone number:
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor c = getContentResolver().query(lookupUri, new String[] {PhoneLookup._ID}, null, null, null);
if (c.getCount() > 0) {
// there is some contact
} else {
// there is no contacts with phoneNumber
}
The application needs android.permission.READ_CONTACTS permission to access contacts data.
You can check Android Developer site for further references about content providers and android.providers package documentation for the list of available standard providers in Android.
I am afraid you did not provide enough information about the language you are talking about. If you use Java -for example- you have a contains method (common to all collections) so, if you want to know if a certain String is contained in a collection you could do it by invoking this method:
boolean found = someCollection.conmtains("1415922353");