如何使用电话簿中的联系人发送短信而不是写电话号码?

发布于 2024-12-12 22:30:10 字数 93 浏览 2 评论 0原文

我通过写一个人的电话号码向他发送短信,但现在我想添加通过选择发件人的联系人从电话簿联系人中自动获取号码的功能。任何人都可以告诉我如何在我的应用程序中添加该功能吗?提前致谢。

I'm sending sms to a person by writing his phone number but now I want to add functionality of getting number automatically from phone book contacts by selecting sender's contact.Can anyone give me some idea how to add that functionality in my application ? Thanks in avance.

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

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

发布评论

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

评论(2

在巴黎塔顶看东京樱花 2024-12-19 22:30:10

这可以通过以下方式完成:

  1. 从电话簿加载联系人(单击按钮或单击用于收集电话号码的EditText

//单击时选择联系人

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);
    switch (reqCode) {
        case (1) :
            getContactInfo(data);
            edittext.setText(contactName); // Set text on your EditText
            break;
    }
}
  1. 实际获取所有联系人的方法

当用户单击 EditText 时,这将加载所有联系人。

public void getContactInfo(Intent intent) {
    String phoneNumber = null;
    String name = null;

    Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
    while (cursor.moveToNext()) {
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

        if (hasPhone.equalsIgnoreCase("1"))
            hasPhone = "true";
        else
            hasPhone = "false";

        if (Boolean.parseBoolean(hasPhone)) {
            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
            while (phones.moveToNext()) {
                phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            }// end while
            phones.close();
        }// end if
    }// end while
    return arr;
}// end class

This can be done as follows:

  1. Load contacts from your phonebook (on a button click or on clicking on the EditText you use to collect phone number)

// Pick contacts when clicked

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);
    switch (reqCode) {
        case (1) :
            getContactInfo(data);
            edittext.setText(contactName); // Set text on your EditText
            break;
    }
}
  1. Method that actually gets all the contacts

This will load all the contacts when the user clicks the EditText.

public void getContactInfo(Intent intent) {
    String phoneNumber = null;
    String name = null;

    Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
    while (cursor.moveToNext()) {
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

        if (hasPhone.equalsIgnoreCase("1"))
            hasPhone = "true";
        else
            hasPhone = "false";

        if (Boolean.parseBoolean(hasPhone)) {
            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
            while (phones.moveToNext()) {
                phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            }// end while
            phones.close();
        }// end if
    }// end while
    return arr;
}// end class
肤浅与狂妄 2024-12-19 22:30:10

我认为您首先需要通过 Contacts API 访问存储在手机上的联系人。然后从原始联系人中检索电话号码并使用它发送短信。

I think you first need to access the contacts stored on the phone via the Contacts API. Then retrieve a phone number from a contact raw and use it to send your sms.

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