0

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.

1 Answers1

0

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

Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
  • Thank you for your answer. in your example you still use activity i want to know if there is a way to not use it so i can make my library in a jar file ?? – user2451237 Aug 13 '13 at 18:01
  • The way I posted it is the way you would implement it in a library. You always need a Context for this but the solution to your problem is that your method needs a Context parameter, and everybody who uses your library would then supply a Context, for example an Activity, to your method. – Xaver Kapeller Aug 13 '13 at 18:22