狂刷Android范例之5:读取手机通讯录
说明
狂刷Android范例系列文章开张了。每篇学习一个Android范例,将一个范例单独生成一个可运行的app,并对重点源代码进行简要分析。然后提供打包好的源代码下载。
功能
提供完整代码,通过ContenResolver,读取手机通讯录的内容。
代码包在此,无需下载分:
http://download.csdn.net/detail/logicteamleader/8806135
来源
例子来自于Android-20的com.example.android.apis.content.PickContact。
环境
代码运行环境:
1.ADT2014版本;
2.android:minSdkVersion=”8”;android:targetSdkVersion=”20”
3.workspace中已经生成了appcompatv7,它的版本是android-22;
代码
本范例有两个重要的技术点:
第一个知识点,通过设置Intent的Action和Type,使用隐式方式启动相关的app,来读取手机上的内容。这里使用的Action是Intent.ACTION_GET_CONTENT,意思就是要读取手机上的各种内容。这里的Type有四个,分别是:
1. ContactsContract.Contacts.CONTENT_ITEM_TYPE
2. “vnd.android.cursor.item/person”
3. ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
4. ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE
其真实值分别是:
1. “vnd.android.cursor.item/contact”
2. “vnd.android.cursor.item/person”
3. “vnd.android.cursor.item/phone_v2”
4. “vnd.android.cursor.item/postal-address_v2”
代表了通讯录、人、手机、地址四种内容的Type。
根据Intent.ACTION_GET_CONTENT和这四种Type的组合,启动Intent就可获取这四种内容,并在void onActivityResult(int requestCode, int resultCode, Intent data)函数中通过data变量返回。
第二个知识点,通过ContentResolver从返回的Intent中查询所需的内容。使用的方法是Cursor android.content.ContentResolver.query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder),其参数含义如下:
1. uri是指查询所需的uri,其schema必须是content://;
2. projection是查询结果项的数组,如果输入null则返回所有的项;
3. selection是条件语句,如同sql语言中的where子句;
4. selectionArgs,是条件语句的真实值数组;
5. sortOrder是排序参数,如同SQL语言中的order by子句。
查询返回结果是一个Cursor,然后通过getInt或者getString等方法从Cursor中取出所需的值。
本例子中仅取出了ID和Name两个值,要获取其他值,请设置projection参数,并从Cursor中读出。
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.pickcontact;
// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/**
* Demonstrates launching the contacts app to pick a contact. Does not require
* permission to read contacts, as that permission will be granted when the
* selected contact is returned.
*/
public class PickContact extends Activity {
Toast mToast;
ResultDisplayer mPendingResult;
class ResultDisplayer implements OnClickListener {
String mMsg;
String mMimeType;
ResultDisplayer(String msg, String mimeType) {
mMsg = msg;
mMimeType = mimeType;
}
public void onClick(View v) {
// 通过设置Intent的action为ACTION_GET_CONTENT,同时设置Type为mMimeType
// 启动手机上可读取通讯录的app,读取通讯录内容
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(mMimeType);
mPendingResult = this;
startActivityForResult(intent, 1);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pick_contact);
// Watch for button clicks.
((Button) findViewById(R.id.pick_contact))
.setOnClickListener(new ResultDisplayer("Selected contact",
ContactsContract.Contacts.CONTENT_ITEM_TYPE));
((Button) findViewById(R.id.pick_person))
.setOnClickListener(new ResultDisplayer("Selected person",
"vnd.android.cursor.item/person"));
((Button) findViewById(R.id.pick_phone))
.setOnClickListener(new ResultDisplayer(
"Selected phone",
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE));
((Button) findViewById(R.id.pick_address))
.setOnClickListener(new ResultDisplayer(
"Selected address",
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
//若要获取更多信息,请查询并设置projection参数
String[] projection = new String[] { BaseColumns._ID, Phone.DISPLAY_NAME};
c = getContentResolver().query(uri, projection, null, null,
null);
if (c != null && c.moveToFirst()) {
int id = c.getInt(0);
String name = c.getString(1);
if (mToast != null) {
mToast.cancel();
}
String txt = mPendingResult.mMsg + ":\n" + uri
+ "\nid: " + id+"\nname:"+name;
mToast = Toast.makeText(this, txt, Toast.LENGTH_LONG);
mToast.show();
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}
}