读取所有喜欢的联系人号码时出错

时间:2018-04-06 23:27:27

标签: android kotlin

我正在尝试阅读我最喜欢的所有联系人的号码,如下所示: 我有3个最喜欢的联系人,每个都有2个号码,所以我需要阅读6个号码,我的代码是:

import android.annotation.SuppressLint
import android.content.Context
import android.provider.ContactsContract
import android.provider.ContactsContract.Contacts.CONTENT_URI
import android.provider.ContactsContract.Contacts.HAS_PHONE_NUMBER
import android.util.Log
import android.view.View
import android.provider.ContactsContract.CommonDataKinds.Phone as Phone

@SuppressLint("MissingPermission")
fun getFavoriteContacts(context: Context, layout: View): MutableList<String> {
    var starredNumbers = mutableListOf<String>()
    val TAG = "Contacts";
    Log.i(TAG, "TAG: I'm here now")
        Log.i(TAG, "TAG: I'm here now to read contacts")
        val selection = ContactsContract.Contacts.STARRED + "='1'"
        val phoneContactID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID  + " = ?"

        val cursor = context.contentResolver.query(CONTENT_URI,
                null, selection, null, null)

        if (cursor.count == 0){Log.i(TAG, "TAG: I'm here now ZERO contacts fond")}

        while (cursor.moveToNext()) {
            Log.i(TAG, "TAG: I'm here now for the contacts")
            val phoneNumber = cursor.getString(
                    cursor.getColumnIndex(Phone.NUMBER))
            starredNumbers.add(phoneNumber)

               val hasPhoneNumber = Integer.parseInt(
                       cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)))
               if (hasPhoneNumber > 0) {

                   //This is to read multiple phone numbers associated with the same contact
                   val contactId = mutableListOf<String>()
                   val phoneCursor = context.contentResolver.query(Phone.CONTENT_URI,
                           null, phoneContactID, contactId.toTypedArray(), null)

                   while (phoneCursor.moveToNext()) {
                       val phoneNumber = phoneCursor.getString(
                               phoneCursor.getColumnIndex(Phone.NUMBER))
                       Log.i(TAG, "Number read $phoneNumber");
                       starredNumbers.add(phoneNumber)
                   }

                phoneCursor.close()
            }
        }
        cursor.close()

    return starredNumbers
}

但是我收到了这个错误:

enter image description here

1 个答案:

答案 0 :(得分:0)

我能够使用以下代码解决它:

import android.annotation.SuppressLint
import android.content.Context
import android.provider.ContactsContract
import android.provider.ContactsContract.Contacts.CONTENT_URI
import android.util.Log
import android.view.View
import android.provider.ContactsContract.CommonDataKinds.Phone as Phone

@SuppressLint("MissingPermission")
fun getFavoriteContacts(context: Context, layout: View): MutableList<String> {
    var contactID =  arrayOf("")
    lateinit var displayName : String
    lateinit var hasPhoneNumber : String
    var starredNumbers = mutableListOf<String>()
    val TAG = "Contacts";

    val selection = ContactsContract.Contacts.STARRED + "='1'"
    val projection = arrayOf(
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.STARRED,
            ContactsContract.Contacts.HAS_PHONE_NUMBER
    )

    val contactNumbers = arrayOf(Phone.NUMBER)

   val cursor = context.contentResolver.query(CONTENT_URI,
           projection, selection, null, null)

    when {
        cursor.count == 0 -> Log.i(TAG, "TAG: ZERO favorite contacts fond")
        else -> Log.i(TAG, "TAG: ${cursor.count} favorite contacts found")
    }

    while (cursor.moveToNext()) {
        contactID[0] = cursor.getString(cursor.getColumnIndex(
                ContactsContract.Contacts._ID))
        displayName = cursor.getString(cursor.getColumnIndex(
                ContactsContract.Contacts.DISPLAY_NAME))
       hasPhoneNumber = cursor.getString(cursor.getColumnIndex(
                ContactsContract.Contacts.HAS_PHONE_NUMBER))
        Log.i(TAG, "TAG: IFavourite contact read: $displayName   ")


        if (hasPhoneNumber.toInt() > 0) {
            val phoneCursor = context.contentResolver.query(
                    Phone.CONTENT_URI, contactNumbers, Phone.CONTACT_ID + "= ?",
                    contactID, null)

            while (phoneCursor.moveToNext()) {
                val phoneNumber = phoneCursor.getString(
                        phoneCursor.getColumnIndex(Phone.NUMBER))
                Log.i(TAG, "Number read is $phoneNumber");
                starredNumbers.add(phoneNumber)
            }

            phoneCursor.close()
        }
      }
        cursor.close()
    Log.i(TAG, "Numbers read are $starredNumbers")

    return starredNumbers
}