【问题标题】:How to send sms using contacts in phonebook instead of writing the phone number?如何使用电话簿中的联系人发送短信而不是写电话号码?
【发布时间】:2011-12-19 10:04:15
【问题描述】:

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

【问题讨论】:

    标签: android sms android-contacts


    【解决方案1】:

    这可以按如下方式完成:

    1. 从您的电话簿加载联系人(点击button 或点击您用来收集电话号码的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
    

    【讨论】:

    • return arrarr 未定义且函数没有返回类型。这是什么?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-02-16
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 2015-07-20
      相关资源
      最近更新 更多