如何在使用电话簿的联系人,而不是写的电话号码发送短信? [英] How to send sms using contacts in phonebook instead of writing the phone number?

查看:161
本文介绍了如何在使用电话簿的联系人,而不是写的电话号码发送短信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写他的电话号码发送短信给一个人,但现在我想补充选择发件人的contact.Can任何人给我一些想法如何添加该功能在我的应用程序,从通讯录中的联系人自动获得多项功能?谢谢AVANCE。


解决方案

这是可以做到如下:


  1. 从电话簿负载触点(在按钮点击或点击的EditText 用来收集手机号)

//点击后选择联系人

  @覆盖
公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(REQ code,结果code,数据);
    开关(REQ code){
        情况1) :
            getContactInfo(数据);
            edittext.setText(联系人姓名); //你的EditText设置文本
            打破;
    }
}


  1. 方法实际获取的所有联系人

当用户点击这将加载所有接触的EditText

 公共无效getContactInfo(意向意图){
    字符串phoneNumber的= NULL;
    字符串名称= NULL;    光标光标= managedQuery(intent.getData(),NULL,NULL,NULL,NULL);
    而(cursor.moveToNext()){
        字符串的ContactID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        名称= cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));        串hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));        如果(hasPhone.equalsIgnoreCase(1))
            hasPhone =真;
        其他
            hasPhone =假;        如果(Boolean.parseBoolean(hasPhone)){
            光标手机= getContentResolver()查询(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,空,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=+的ContactID,NULL,NULL);
            而(phones.moveToNext()){
                phoneNumber的= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));            } //结束时
            phones.close();
        }// 万一
    } //结束时
    返回ARR;
} //结束类

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.

解决方案

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

这篇关于如何在使用电话簿的联系人,而不是写的电话号码发送短信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