Android联系人数据库全解析(2)
Android联系人数据库全解析(2)
Android 1.6以及以前的版本API
本节的开头跟上一节的开头很相似。本节所要介绍的内容将是独立的介绍1.6以及以前版本的API
权限的授予
在操作联系人记录之前,你必须在AndroidManifest.xml中声明权限。这样你才能被授权查看联系人。增加下述权限:
<uses-permission android:name="android.permission.READ_CONTACTS" />
查询联系人数据库
查询联系人详细信息
为了规范信息,基本的联系人信息放在联系人表里,而其他的详细信息则被储存在独立的数据表中。在Android1.x中,要查询基本的联系人记录,URI被指定在People.CONTENT_URI变量中。
package higherpass.TestContacts;import android.app.Activity;import android.content.ContentResolver;import android.database.Cursor;import android.os.Bundle;import android.provider.Contacts;import android.provider.Contacts.People;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(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)); } } }}if (Integer.parseInt(cur.getString( 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++;} }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();cur.getString(cur.getColumnIndex(People.NOTES));
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();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();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(); 1 楼 jspjson 2012-05-10 先研究研究。。