Use of Android Contacts (2)

Source: Internet
Author: User
Using Android Contacts (I) 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. if you have just read the beginning of this article, do you feel familiar with each other. This document describes the APIs for contacts of versions earlier than 1.6.

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. same as Android Contacts (1)

 <uses-permission android:name="android.permission.READ_CONTACTS" />
Querying the contact database query

 

Retrieving Contact Details get Contact information

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. the basic contact information is stored in the contact table, and the detailed information is stored in the personal table. The URI of the Contact Record Database queried in Android1.x is 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. see Use of Android Contacts (1)

Phone Numbers Phone number

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. use the same as Android Contact (1). Change the URI to 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 ). query the telephone table and return a cursor pCur. Since each contact in Android can store multiple phone numbers, we need to traverse the returned results. Besides the phone number, other types (home, work, mobile phone, etc.) are returned for the query.

Email Address

Querying email addresses is similar to phone numbers. A special query must be passed med to get email addresses from the database. query the URI stored in Contacts. contactMethods. CONTENT_EMAIL_URI to query the email addresses. use the same as Android Contact (1). Change the URI to 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. query Contacts. contactMethods. CONTENT_EMAIL_URI, matching Contacts with the Contact Record ID as the condition. con The value of tactMethods. PERSON_ID. Just as each contact of a telephone number can have multiple email addresses, we also need to traverse the returned results.

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. you can add custom comments to each contact. Annotations are stored in the contact record and accessed through the data stored in 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. each contact can store multiple postal addresses. The address is stored in the contact information table. secondary conditions are required for obtaining data. Add the Contacts. ContactMethods. CONTENT_POSTAL_ITEM_TYPE condition Contacts. ContactMethods. KIND to access the address passed by 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 addr Ess 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. query Contacts. contactMethods. CONTENT_URI requires two conditions. One is the contact's ID and the other is through Contacts. contactMethods. CONTENT_POSTAL_ITEM_TYPE to match Contacts. contactMethods. KIND query postal address. Android can traverse multiple postal addresses in the returned result list through loops, which also stores address-type records. In Android1.6 or earlier versions, this address is stored by a free-Format String. In version 2.0 or later, it has been changed to multiple small address storage.

Instant Messenger (IM) Instant Messaging

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. the instant message query is similar to the postal address query. to query the data, you also need the contact Id and Contacts. contactMethods. CONTENT_IM_ITEM_TYPE to match Contacts. contactMethods. KIND is a condition.

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 Organization

The last part of the contact record to be covered is the Organizations data. the Android contact record can contain information about employee, professional, and social memberships as well as roles and titles. these records are queried from the URI stored in Contacts. organizations. CONTENT_URI. organize data in the last part of the contact record. In Android, the contact record can contain information about employment, occupation, social members, roles and titles. These records must be queried from the URI stored in Contacts. Organizations. CONTENT_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();

<To be continued>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.