Android Contacts的使用(二)

來源:互聯網
上載者:User
接Android Contacts的使用(一)API For 1.6 and Before  1.6之前的版本

If you just read the begining of this article the first bit of this page is going to look familiar. This page is designed to act as a standalone guide to working with the contacts API in Android 1.6 and before. 假如你剛剛閱讀了該文的開頭是否有種時曾相識的感覺。該文主要是用來介紹1.6以下版本連絡人的API。

Granting Access 授予許可權

Before an application can query the contact records access must be granted through the AndroidManifest.xml file stored in the root of the project. Add the following uses-permission belows the uses-sdk statement. 同Android Contacts的使用(一)

 <uses-permission android:name="android.permission.READ_CONTACTS" />
 Querying the contact database 連絡人資料庫查詢

 

Retrieving Contact Details 擷取連絡方式

Basic contact information stored in Contacts table with detailed information stored in individual tables for normalization. In Android 1.x to query the base contact records the URI to query is stored in People.CONTENT_URI. 基本的連絡人資訊儲存在連絡人表中,而詳細資料儲存在個人表中。在 Android1.x 中查詢的連絡人記錄資料庫的URI是People.CONTENT_URI。

package com.test;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;

public class TestContacts extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(People.CONTENT_URI, null, null, null, null);
if(cur.getCount()>0){
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(People._ID));
String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));
}
}
}
}

 Start off with the standard view loading. Then we create a ContentResolver instance that will be used to query the SQLite database that stores the contacts. The ContentResolver query returns a Cursor instance that holds the contact records queried from the database. Then take the ID field from the contact record and store it in the string id and take the DISPLAY_NAME field and place it in the string name. For more information about cursors see the Android Cursor Tutorial.  參考Android Contacts的使用(一)

Phone Numbers 電話號碼

Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable Contacts.Phones.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact. 同Android Contact的使用(一),查詢的URI換成Contacts.Phones.CONTENT_URI。

if (cur.getInt(cur.getColumnIndex(People.PRIMARY_PHONE_ID)) > 0) {
Cursor pCur = cr.query(Contacts.Phones.CONTENT_URI,null,
Contacts.Phones.PERSON_ID +" = ?",new String[]{id}, null);
int i=0;
int pCount = pCur.getCount();
String[] phoneNum = new String[pCount];
String[] phoneType = new String[pCount];
while (pCur.moveToNext()) {
phoneNum[i] = pCur.getString(pCur.getColumnIndex(Contacts.Phones.NUMBER));
phoneType[i] = pCur.getString(pCur.getColumnIndex(Contacts.Phones.TYPE));
i++;
}
}

Query the phones table and get a Cursor stored in pCur. Since the Android contacts database can store multiple phone numbers per contact we need to loop through the returned results. In addition to returning the phone number the query also returned the type of number (home, work, mobile, etc). 查詢電話表,返回一個遊標pCur。由於Android中每個連絡人可以儲存多個電話號碼,我們需要遍曆返回的結果。查詢除了返回電話號碼外還返回了其他(家庭,工作,手機等)類型。

 Email Addresses 郵件地址

Querying email addresses is similar to phone numbers. A special query must be performed to get email addresses from the database. Query the URI stored in Contacts.ContactMethods.CONTENT_EMAIL_URI to query the email addresses.  同Android Contact的使用(一),查詢的URI換成Contacts.ContactMethods.CONTENT_EMAIL_URI。

Cursor emailCur = cr.query(Contacts.ContactMethods.CONTENT_EMAIL_URI,null,
Contacts.ContactMethods.PERSON_ID + " = ?",new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
}
emailCur.close();

Simple query Contacts.ContactMethods.CONTENT_EMAIL_URI with a conditional limiting the results to numbers that match the ID of the contact record matches the value in the field Contacts.ContactMethods.PERSON_ID. As with phone numbers each contact can contain multiple email addresses so we need to loop through the Cursor records. 查詢Contacts.ContactMethods.CONTENT_EMAIL_URI,以連絡人記錄的ID為條件匹配Contacts.ContactMethods.PERSON_ID的值。正如電話號碼每個連絡人也可以有多個email地址,同樣的我們也需要遍曆返回的結果。

Notes 注釋

Custom notes can be attached to each contact record. Notes though are stored in the main contact record and are simply accessed through the data stored in People.NOTES. 可以為每個連絡人附加自訂注釋。注釋儲存在連絡人記錄中,並通過People.NOTES儲存的資料進行訪問。

