Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the email from contacts and phone numbers from contacts and class doesn,t have extends activity and oncreate() method?

I am getting phone no's, mails from contacts with out extends activity and oncreate method.

By using the fallowing code:

 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
          Class A extends Activity{

  new ClassB(this);
 }

 ////////////////////////////////////////////////////
 public static void getContactNumbers(Context context) {
    String contactNumber = null;
    int contactNumberType = Phone.TYPE_MOBILE;
    String nameOfContact = null;

        ContentResolver cr = context.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(BaseColumns._ID));
                nameOfContact = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                if (Integer
                        .parseInt(cur.getString(cur
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    Cursor phones = cr
                            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                    null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                            + " = ?", new String[] { id },
                                    null);

                    while (phones.moveToNext()) {
                        contactNumber = phones.getString(phones
                                .getColumnIndex(Phone.NUMBER));
                        contactNumberType = phones.getInt(phones
                                .getColumnIndex(Phone.TYPE));
                        Log.i(TAG, "...Contact Name ...." + nameOfContact
                                + "...contact Number..." + contactNumber);
                        ApplicationConstants.phoneContacts
                                .add(new ContactNumberBean(nameOfContact,
                                        contactNumber, contactNumberType));
                    }
                    phones.close();
                }

            }
        }// end of contact name cursor
        cur.close();

}

/**
 * 
 * This method is responsible to get native contacts and corresponding email
 * id (ApplicationConstants.emailContacts)
 * 
 * @param context
 */
public static void getContactEmails(Context context) {
    String emailIdOfContact = null;
    int emailType = Email.TYPE_WORK;
    String contactName = null;


        ContentResolver cr = context.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(BaseColumns._ID));
                contactName = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                // Log.i(TAG,"....contact name....." +
                // contactName);

                cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { id }, null);

                Cursor emails = cr.query(Email.CONTENT_URI, null,
                        Email.CONTACT_ID + " = " + id, null, null);
                while (emails.moveToNext()) {
                    emailIdOfContact = emails.getString(emails
                            .getColumnIndex(Email.DATA));
                    // Log.i(TAG,"...COntact Name ...."
                    // + contactName + "...contact Number..."
                    // + emailIdOfContact);
                    emailType = emails.getInt(emails
                            .getColumnIndex(Phone.TYPE));
                    ApplicationConstants.emailContacts
                            .add(new ContactEmailBean(contactName,
                                    emailIdOfContact, emailType));

                }
                emails.close();

            }
        }// end of contact name cursor
        cur.close();


} 

/////////////////////////////////////////

It is working fine from getting results but I don't know how to implement the fallowing code in the above example:

ApplicationConstants.phoneContacts
.add(new ContactNumberBean(nameOfContact,
contactNumber, contactNumberType));

If any one know this please help me.

like image 205
kiran Avatar asked Jun 15 '11 04:06

kiran


1 Answers

You only need access to an android.content.Context object to access the ContentResolver and thus query ContentProviders. Activity extends Context, so that works. android.app.Service also extends Activity, so that works too. android.app.Application also extends Context, so that will work too.

like image 172
michaelg Avatar answered Sep 19 '22 10:09

michaelg