如何通过ContactsProvider获取联系人信息

avatar
作者
筋斗云
阅读量:0

要通过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();     } } 

现在,您可以根据需要处理这些联系人信息,例如将其显示在列表视图中或将其发送到服务器。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!