中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

如何通過ContactsProvider獲取聯系人信息

小樊
85
2024-08-30 22:43:35
欄目: 編程語言

要通過ContactsProvider獲取聯系人信息,您需要遵循以下步驟:

  1. 首先,確保您的AndroidManifest.xml文件中包含讀取聯系人的權限。添加以下權限:
<uses-permission android:name="android.permission.READ_CONTACTS" />
  1. 在運行時請求權限(如果您的應用針對Android 6.0或更高版本)。在您的活動或片段中添加以下代碼:
private static final int REQUEST_CODE = 1;

private void requestContactsPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CODE);
    } else {
        // Permission already granted, continue with your operation.
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == REQUEST_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission granted, continue with your operation.
        } else {
            // Permission denied, inform the user.
        }
    }
}
  1. 使用ContentResolver查詢聯系人數據。以下是一個示例方法,用于獲取所有聯系人的姓名和電話號碼:
private void getContacts() {
    ContentResolver contentResolver = getContentResolver();
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    if (cursor != null && cursor.moveToFirst()) {
        do {
            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            // Get phone numbers
            Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{contactId}, null);

            if (phoneCursor != null && phoneCursor.moveToFirst()) {
                do {
                    String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // Process the phone number, such as adding it to a list or displaying it in a TextView.
                } while (phoneCursor.moveToNext());
                phoneCursor.close();
            }

            // Get email addresses
            Cursor emailCursor = contentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{contactId}, null);

            if (emailCursor != null && emailCursor.moveToFirst()) {
                do {
                    String emailAddress = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
                    // Process the email address, such as adding it to a list or displaying it in a TextView.
                } while (emailCursor.moveToNext());
                emailCursor.close();
            }

        } while (cursor.moveToNext());
        cursor.close();
    }
}

現在,您可以根據需要處理這些聯系人信息,例如將其顯示在列表視圖中或將其發送到服務器。

0
突泉县| 休宁县| 安福县| 茌平县| 东源县| 珲春市| 新河县| 化州市| 永德县| 上栗县| 百色市| 巴楚县| 缙云县| 巴塘县| 玉树县| 大城县| 平顶山市| 博爱县| 温泉县| 武邑县| 洮南市| 隆子县| 从化市| 五指山市| 礼泉县| 叙永县| 威海市| 肥城市| 金坛市| 同江市| 乌鲁木齐县| 进贤县| 吉木乃县| 凤冈县| 柘荣县| 札达县| 济阳县| 琼中| 淮北市| 永靖县| 紫云|