本文介绍了获取联系人的手机,工作和家庭数量在一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要得到的联系人(保存在电话本)的手机号码,工作号码和家庭号码。我想在我3的EditText观点来设置这些号码。如何做到这一点?这里是我的code

 光标手机= getActivity()。getContentResolver()查询(
        Phone.CONTENT_URI,
        空值,
        Phone.CONTACT_ID +=+ phoneId,
        空值,
        空值
);
而(phones.moveToNext()){
    数= phones.getString(phones.getColumnIndex(Phone.NUMBER));
    整型= phones.getInt(phones.getColumnIndex(Phone.TYPE));
    如果(类型== Phone.TYPE_HOME){
        数= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
        返回数;
    }
    如果(类型== Phone.TYPE_MOBILE){
        数= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
        返回数;
    }
    如果(类型== Phone.TYPE_WORK){
        数= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
        返回数;
    }
}


解决方案

尝试this链接从电话簿中获得的接触细节,如PHONENUMBER,名称。
并检查张贴的答案乔恩

 公开名单<&人GT; getContactList(){
    ArrayList的<&人GT; contactList =新的ArrayList<&人GT;();    乌里contactUri = ContactsContract.Contacts.CONTENT_URI;
    的String [] =投影新的String [] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.HAS_PHONE_NUMBER,
    };
    串选择= ContactsContract.Contacts.HAS_PHONE_NUMBER +='1';
    光标接触= getContentResolver()查询(ContactsContract.Contacts.CONTENT_URI,投影,选择,NULL,NULL);
    如果(contacts.getCount()大于0)
    {
        而(contacts.moveToNext()){
            人aContact =新的Person();
            INT idFieldColumnIndex = 0;
            INT nameFieldColumnIndex = 0;
            INT numberFieldColumnIndex = 0;            字符串的ContactID = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts._ID));            nameFieldColumnIndex = contacts.getColumnIndex(PhoneLookup.DISPLAY_NAME);
            如果(nameFieldColumnIndex -1个)
            {
                aContact.setName(contacts.getString(nameFieldColumnIndex));
            }            投影=新的String [] {} Phone.NUMBER;
            最后光标手机= managedQuery(Phone.CONTENT_URI,投影,Data.CONTACT_ID +=?,新的String [] {将String.valueOf(的ContactID)},NULL);
            如果(phone.moveToFirst()){
                而(!phone.isAfterLast())
                {
                    numberFieldColumnIndex = phone.getColumnIndex(Phone.NUMBER);
                    如果(numberFieldColumnIndex -1个)
                    {
                        aContact.setPhoneNum(phone.getString(numberFieldColumnIndex));
                        phone.moveToNext();
                        TelephonyManager mTelephonyMgr;
                        mTelephonyMgr =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                        如果(!mTelephonyMgr.getLine1Number()。含有(aContact.getPhoneNum()))
                        {
                            contactList.add(aContact);
                        }
                    }
                }
            }
            phone.close();
        }        contacts.close();
    }    返回contactList;
}

并Person类

 公共类Person {
    字符串MYNAME =;
    字符串为mynumber =;    公共字符串的getName(){
        返回MYNAME;
    }    公共无效setname可以(字符串名称){
        MYNAME =名称;
    }    公共字符串getPhoneNum(){
        返回mynumber的;
    }    公共无效setPhoneNum(串号){
        为mynumber =号;
    }
}

希望这可以帮助你。

I want to get the contact's(Saved in phonebook) mobile number, work number and home number. I want to set these numbers in my 3 edittext views. How to do this? Here is my code

Cursor phones = getActivity().getContentResolver().query(
        Phone.CONTENT_URI, 
        null, 
        Phone.CONTACT_ID + " = " + phoneId, 
        null, 
        null
);
while (phones.moveToNext()) {
    number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
    int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
    if (type == Phone.TYPE_HOME) {
        number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
        return number;
    }
    if (type == Phone.TYPE_MOBILE) {
        number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
        return number;
    }
    if (type == Phone.TYPE_WORK) {
        number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
        return number;
    }
}
解决方案

try this link to get the contact details like phonenumber,name from phonebook.And check the answer posted by jon

public List<Person> getContactList(){
    ArrayList<Person> contactList = new ArrayList<Person>();

    Uri contactUri = ContactsContract.Contacts.CONTENT_URI;
    String[] PROJECTION = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.HAS_PHONE_NUMBER,
    };
    String SELECTION = ContactsContract.Contacts.HAS_PHONE_NUMBER + "='1'";
    Cursor contacts = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, SELECTION, null, null);


    if (contacts.getCount() > 0)
    {
        while(contacts.moveToNext()) {
            Person aContact = new Person();
            int idFieldColumnIndex = 0;
            int nameFieldColumnIndex = 0;
            int numberFieldColumnIndex = 0;

            String contactId = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts._ID));

            nameFieldColumnIndex = contacts.getColumnIndex(PhoneLookup.DISPLAY_NAME);
            if (nameFieldColumnIndex > -1)
            {
                aContact.setName(contacts.getString(nameFieldColumnIndex));
            }

            PROJECTION = new String[] {Phone.NUMBER};
            final Cursor phone = managedQuery(Phone.CONTENT_URI, PROJECTION, Data.CONTACT_ID + "=?", new String[]{String.valueOf(contactId)}, null);
            if(phone.moveToFirst()) {
                while(!phone.isAfterLast())
                {
                    numberFieldColumnIndex = phone.getColumnIndex(Phone.NUMBER);
                    if (numberFieldColumnIndex > -1)
                    {
                        aContact.setPhoneNum(phone.getString(numberFieldColumnIndex));
                        phone.moveToNext();
                        TelephonyManager mTelephonyMgr;
                        mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                        if (!mTelephonyMgr.getLine1Number().contains(aContact.getPhoneNum()))
                        {
                            contactList.add(aContact);  
                        }
                    }
                }
            }
            phone.close();
        }

        contacts.close();
    }

    return contactList;
}

AND PERSON CLASS

public class Person {
    String myName = "";
    String myNumber = "";

    public String getName() {
        return myName;
    }

    public void setName(String name) {
        myName = name;
    }    

    public String getPhoneNum() {
        return myNumber;
    }

    public void setPhoneNum(String number) {
        myNumber = number;
    }
}

hope this helps you.

这篇关于获取联系人的手机,工作和家庭数量在一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 06:42