android(2.0以后版本)中读取联系人和通话记录

scdqs 贡献于2012-04-06

作者 ZYB  创建于2011-02-17 00:27:14   修改者  修改于1899-12-30 00:00:00字数9593

文档摘要:android(2.0以后版本)中读取联系人和通话记录
关键词:

android(2.0以后版本) 中读取联系人和通话记录 linux 2010-08-09 23:32:13 阅读937 评论2 字号:大中小 实例1: android 中获取联系人 ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while(cursor.moveToNext()){ //get name int nameFiledColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME); String contact = cursor.getString(nameFiledColumnIndex); String[] PHONES_PROJECTION = new String[] { "_id","display_name","data1","data3"};// String contactId = cursor.getString(cursor.getColumnIndex(PhoneLookup._ID)); Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PHONES_PROJECTION, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null); //name type .. while(phone.moveToNext()) { int i = phone.getInt(0); String str = phone.getString(1); str = phone.getString(2); str = phone.getString(3); } phone.close(); //addr Cursor addrCur = cr.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI , new String[]{"_id","data1","data2","data3"}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId , null, null); while(addrCur.moveToNext()) { int i = addrCur.getInt(0); String str = addrCur.getString(1); str = addrCur.getString(2); str = addrCur.getString(3); } addrCur.close(); //email Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI , new String[]{"_id","data1","data2","data3"}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId , null, null); while(emailCur.moveToNext()) { int i = emailCur.getInt(0); String str = emailCur.getString(1); str = emailCur.getString(2); str = emailCur.getString(3); } emailCur.close(); } cursor.close(); android中获取通话记录 String str = ""; int type; long callTime; Date date; String time= ""; ContentResolver cr = getContentResolver(); final Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls.NUMBER,CallLog.Calls.CACHED_NAME,CallLog.Calls.TYPE, CallLog.Calls.DATE}, null, null,CallLog.Calls.DEFAULT_SORT_ORDER); for (int i = 0; i < cursor.getCount(); i++) { cursor.moveToPosition(i); str = cursor.getString(0); str = cursor.getString(1); type = cursor.getInt(2); SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); date = new Date(Long.parseLong(cursor.getString(3))); time = sfd.format(date); } 实例2: Andorid1.5及其以前的项目放到Android2.0上时,如果代码中有 import android.provider.Contacts; Eclipse会提示“建议不使用”,那是因为在Android2.0中,联系人api发生了变化,需要使用ContactsContract。 直接看下面一个最简单的例子,读取联系人的姓名和电话号码: 读取联系人的名字很简单,但是在读取电话号码时,就需要先去的联系人的ID,然后在通过ID去查找电话号码!一个联系人可能存在多个电话号码! //得到ContentResolver对象 ContentResolver cr = getContentResolver(); //取得电话本中开始一项的光标 Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { // 取得联系人名字 int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME); String name = cursor.getString(nameFieldColumnIndex); string += (name); // 取得联系人ID String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); // 取得电话号码(可能存在多个号码) while (phone.moveToNext()) { String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); string += (":" + strPhoneNumber); } string += "\n"; phone.close(); } cursor.close(); 实例3: import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class PhoneBook extends Activity { /*声明四个UI变量与一个常数作为Activity接收回传值用*/ private TextView mTextView01; private Button mButton01; private EditText mEditText01; private EditText mEditText02; private static final int PICK_CONTACT_SUBACTIVITY = 2; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /*透过findViewById建构子来建构一个TextView,两个EditText,一个Button对象**/ mTextView01 = (TextView)findViewById(R.id.myTextView1); mEditText01 = (EditText)findViewById(R.id.myEditText01); mEditText02 = (EditText)findViewById(R.id.myEditText02); mButton01 = (Button)findViewById(R.id.myButton1); /*设定onClickListener 让使用者点选Button时搜寻联系人*/ mButton01.setOnClickListener(new Button.OnClickListener() { // @Override public void onClick(View v) { // TODO Auto-generated method stub /*建构Uri来取得联络人的资源位置*/ // Uri uri = Uri.parse("content://contacts/people/"); /*透过Intent来取得联络人数据并回传所选的值*/ //Intent intent = new Intent(Intent.ACTION_PICK, uri); /*开启新的Activity并期望该Activity回传值*/ //startActivityForResult(intent, PICK_CONTACT_SUBACTIVITY); startActivityForResult ( new Intent(Intent.ACTION_PICK, android.provider.ContactsContract.Contacts.CONTENT_URI), PICK_CONTACT_SUBACTIVITY); } }); } @Override protected void onActivityResult (int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub try { switch (requestCode) { case PICK_CONTACT_SUBACTIVITY: final Uri uriRet = data.getData(); if(uriRet != null) { try { /* 必须要有android.permission.READ_CONTACTS权限 */ Cursor c = managedQuery(uriRet, null, null, null, null); /*将Cursor移到资料最前端*/ c.moveToFirst(); /*取得联络人的姓名*/ String strName = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); /*将姓名写入EditText01中*/ mEditText01.setText(strName); /*取得联络人的电话*/ int contactId = c.getInt(c.getColumnIndex(ContactsContract.Contacts._ID)); Cursor phones = getContentResolver().query ( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null ); StringBuffer sb = new StringBuffer(); int typePhone, resType; String numPhone; if (phones.getCount() > 0) { phones.moveToFirst(); /* 2.0可以允许User设定多组电话号码,但本范例只捞一组电话号码作示范 */ typePhone = phones.getInt ( phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE) ); numPhone = phones.getString ( phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER) ); resType = ContactsContract.CommonDataKinds.Phone.getTypeLabelResource(typePhone); sb.append(getString(resType) +": "+ numPhone +"\n"); /*将电话写入EditText02中*/ mEditText02.setText(numPhone); } else { sb.append("no Phone number found"); } /*Toast是否读取到完整的电话种类与电话号码*/ Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show(); } catch(Exception e) { /*将错误信息在TextView中显示*/ mTextView01.setText(e.toString()); e.printStackTrace(); } } break; default: break; } } catch(Exception e) { e.printStackTrace(); } super.onActivityResult(requestCode, resultCode, data); } } ------------------------- string.xml PhoneBook 搜索 我的联系人 姓名 电话号码 ----------------------------- main.xml ------------------------------------ AndroidManifest.xml 注意在后要加上 实例4: package com.activity; import android.app.Activity; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.Contacts; import android.provider.Contacts.People; import android.widget.TextView; public class Main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.TextView01); String columns[] = new String[] { People._ID, People.NAME, People.NUMBER, People.PRIMARY_EMAIL_ID, People.PRIMARY_ORGANIZATION_ID, People.PRIMARY_PHONE_ID, People.DISPLAY_NAME, People.IM_ACCOUNT, People.IM_HANDLE, People.PHONETIC_NAME, People.TYPE }; Uri mContacts = People.CONTENT_URI; Cursor cur = managedQuery(mContacts, columns, // 要返回的数据字段 null, // WHERE子句 null, // WHERE 子句的参数 People.NAME // Order-by子句 ); if (cur.moveToFirst()) { Cursor newcur = null; do { // 获取字段的值 String name = cur.getString(cur.getColumnIndex(People.NAME)); String phoneNo = cur.getString(cur .getColumnIndex(People.NUMBER)); String peopleId = cur.getString(cur.getColumnIndex(People._ID)); String[] PROJECTION = new String[] { Contacts.ContactMethods._ID, Contacts.ContactMethods.KIND, Contacts.ContactMethods.DATA }; newcur = managedQuery(Contacts.ContactMethods.CONTENT_URI, PROJECTION, Contacts.ContactMethods.PERSON_ID + "=\'" + cur.getLong(cur.getColumnIndex(People._ID)) + "\'", null, null); startManagingCursor(newcur); String email = ""; if (newcur.moveToFirst()) { while (newcur.moveToNext()) { email = email+ newcur.getString(newcur.getColumnIndex(Contacts.ContactMethods.DATA)); } } tv.setText("name = " + name + " phoneNo = " + phoneNo + "email = " + email); if (email != null && !"".equals(email) && email.trim().length() != 0) { // 此处可以取到联系人邮件 } } while (cur.moveToNext()); if (newcur != null) { newcur.close();// 用完得关闭吧 } } if (cur != null) cur.close(); // 用完得关闭吧 } }

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 8 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档