从Android中的联系人检索电话号码 [英] Retrieve phone number from contacts in android

查看:140
本文介绍了从Android中的联系人检索电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从联系人处获取电话号码,但是光标没有进入此循环.请帮帮我....

I need to get a phone number from contacts but the cursor is not entering in this loop. Please help me out....

for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor.moveToNext()) {
// Get a phone number
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

Toast.makeText(this, "Phone  =  "+phoneNumber, Toast.LENGTH_LONG).show();

推荐答案

使用下面的代码,您肯定会获得电话号码.

use below code you will definitely get a phone no.

ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);
        if (cursor.moveToFirst()) {
            String contactId =
                cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            //
            //  Get all phone numbers.
            //
            Cursor phones = cr.query(Phone.CONTENT_URI, null,
                Phone.CONTACT_ID + " = " + contactId, null, null);
            while (phones.moveToNext()) {
                String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
                int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
                switch (type) {
                    case Phone.TYPE_HOME:
                       TextView textView=(TextView)findViewById(R.id.textView1);
                       textView.setText(number);
                        break;
                    case Phone.TYPE_MOBILE:
                        // do something with the Mobile number here...
                        break;
                    case Phone.TYPE_WORK:
                        // do something with the Work number here...
                        break;
                    }
            }
            phones.close(); 

这篇关于从Android中的联系人检索电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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