Question

Im developping an library that read,list,add and search contacts in the phone. I did it using Activity,cursor and managedQuery. But to have a complete indepandant library i have to do it with out using an activity. Is it possible to have a contact name per example just by this uri :"content://contacts/people/id" ? if yes can i have an example without using an activity ? Any help would be greatly appreciated. Many thanks.

Was it helpful?

Solution

I can give you a more accurate answer if you would post some code, but if you would implement a method in your library which would retrieve contacts, then you will alyways have to have a Context instance as parameter of this method.

Try something like this:

public Contact readContacts(Context context)
{
  ContentResolver resolver = context.getContentResolver();
  Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
  ....
}

You would then call this method in an app which uses your library like this:

ContactHelper.readContacts(activity);

or like this:

ContactHelper.readContacts(getApplicationContext());

etc...

I assume you already have all required permissions, but don't forget to add this or you will not be able to read contacts:

<uses-permission android:name="android.permission.READ_CONTACTS" />

Here you can find more information about reading contacts with several code examples: https://developer.android.com/training/contacts-provider/retrieve-names.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top