ImageVerifierCode 换一换
格式:DOCX , 页数:36 ,大小:25.04KB ,
资源ID:11535446      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11535446.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Working With Android Contacts.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Working With Android Contacts.docx

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