将电话簿获取到应用程序

发布于 2024-11-01 10:53:26 字数 942 浏览 0 评论 0原文

我编写了以下代码来获取联系方式。问题是我收到的电话号码显示为空。你能帮助我吗?

private void displayRecords() {
    // An array specifying which columns to return.

    String columns[] = new String[] { People.NAME, People.NUMBER_KEY};
    Uri mContacts = People.CONTENT_URI;
    Cursor cur = managedQuery(mContacts, columns, // Which columns to return
            null, // WHERE clause; which rows to return(all rows)
            null, // WHERE clause selection arguments (none)
            null // Order-by clause (ascending by name)

    );
    if (cur.moveToFirst()) {
        String name = null;
        String phoneNo = null ;
        do {
            // Get the field values
            name = cur.getString(cur.getColumnIndex(People.NAME));
            phoneNo =cur.getString(cur.getColumnIndex(People.NUMBER_KEY));
            Toast.makeText(this, name + " " + phoneNo, Toast.LENGTH_LONG).show();
        } while (cur.moveToNext());
    }
}

I wrote the following code to get the contact details. The problem is the phone number I am getting is showing null. Can You help me?

private void displayRecords() {
    // An array specifying which columns to return.

    String columns[] = new String[] { People.NAME, People.NUMBER_KEY};
    Uri mContacts = People.CONTENT_URI;
    Cursor cur = managedQuery(mContacts, columns, // Which columns to return
            null, // WHERE clause; which rows to return(all rows)
            null, // WHERE clause selection arguments (none)
            null // Order-by clause (ascending by name)

    );
    if (cur.moveToFirst()) {
        String name = null;
        String phoneNo = null ;
        do {
            // Get the field values
            name = cur.getString(cur.getColumnIndex(People.NAME));
            phoneNo =cur.getString(cur.getColumnIndex(People.NUMBER_KEY));
            Toast.makeText(this, name + " " + phoneNo, Toast.LENGTH_LONG).show();
        } while (cur.moveToNext());
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何其悲哀 2024-11-08 10:53:26

试试这个代码,它会对你有帮助

  people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        int position=0;
        Cursor q=db.query(mProfile,new String[]{"person_name"},"person_name"+"!='"+null+"'",null,null, null, null);
        people.moveToFirst();
            int nameFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);    
            while(!people.isAfterLast()) {
                Cursor o=db.query(mProfile,new String[]{"person_name"},"person_name"+"='"+people.getString(nameFieldColumnIndex)+"'",null,null, null, null);
                if(!(o.getCount()>0))
                {  
                mConname.add(position, people.getString(nameFieldColumnIndex));
                try{
                    String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
                    String hasPhone = people.getString(people.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()) 
                        {
                            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            mConno.add(position,phoneNumber);

                        }
                        phones.close(); 
                    }   
                    if(hasPhone=="false")
                    {   mConname.remove(position);
                    }   
                    else
                        position++;
                }       
                catch(Exception e)
                { 

                }
            }
                people.moveToNext();
            }

try this code it will helpful for u

  people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        int position=0;
        Cursor q=db.query(mProfile,new String[]{"person_name"},"person_name"+"!='"+null+"'",null,null, null, null);
        people.moveToFirst();
            int nameFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);    
            while(!people.isAfterLast()) {
                Cursor o=db.query(mProfile,new String[]{"person_name"},"person_name"+"='"+people.getString(nameFieldColumnIndex)+"'",null,null, null, null);
                if(!(o.getCount()>0))
                {  
                mConname.add(position, people.getString(nameFieldColumnIndex));
                try{
                    String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
                    String hasPhone = people.getString(people.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()) 
                        {
                            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            mConno.add(position,phoneNumber);

                        }
                        phones.close(); 
                    }   
                    if(hasPhone=="false")
                    {   mConname.remove(position);
                    }   
                    else
                        position++;
                }       
                catch(Exception e)
                { 

                }
            }
                people.moveToNext();
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文