1、Working With Android Contacts Introduction To Android ContactsLearn to work with the Android contacts database. Basic knowledge of accessing SQLite in Android along with using Cursors is expected. See theAndroid SQLite and Cursor Articlefor more information. Google changed the contacts database movi
2、ng from 1.x to 2.0 versions of Android. This tutorial will be broken into 3 sections. First covering accessing contacts in Android 2.0. The second page will deal with accessing the contacts in Android 1.6 and before. Third well glue it all together with a class that abstracts specific classes for ea
3、ch version and a set of classes to manage the data from the contact records.Create a new project called TestContacts in Eclipse setup for Android 2.0.Android Contact API For 2.0Granting AccessBefore an application can query the contact records access must be granted through the AndroidManifest.xml f
4、ile stored in the root of the project. Add the following uses-permission belows the uses-sdk statement. Querying The Android Contact DatabaseRetrieving Contact DetailsBasic contact information stored in Contacts table with detailed information stored in individual tables for normalization. In Androi
5、d 2.0 to query the base contact records the URI to query is stored in ContactsContract.Contacts.CONTENT_URI.package higherpass.TestContacts;import android.app.Activity;import android.content.ContentResolver;import android.database.Cursor;import android.os.Bundle;import android.provider.ContactsContr
6、act;public class TestContacts extends Activity /* Called when the activity is first created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContrac
7、t.Contacts.CONTENT_URI, null, null, null, null); if (cur.getCount() 0) while (cur.moveToNext() String id = cur.getString( cur.getColumnIndex(ContactsContract.Contacts._ID); String name = cur.getString( cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); if (Integer.parseInt(cur.getString(cur
8、.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER) 0) /Query phone here. Covered next This application starts off as any other Android application. First create a ContentResolver isntance in cr. Then use the ContentResolver instance to query the database and return a Cursor with the contact
9、s list. The query is perofrmed against the URI stored in ContactsContract.Contacts.CONTENT_URI. Next check if the cursor contains records and if so loop through them. The record ID field is stored in the id variable. This will be used as a where parameter later. Also the display name field is stored
10、 in the string name. For more details about working with cursors seeAndroid Cursors Tutorial.Phone NumbersPhone 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 ContactsContract.CommonDataKinds.Phone.CONTE
11、NT_URI. Use a WHERE conditional to get the phone numbers for the specified contact. if (Integer.parseInt(cur.getString( cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER) 0) Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds
12、.Phone.CONTACT_ID + = ?, new Stringid, null); while (pCur.moveToNext() / Do something with phones pCur.close(); Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact
13、 ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.Email AddressesQuerying email addresses is similar to phone numbers. A query must be performed to get email addresses from the database. Query the URI stored
14、in ContactsContract.CommonDataKinds.Email.CONTENT_URI to query the email address table. Cursor emailCur = cr.query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + = ?, new Stringid, null); while (emailCur.moveToNext() / This would allow
15、you get several email addresses / if the email addresses were stored in an array String email = emailCur.getString( emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA); String emailType = emailCur.getString( emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE); email
16、Cur.close();As with the phone query the field names for the email table are also stored under ContactsContract.CommonDataKinds. The email query is performed on the URI in ContactsContract.CommonDataKinds.Email.CONTENT_URI and the WHERE clause has to match the ContactsContract.CommonDataKinds.Email.C
17、ONTACT_ID field. Since multiple email addresses can be stored loop through the records returned in the Cursor.NotesCustom notes can be attached to each contact record. As before these are stored in a separate table and are related based on the contact ID. String noteWhere = ContactsContract.Data.CON
18、TACT_ID + = ? AND + ContactsContract.Data.MIMETYPE + = ?; String noteWhereParams = new Stringid, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE; Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null); if (noteCur.moveToFirst() String note = note
19、Cur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE); noteCur.close();Notes are stored in the Android Contacts generic data table. When accessing specific data the WHERE clause will need 2 conditionals. First the standard contact ID, second a MIMETYPE for the data that is
20、 being requested. The Android SDK comes with a series of auto-generated variables that take care of this. Use the ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE variable to limit the query to note records. The data table URI is stored at ContactsContract.Data.CONTENT_URI. Finally the note f
21、ield name is stored in ContactsContract.CommonDataKinds.Note.NOTE.Postal AddressesAndroid can store multiple postal addresses per contact. Addresses are also stored in the data table like notes and queried via the URI stored in ContactsContract.Data.CONTENT_URI. Similar to the notes query a MIMETYPE
22、 must be added to the WHERE conditional. Also in Android 2.0 the Address record was split into multiple fields containing different parts of the address (PO-Box, stree, city, region, postal code). In earlier versions of the Android SDK this was a free-form string storage. String addrWhere = Contacts
23、Contract.Data.CONTACT_ID + = ? AND + ContactsContract.Data.MIMETYPE + = ?; String addrWhereParams = new Stringid, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE; Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI, null, where, whereParameters, null); while(addrCur.moveT
24、oNext() String poBox = addrCur.getString( addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX); String street = addrCur.getString( addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET); String city = addrCur.getString( addrCur.getColumnIndex(Cont
25、actsContract.CommonDataKinds.StructuredPostal.CITY); String state = addrCur.getString( addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION); String postalCode = addrCur.getString( addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE); String c
26、ountry = addrCur.getString( addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY); String type = addrCur.getString( addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE); addrCur.close();This code is similar to the previous example. Notice the fie
27、ld names for the address pieces are stored in ContactsContract.CommonDataKinds.StructuredPostal.Instant Messenger (IM)The instant messenger query performs just as the notes and address queries. Important field names for IM related data are stored in ContactsContract.CommonDataKinds.Im. String imWher
28、e = ContactsContract.Data.CONTACT_ID + = ? AND + ContactsContract.Data.MIMETYPE + = ?; String imWhereParams = new Stringid, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE; Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI, null, imWhere, imWhereParams, null); if (imCur.moveToFirst() S
29、tring imName = imCur.getString( imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA); String imType; imType = imCur.getString( imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE); imCur.close();OrganizationsThe last part of the contact record to be covered is the Organizations d
30、ata. 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 ContactsContract.Data.CONTENT_URI. Important field names for the organization data are stored in ContactsContra
31、ct.CommonDataKinds.Organization. String orgWhere = ContactsContract.Data.CONTACT_ID + = ? AND + ContactsContract.Data.MIMETYPE + = ?; String orgWhereParams = new Stringid, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE; Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI, null, orgWhere, orgWhereParams, null); if (orgCur.moveToFirst() String orgName = orgCur.getString(orgCur.getColumn
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1