1、Content Provider是Android提供的一个供Android多个应用程序数据共享的技术

2、Andriod系统自带联系人(Contacts)软件,如何在自定义系统中调用Contacts中的联系人,以及如何处理一个联系人下面的多个号码


main.xml:仅显示按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click Me"></Button>
</LinearLayout>

Java代码

package yyl.contentprovider;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ContentProviderDemo extends Activity {

//定义变量
private Button button = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//根据控件Id得到控件对象
button = (Button) findViewById(R.id.button);
//给按钮控件添加单击事件监听器
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
//调用显示联系人方法
printContacts();

}
});
}

/*
* 自定义显示Contacts提供的联系人的方法
*/
public void printContacts() {
//生成ContentResolver对象
ContentResolver contentResolver = getContentResolver();

// 获得所有的联系人
/*Cursor cursor = contentResolver.query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
*/
//这段代码和上面代码是等价的,使用两种方式获得联系人的Uri
Cursor cursor = contentResolver.query(Uri.parse("content://com.android.contacts/contacts"),null,null,null,null);

// 循环遍历
if (cursor.moveToFirst()) {

int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
int displayNameColumn = cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

do {
// 获得联系人的ID
String contactId = cursor.getString(idColumn);
// 获得联系人姓名
String displayName = cursor.getString(displayNameColumn);

//使用Toast技术显示获得的联系人信息
Toast.makeText(ContentProviderDemo.this, "联系人姓名:" + displayName,Toast.LENGTH_LONG).show();


// 查看联系人有多少个号码,如果没有号码,返回0
int phoneCount = cursor
.getInt(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

if (phoneCount > 0) {
// 获得联系人的电话号码列表
Cursor phoneCursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ "=" + contactId, null, null);
if(phoneCursor.moveToFirst())
{
do
{
//遍历所有的联系人下面所有的电话号码
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//使用Toast技术显示获得的号码
Toast.makeText(ContentProviderDemo.this, "联系人电话:"+phoneNumber,Toast.LENGTH_LONG).show();

}while(phoneCursor.moveToNext());
}
}


} while (cursor.moveToNext());
}

}
}

更多相关文章

  1. 【android】TextView属性大全
  2. Android-View-Attribute
  3. android imageView详解
  4. android的上下文菜单---context menu
  5. Android(安卓)DatePickerDialog 只显示年月
  6. 详解android:scaleType属性
  7. android ---spannableStringBuilder
  8. Day1.5--Android简介之初识Activity
  9. Android本地的Content Provider

随机推荐

  1. Android多点触控
  2. Fuchsia OS 要取代 Android?小论Google Fu
  3. Android(安卓)源码分析-Dalvik 虚拟机创
  4. Android使用JNI实现Java与C之间传递数据
  5. Android二维码扫描之ZXing快速项目集成
  6. Android(安卓)onTouchEvent, onClick及on
  7. Android(安卓)创建与解析XML(一)—— 概述
  8. Android 头像上传
  9. 最强 Android(安卓)Studio 使用小技巧和
  10. Android(安卓)5.0为了安全而“关门”