android - 确定所选联系人的号码是手机还是固定电话?

标签 android android-contacts contacts contactscontract

我正在使用此代码让用户选择要向其发送短信的联系人:

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);

...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == PICK_CONTACT){
        if(resultCode == RESULT_OK){
            Uri contactData = data.getData();
            Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
            if(cursor != null) {
                cursor.moveToFirst();
                String hasPhone = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                if (hasPhone.equals("1")) {
                    Cursor phones = getContentResolver().query
                            (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                            + " = " + contactId, null, null);
                    while (phones != null && phones.moveToNext()) {
                        number = phones.getString(phones.getColumnIndex
                                (ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceAll("[-() ]", "");
                    }
                    if(phones!=null) phones.close();

                } else {
                    Toast.makeText(getApplicationContext(), "This contact has no phone number", Toast.LENGTH_LONG).show();
                }
                cursor.close();

            }

        }
    }

有没有办法在用户选择联系人时读取电话号码类型,然后根据电话号码的类型采取行动?例如,如果有手机号,则将其作为字符串返回,如果有固定电话,则什么也不做,如果两者都有,则只返回手机号?

最佳答案

是的,您可以使用列TYPE 来确定电话号码的类型。

在您的 while 循环中添加一个 if 语句来检查电话号码的类型:

                while (phones != null && phones.moveToNext()) {
                    int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                    if(type != ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
                        // It's not a cellphone number, just skip
                        continue;
                    }
                    number = phones.getString(phones.getColumnIndex
                            (ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceAll("[-() ]", "");
                }

关于android - 确定所选联系人的号码是手机还是固定电话?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34125819/

相关文章:

android - 调整工具栏不透明度

android - 如何实现联系人连接(mimetype)来启动我的应用程序?

java - 当x和y位置进入另一个位置时如何执行操作?

ios - 在同一列表/表格 View 中显示缓存的服务器联系人和本地 iOS 联系人

java - 尝试访问 android 应用程序中的联系人不断崩溃

c++ - 通过 Box2d 中的 NSNotificationCenter 发送 C++ 类对象。错误 : No viable conversion from 'MyContact' to 'id'

android - CircleCI Build 中的自动化测试失败并出现异常

Android:在通话/SIP 期间播放声音文件或文字转语音

android - 使用 Retrofit2 和 rxJava2 处理没有主体的响应

java - 从 Android 中的 StartActivityForResult 返回一个类