只有获取联系人的手机号码 [英] Get Contacts mobile number only

查看:133
本文介绍了只有获取联系人的手机号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前已在SO关于使用联系人API,但我想知道是否有一种方法来识别检索到的号码是手机号码获取联系人号码是几个问题。

There have been several questions on SO regarding getting Contacts numbers using the Contacts API but I'd like to know if there is a way to identify that the number retrieved is a mobile number.

下面code常表现为一种方式来获得一个联系人的电话号码,因为它得到的一个或多个电话号码的清单:

The following code is often shown as a way to get a Contact's phone numbers, as it gets a list of one or more phone numbers:

String[] projection = {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER};
    String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1";

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

            phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
            while (phones.moveToNext()) 
            {               
                String pdata = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
                Log.v("DATA",pdata);
            }                           
       }  
    }
    catch (NullPointerException npe)
    {
        Log.e(TAG, "Error trying to get Contacts.");
    }
    finally
    {
        if (phones != null)
        {
            phones.close();
        }
        if (cursor != null)
        {
            cursor.close();
        }           
    } 

虽然,这个工程还行,有没有什么办法可以轻松地识别该电话号码是移动式(除了试图用正则表达式匹配)。

Whilst, this works okay, is there any way to easily identify that the phone number is a mobile type (apart from trying to pattern match with Regex).

我怀疑必须有一个相关的数据块,从而使本机应用程序进行分类的电话号码 - 如在下面的图片:

I suspect there must be a related piece of data, so that native apps can classify the phone number - as in the picture below:

推荐答案

我偶然发现了一个博客文章,其中给出了pretty的的使用ContactsContract API <一个很好的解释href="http://app-solut.com/blog/2011/03/working-with-the-contactscontract-to-query-contacts-in-android/">here.

I stumbled upon a blog article which gives a pretty good explanation of using the ContactsContract api here.

所以,在我上面的例子中,我改变了我的code部分上面这样的:

So, in my example above, I changed part of my code above to this:

while (phones.moveToNext()) 
{                   
     int phoneType = phones.getInt(phones.getColumnIndex(Phone.TYPE));
     if (phoneType == Phone.TYPE_MOBILE)
     {
          phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
          phoneNumber = phoneNumber.replaceAll("\\s", "");
          break;
     }
}

这个循环周围所有的手机为个别接触和类型是Phone.TYPE_MOBILE那么它需要这个。

This loops around all the phones for an individual contact and if the type is Phone.TYPE_MOBILE then it takes this one.

希望这有助于有人用同样的问题我有。

Hope this helps someone with the same issue I had.

这篇关于只有获取联系人的手机号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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