从android中的联系人中检索电话号码

And*_*hul 2 android android-manifest

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

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();
Run Code Online (Sandbox Code Playgroud)

Dha*_*ant 8

使用下面的代码,你肯定会得到一个电话号码.

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(); 
Run Code Online (Sandbox Code Playgroud)