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

发布于 2024-11-16 00:08:32 字数 4667 浏览 3 评论 0原文

我收到电话号码、来自联系人的邮件,但没有扩展活动和 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));

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小兔几 2024-11-23 00:08:32

您只需要访问 android.content.Context 对象即可访问 ContentResolver,从而查询 ContentProviders。 Activity 扩展了 Context,因此可以工作。 android.app.Service 还扩展了 Activity,因此也可以工作。 android.app.Application 还扩展了 Context,所以这也可以工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文