Android Contacts(一)—— 读取联系人
来源:互联网 发布:java filereader类 编辑:程序博客网 时间:2024/05/17 22:56
Android Contacts(一)—— 读取联系人
Introduction To Android Contacts
Learn to work with the Android contacts database. Basic knowledge of accessing SQLite in Android along with using Cursors is expected. See the Android SQLite and Cursor Article for more information. Google changed the contacts database moving 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 we'll glue it all together with a class that abstracts specific classes for each version and a set of classes to manage the data from the contact records.
Contacts 读取代码:
- package com.homer.phone;
- import java.util.ArrayList;
- import java.util.HashMap;
- import android.app.Activity;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.provider.ContactsContract;
- import android.provider.ContactsContract.CommonDataKinds.Phone;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- public class phoneRead extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- showListView();
- }
- private void showListView(){
- ListView listView = new ListView(this);
- ArrayList<HashMap<String, String>> list = getPeopleInPhone2();
- SimpleAdapter adapter = new SimpleAdapter(
- this,
- list,
- android.R.layout.simple_list_item_2,
- new String[] {"peopleName", "phoneNum"},
- new int[]{android.R.id.text1, android.R.id.text2}
- );
- listView.setAdapter(adapter);
- setContentView(listView);
- }
- private ArrayList<HashMap<String, String>> getPeopleInPhone2(){
- ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
- Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); // 获取手机联系人
- while (cursor.moveToNext()) {
- HashMap<String, String> map = new HashMap<String, String>();
- int indexPeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME); // people name
- int indexPhoneNum = cursor.getColumnIndex(Phone.NUMBER); // phone number
- String strPeopleName = cursor.getString(indexPeopleName);
- String strPhoneNum = cursor.getString(indexPhoneNum);
- map.put("peopleName", strPeopleName);
- map.put("phoneNum", strPhoneNum);
- list.add(map);
- }
- if(!cursor.isClosed()){
- cursor.close();
- cursor = null;
- }
- return list;
- }
- }
AndroidManifest.xml 权限
记得在AndroidManifest.xml中加入android.permission.READ_CONTACTS这个permission
<uses-permission android:name="android.permission.READ_CONTACTS" />
运行结果:
代码示例
Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联
Android 的SMS读取短信,可以获取发信人/收信人的手机号码(address),Contacts的联系人,可以过滤手机号码(address),因此SMS可以通过手机号码(address)关联到Contacts联系人
SMS - Contacts 关联代码
- // 通过address手机号关联Contacts联系人的显示名字
- private String getPeopleNameFromPerson(String address){
- if(address == null || address == ""){
- return "( no address )\n";
- }
- String strPerson = "null";
- String[] projection = new String[] {Phone.DISPLAY_NAME, Phone.NUMBER};
- Uri uri_Person = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, address); // address 手机号过滤
- Cursor cursor = getContentResolver().query(uri_Person, projection, null, null, null);
- if(cursor.moveToFirst()){
- int index_PeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME);
- String strPeopleName = cursor.getString(index_PeopleName);
- strPerson = strPeopleName;
- }
- cursor.close();
- return strPerson;
- }
- package com.homer.phone;
- import java.sql.Date;
- import java.text.SimpleDateFormat;
- import android.app.Activity;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteException;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.ContactsContract;
- import android.provider.ContactsContract.CommonDataKinds.Phone;
- import android.util.Log;
- import android.widget.ScrollView;
- import android.widget.TextView;
- public class phoneRead2 extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- TextView tv = new TextView(this);
- tv.setText(getSmsInPhone());
- ScrollView sv = new ScrollView(this);
- sv.addView(tv);
- setContentView(sv);
- }
- public String getSmsInPhone() {
- final String SMS_URI_ALL = "content://sms/";
- final String SMS_URI_INBOX = "content://sms/inbox";
- final String SMS_URI_SEND = "content://sms/sent";
- final String SMS_URI_DRAFT = "content://sms/draft";
- final String SMS_URI_OUTBOX = "content://sms/outbox";
- final String SMS_URI_FAILED = "content://sms/failed";
- final String SMS_URI_QUEUED = "content://sms/queued";
- StringBuilder smsBuilder = new StringBuilder();
- try {
- Uri uri = Uri.parse(SMS_URI_ALL);
- String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
- Cursor cur = getContentResolver().query(uri, projection, null, null, "date desc"); // 获取手机内部短信
- if (cur.moveToFirst()) {
- int index_Address = cur.getColumnIndex("address");
- int index_Person = cur.getColumnIndex("person");
- int index_Body = cur.getColumnIndex("body");
- int index_Date = cur.getColumnIndex("date");
- int index_Type = cur.getColumnIndex("type");
- do {
- String strAddress = cur.getString(index_Address);
- int intPerson = cur.getInt(index_Person);
- String strbody = cur.getString(index_Body);
- long longDate = cur.getLong(index_Date);
- int intType = cur.getInt(index_Type);
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- Date d = new Date(longDate);
- String strDate = dateFormat.format(d);
- String strType = "";
- if (intType == 1) {
- strType = "接收";
- } else if (intType == 2) {
- strType = "发送";
- } else {
- strType = "null";
- }
- String strAddress2 = getPeopleNameFromPerson(strAddress);
- smsBuilder.append("[ ");
- // smsBuilder.append(strAddress + ", ");
- smsBuilder.append(strAddress + " : " + strAddress2 + ", ");
- smsBuilder.append(intPerson + ", ");
- smsBuilder.append(strbody + ", ");
- smsBuilder.append(strDate + ", ");
- smsBuilder.append(strType);
- smsBuilder.append(" ]\n\n");
- } while (cur.moveToNext());
- if (!cur.isClosed()) {
- cur.close();
- cur = null;
- }
- } else {
- smsBuilder.append("no result!");
- } // end if
- smsBuilder.append("getSmsInPhone has executed!");
- } catch (SQLiteException ex) {
- Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
- }
- return smsBuilder.toString();
- }
- // 通过address手机号关联Contacts联系人的显示名字
- private String getPeopleNameFromPerson(String address){
- if(address == null || address == ""){
- return "( no address )\n";
- }
- String strPerson = "null";
- String[] projection = new String[] {Phone.DISPLAY_NAME, Phone.NUMBER};
- Uri uri_Person = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, address); // address 手机号过滤
- Cursor cursor = getContentResolver().query(uri_Person, projection, null, null, null);
- if(cursor.moveToFirst()){
- int index_PeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME);
- String strPeopleName = cursor.getString(index_PeopleName);
- strPerson = strPeopleName;
- }
- cursor.close();
- return strPerson;
- }
- }
AndroidManifest.xml 权限
记得在AndroidManifest.xml中加入android.permission.READ_SMS和android.permission.READ_CONTACTS这两个permission
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
示例代码
- Android Contacts(一)—— 读取联系人
- Android Contacts(一)—— 读取联系人
- (转)Android Contacts(一)—— 读取联系人
- Android Contacts(一)—— 读取联系人
- Android Contacts(一)—— 读取联系人
- Android Contacts(一)—— 读取联系人
- Android Contacts(一)—— 读取联系人
- Android Contacts(一)—— 读取联系人
- Android Contacts(一)—— 读取联系人
- Android源码——通讯录联系人(一):Contacts数据库初识
- Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联
- Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联
- (转)Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联
- Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联
- Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联
- Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联
- Android 学习笔记 Contacts (二)Contacts 联系人详解
- Android 学习笔记 Contacts (二)Contacts 联系人详解
- linux ps 命令参数详解
- 推荐一款数据修复软件 - Roadkil's Unstoppable Copier
- pyqt4信号与槽之间参数传递
- Codejock: 如何添加一个docking pane到你的应用程序中
- 关于WII光枪定位的设计
- Android Contacts(一)—— 读取联系人
- grep参数
- PeopleSoft 学习笔记 全球薪资 第 06 章 变量元素
- PyQt4 QLineEdit 使用方法
- ASP.Net缓存总结
- Server at localhost was unable to start within 45 seconds
- PyQt4学习资料
- 论模式和非模式对话框
- 从CVPR2013看计算机视觉研究的三个趋势