如何从联系人和类中获取联系人的电子邮件和电话号码,没有扩展活动和 oncreate() 方法?

Posted

技术标签:

【中文标题】如何从联系人和类中获取联系人的电子邮件和电话号码,没有扩展活动和 oncreate() 方法?【英文标题】:How to get the email from contacts and phone numbers from contacts and class doesn,t have extends activity and oncreate() method? 【发布时间】:2011-09-15 05:39:37 【问题描述】:

我收到电话号码、来自联系人的邮件,但没有扩展活动和oncreate 方法。

通过使用以下代码:

 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
          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();


 

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

从获得结果中可以正常工作,但我不知道如何实现上述示例中的闲置代码:

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

如果有人知道,请帮助我。

【问题讨论】:

【参考方案1】:

您只需要访问 android.content.Context 对象即可访问 ContentResolver 并查询 ContentProviders。 Activity 扩展了 Context,这样就可以了。 android.app.Service 也扩展了 Activity,所以也可以。 android.app.Application 也扩展了 Context,所以它也可以。

【讨论】:

以上是关于如何从联系人和类中获取联系人的电子邮件和电话号码,没有扩展活动和 oncreate() 方法?的主要内容,如果未能解决你的问题,请参考以下文章

如何从联系人中获取电子邮件和姓氏?

如何从 Android 设备中仅获取电话号码联系人

从电话联系人中获取姓名、电话号码和电子邮件地址[关闭]

如何从联系人列表中获取电子邮件地址?

从 Android 联系人中获取电子邮件和电话

如何从具有电子邮件或电话或两者的Android手机中选择联系人[关闭]