Use ContentProvider Get Contacts

本文介绍了一种通过ContentProvider接口获取Android设备上联系人信息的方法,并提供了完整的Java实现代码及应用配置。此方法适用于需要读取用户联系人信息的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UseContentProviderGetContactsActivity .java

package gogler.myandroid;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;

import android.os.Bundle;

import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.widget.TextView;

public class UseContentProviderGetContactsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
String string = "contacts:\n";

String number = "";
/* 1、获取contentResolver */
ContentResolver cr = this.getContentResolver();
/* 2、获取一个游标查询对象 */
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
/* 3、遍历contentResolver */
while (cursor.moveToNext()) {
/*
* 获取联系人名字索引
*/
int nameFieldColumnIndex = cursor
.getColumnIndex(PhoneLookup.DISPLAY_NAME);
/* 获取联系人名 */
String contact = cursor.getString(nameFieldColumnIndex);
/* 4、新建游标 */
Cursor target = this.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null,
null);
/* 5、定位游标到第一行 */
target.moveToFirst();
/* 6、得到联系人的ID */
String contentId = cursor.getString(target
.getColumnIndex(ContactsContract.Contacts._ID));
/* 7、得到当前联系人有没有手机号码的值 */
String isPnone = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
/* 8、如果当前联系人有电话号码 */
if (Integer.parseInt(isPnone) > 0) {
/* 9、新建游标-联系人ID */
Cursor phone = this.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
+ contentId, null, null);
/* 10、遍历得到当前联系人的多个手机号码 */
while (phone.moveToNext()) {
// 得到联系人电话
number = phone
.getString(phone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
string += (contact + ":" + number + "\n");
}
}
}
tv.setText(string);
setContentView(tv);

}
}

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gogler.myandroid"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="13" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>

<application
android:icon="@drawable/russia"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".UseContentProviderGetContactsActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

代码打包下载地址

http://download.youkuaiyun.com/download/gogler/3725437

演示截图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值