String notes=cur.getString(cur.getColumnIndex(People.NOTES));
Postal Addresses 郵政地址

Android can store multiple postal addresses per contact. Addresses are stored in the contact methods table and need to have a second conditional added to retrieve the data. Add a conditional Contacts.ContactMethods.KIND that matches Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE to only query postal addresses from Contacts.ContactMethods.CONTENT_URI. 每個連絡人都可以儲存多個郵政地址。地址儲存在連絡方式表中,要擷取資料,需要有次要條件。添加適配Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE的條件Contacts.ContactMethods.KIND來訪問Contacts.ContactMethods.CONTENT_URI傳遞過來的地址。

String addrWhere = Contacts.ContactMethods.PERSON_ID 
+ " = ? AND " + Contacts.ContactMethods.KIND + " = ?";
String[] addrWhereParams = new String[]{id,
Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE};
Cursor addrCur = cr.query(Contacts.ContactMethods.CONTENT_URI,
null, addrWhere, addrWhereParams, null);
while(addrCur.moveToNext()) {
String addr = addrCur.getString(
addrCur.getColumnIndex(Contacts.ContactMethodsColumns.DATA));
String type = addrCur.getString(
addrCur.getColumnIndex(Contacts.ContactMethodsColumns.TYPE));
}
addrCur.close();

Query Contacts.ContactMethods.CONTENT_URI with 2 conditionals, one limiting the contact ID and the second Contacts.ContactMethods.KIND matching Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE to only query postall addresses. Android can store multiple postal addresses so loop through the list of returned results. Android also stores a type record for the address. In Android 1.6 and before the address is stored as a free-form string containing the data. In Android 2.0 and later this has changed to being a series of fields containing parts of the address. 查詢Contacts.ContactMethods.CONTENT_URI需要2個條件,一個是連絡人的ID另一個是通過Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE來匹配Contacts.ContactMethods.KIND查詢postal地址。Android可以通過迴圈來遍曆返回的結果清單中的多個postal地址,他也儲存了地址類型的記錄。在Android1.6或更早的版本中,該地址是由一個自由格式的字串儲存,在2.0或更高的版本中,已經更改為多個小地址儲存。

Instant Messenger (IM)  立即訊息

The instant messenger query works just like the previous 2. The data is queried from Contacts.ContactMethods.CONTENT_URI and needs conditionals for the contact ID and Contacts.ContactMethods.KIND matching Contacts.ContactMethods.CONTENT_IM_ITEM_TYPE. 立即訊息查詢類似於postal地址查詢,查詢這些資料也需要連絡人的Id和Contacts.ContactMethods.CONTENT_IM_ITEM_TYPE來匹配Contacts.ContactMethods.KIND為條件。

String imWhere = Contacts.ContactMethods.PERSON_ID 
+ " = ? AND " + Contacts.ContactMethods.KIND + " = ?";
String[] imWhereParams = new String[]{id,
Contacts.ContactMethods.CONTENT_IM_ITEM_TYPE};
Cursor imCur = cr.query(Contacts.ContactMethods.CONTENT_URI,
null, imWhere, imWhereParams, null);
if (imCur.moveToFirst()) {
String imName = imCur.getString(
imCur.getColumnIndex(Contacts.ContactMethodsColumns.DATA));
String imType = imCur.getString(
imCur.getColumnIndex(Contacts.ContactMethodsColumns.TYPE));
}
imCur.close();
 Organizations 組織

The last part of the contact record to be covered is the Organizations data. The Android contact record can contain information about Employment, professional, and social memberships as well as roles and titles. These records are queried from the URI stored in Contacts.Organizations.CONTENT_URI. 連絡人記錄的最後一部分資料群組織資料。在Android中聯絡記錄可以包含有關就業、職業資訊、社會成員以及角色和職稱等資訊。這些記錄需從Contacts.Organizations.CONTENT_URI儲存的URI查詢。

String orgWhere = Contacts.ContactMethods.PERSON_ID + " = ?"; 
String[] orgWhereParams = new String[]{id};
Cursor orgCur = cr.query(Contacts.Organizations.CONTENT_URI,
null, orgWhere, orgWhereParams, null);
if (orgCur.moveToFirst()) {
String orgName = orgCur.getString(
orgCur.getColumnIndex(Contacts.Organizations.COMPANY));
String title = orgCur.getString(
orgCur.getColumnIndex(Contacts.Organizations.TITLE));
}
orgCur.close();

<未完待續>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.