|
前阵子搞短信,发现
Android 1.x至2.0 版本联系人数据库很多地方做了更改,且关于这方面的资料也比较少,所以找到一篇文章稍作翻译了下,以供大家参考,该文将分三部分发布。有翻译不对的地方还望见谅。本文多以俗语著称。最后,还是那句老话,谢绝转载!
Working With 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 < http://www.cnblogs.cc2/Android/Tutorials/Accessing-Data-With-Android-Cursors/> 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. 学习使用Android联系人数据库。要求懂得基本的SQLite的知识。可以查看 Android SQLite and Cursor Article相关文章以获取更多信息。从Android 1.x 至 2.0 版本谷歌改变了Android的联系人数据库。该手册主要分为三个部分:一是介绍2.0中访问名片夹;二是介绍1.6之前的版本;三我们综合了为每个版本给出一个抽象类和累积来管理名片记录数据。 Create a new project called TestContacts in Eclipse setup for Android 2.0. 首先在Eclipse中创建一个新项目TestContacts设置为Android 2.0。 Android Contact API For 2.0 Granting Access 授予权限 Before an application can query the contact records access must be granted through the AndroidManifest.xml file stored in the root of the project. Add the following uses-permission belows the uses-sdk statement. 在AndroidManifest.xml文件中授予以下权限 Querying The Android Contact Database 联系人数据库查询 Retrieving Contact Details 检索联系方式 Basic contact information stored in Contacts table with detailed information stored in individual tables for normalization. In Android 2.0 to query the base contact records the URI to query is stored in ContactsContract.Contacts.CONTENT_URI. 基本的个人信息存储在名片夹表,而详细的存储在个人表里。在Andoid2.0中查询相应联系记录的URI是ContactsContract.Contacts.CONTENT_URI。 package com.test; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.Contacts; public class TestContacts extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);//查询通讯录 if(cursor.getCount()>0){ while (cursor.moveToNext()) { String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));//联系人id String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));//联系人名称 if(cursor.getInt(cursor.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 contacts 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 in the string name. For more details about working with cursors see Android Cursors Tutorial < http://www.cnblogs.cc2/Android/Tutorials/Accessing-Data-With-Android-Cursors/>. ?启动该应用程序时需关闭任何其他 Android应用程序。首先,创建一个ContentResolver的实例cr。然后使用ContentResolver的实例查询数据库并返回联系人列表游标。该查询是针对ContactsContract.Contacts.CONTENT_URI 进行存储的URI。下一步检查游标是否包含记录,如果包含记录,侧记录ID字段的值存储在ID变量中。他将作为一个参数在后面的地方使用。也把名称字段的值存储在name变量中。对于游标的更多详细用法可以查看 Android的游标教程 < http://www.higherpass.com/Android/Tutorials/Accessing-Data-With-Android-Cursors/> 。 Phone Numbers 电话号码 Phone 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.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact. 电话号码存储在它们自己的表中,需要单独进行查询。要查询的电话号码表使用的是SDK中的变量ContactsContract.CommonDataKinds.Phone.CONTENT_URI存储的URI。使用WHERE条件得到指定联系人的电话号码。 //根据ID查询出电话号码 Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, 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 ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned. 在Android联系人SQLite数据库中执行第二个查询。查询的电话号码是针对ContactsContract.CommonDataKinds.Phone.CONTENT_URI存储的URI。CONTACT_ID存储在电话表中,ContactsContract.CommonDataKinds.Phone.CONTACT_ID和where子句用于限制返回的数据。 ? Email Addresses 电子邮件地址 Querying email addresses is similar to phone numbers. A query must be performed to get email addresses from the database. Query the URI stored in ContactsContract.CommonDataKinds.Email.CONTENT_URI to query the email address table. ?查询电子邮件地址类似电话号码。必须执行一个查询从数据库中获取电子邮件地址。根据存储在ContactsContract.CommonDataKinds.Email.CONTENT_URI的URI来查询电子邮件地址表。 Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null); while (emailCur.moveToNext()) { //如果email地址被保存在一个数组中,你将得到多个邮件地址 String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); } emailCur.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.CONTACT_ID field. Since multiple email addresses can be stored loop through the records returned in the Cursor. 正如手机查询email表中的字段名称也存在ContactsContract.CommonDataKinds。该email执行查询的URI ContactsContract.CommonDataKinds.Email.CONTENT_URI和where子句必须符合ContactsContract.CommonDataKinds.Email.CONTACT_ID领域。多个email地址可以通过存储在游标返回的记录循环。 Notes 注释 Custom 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. 可以为每个联系人记录附加自定义注释。这些注释被存储在一个单独的表中,根据相关的联系人ID查询。 String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; String[] noteWhereParams = new String[]{id,ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE}; Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null); if (noteCur.moveToFirst()) { String note = noteCur.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 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 field name is stored in ContactsContract.CommonDataKinds.Note.NOTE. 注释存储在Android联系人通用数据表中。当访问指定数据时where子句将需要2个条件。首先是标准的联系人ID,第二个是对那些被请求数据的媒体类型。在Android SDK中有一系列自动生成的变量处理这个。使用ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE来限要查询的记录。数据表URI存放在ContactsContract.Data.CONTENT_URI。最后,注意字段名称存储在ContactsContract.CommonDataKinds.Note.NOTE。 Postal Addresses 邮政地址 Android 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 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. Android的每个联系人都可以多个邮政地址。地址也存储在Notes数据表中,并通过存储在ContactsContract.Data.CONTENT_URI的URI查询。类似于注释查询,媒体类型必须被添加到where条件。另外,在Android2.0中,邮政地址记录被分割成多个小地址(邮政信箱、应力[不知道什么意思]、城市、地区、邮政编码)。在早期的Android SDK版本中,该地址是由一个自由格式的字符串存储。 String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; String[] addrWhereParams = new String[]{id,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE}; Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI,null, addrWhere, addrWhereParams, null); while(addrCur.moveToNext()) { 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(ContactsContract.CommonDataKinds.StructuredPostal.CITY)); String region = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION)); String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)); String country = 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 field names for the address pieces are stored in ContactsContract.CommonDataKinds.StructuredPostal. 此代码类似于前面的示例。请注意该地址块的字段名称都存储在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. 即时消息查询仅作为注释及地址查询。IM相关数据的重要字段名称存储在ContactsContract.CommonDataKinds.Im。 String imWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; String[] imWhereParams = new String[]{id,ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE}; Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI,null, imWhere, imWhereParams, null); if (imCur.moveToFirst()) { String imName = imCur.getString(imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)); String imType = imCur.getString(imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE)); } imCur.close(); Organizations 组织 The last part of the contact record to be covered is the Organizations data. 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 ContactsContract.CommonDataKinds.Organization. 联系人记录的最后一部分是组织的数据。Android的联系人记录可以包含有关就业、职业信息、社会成员以及角色和职称。这些记录从C??ontactsContract.Data.CONTENT_URI存储的URI查询。组织数据的重要字段名称存储在ContactsContract.CommonDataKinds.Organization。 String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; String[] orgWhereParams = new String[]{id,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.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA)); String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE)); } orgCur.close(); |
相关推荐
android studio api 23 android 6.0 requires android.permission.READ_CONTACTS or android.permiss
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{49af1fb 14363:com.example.gszczepanski.eduandroid/u0a59}
Intent学习
http://www.cnblogs.com/feike/archive/2013/01/03/2842952.html
Android代码-Contacts
Contacts Android Contacts API. Quick Start Initialize Contacts Library public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); Contacts.initialize(this); } Get All Contacts List contacts = Contacts.getQuery().find(); Get Contacts with phone numbers only Query q = Contacts.getQuery(); q.hasPhoneNumber(); List contacts = q.find(); Get Specific fields Query q = Contacts.getQuery(); q.include(Contact.Field.DisplayName, Conta
[Android基础系列]重新审视ContentProvider
这篇文章对大家用的不多的ContentProvider进行了较为系统的整理,没有去剖析原理,着眼于使用。希望对这块内容不熟的同学起到一点帮助作用
android.Contacts
public List<String> onContactsSelect(SQLiteDatabase dbreader) { List<String> list = new ArrayList<String>(); Cursor cursor = dbreader.query(TABLE_NAME, null, null, null, null, null, null); if (cursor.getCount() > 0) { while (cursor.moveToNext()) { list.add(cursor.getString(cursor.getColumnIndex(NUMBER))); } return list; } return null; }
Android编程基于Contacts读取联系人的方法(附demo源码)
本文实例讲述了Android编程基于Contacts读取联系人的方法。分享给大家供大家参考,具体如下: Android Contacts简介: 这里介绍安卓通讯录数据库。包括Android使用Contacts访问SQLite的基本知识,并了解Android SQLite和Contacts的更多信息。谷歌改变了从版本1到版本2的Contacts数据库。下面加以简单介绍。 Contacts 读取代码: package com.homer.phone; import java.util.ArrayList; import java.util.HashMap; import android.app.
Android 5.0 Contacts源码分析
最近工作中要整理最新的Android L的联系人源码分析,在网上各种搜索都不怎么满意,要么版本太老,要么分析的太浅层次了,要么只有简单的使用。无奈就自己整理,发出来共享一下。
OpenCV13-图像噪声:椒盐噪声和高斯噪声
考虑到椒盐噪声会在图像中的任何一个位置随机产生,因此对于椒盐噪声的生成,需要使用OpenCV中能够产生随机数的函数。OpenCV中也没有专门为图像添加高斯噪声的函数,对早在图像中添加椒盐噪声的过程,可以根据需求利用能够产生随机数的函数来完成在图像中添加高斯噪声的任务。椒盐噪声又称作脉冲噪声,他会随机改变图像中的像素值,其样子就像是在图像上随机地撒上一些盐粒和黑椒粒,因此被称为椒盐噪声。函数能够产生均匀分布或者高斯分布的随机数,可以利用该函数产生符合高斯分布的随机数,之后在图像中加入这些随机数即可。
Android学习笔记(四七):Content Provider初谈和Android联系人信息
Content Provider 在数据处理中,Android通常使用Content Provider的方式。Content Provider使用Uri实例作为句柄的数据封装的,很方便地访问地进行数据的增、删、改、查的操作。Android并不提供所有应用共享的数据存储,采用content Provider,提供简单便捷的接口来保持和获取数据,也可以实现跨应用的数据访问。简单地说,Androi...
硅光芯片边耦合器综述(一)
摘要:硅光子学在过去几十年间吸引了越来越多的关注,由于其具有诸多优点,如极低的成本、硅的高折射率带来的高集成密度,以及与现有半导体制造工艺的兼容性,被认为是未来日常应用中的一项充满前景的关键技术。在硅光子集成电路中,光互连是一个重要的问题,而在数据中心和光传输系统等应用场景中,光纤到芯片的光互连至关重要。光纤到芯片的光学耦合主要分为两大类:面外耦合(off-plane coupling)和面内耦合(in-plane coupling)。光栅耦合器属于前者,而边耦合器则属于面内耦合。本文主要关注硅光子集成电路
使用sysbench对Oracle做基准测试(-) 安装篇
sysbench是一款开源的多线程性能测试工具, 可以执行CPU/内存/线程/IO/数据库等方面的性能测试。数据库目前支持MySQL/Oracle/PostgreSQL。
php 导出vcard,将Android的contacts2.db导出成vcard联系人的方法
从Android的contacts2.db中导出vcard联系人方法,只简单说明下步骤1:拿到contacts2.db文件两种方法(1):通过手机的RE管理工具将目录data/data/com.android.providers.contacts/databases/contacts2.db文件拷贝出来(2):通过Recovery备份的文件data.img用unyaff程序解压缩后,可找到对应的d...
ESP32开发环境与微信小程序通信的完整示例
通过搭建稳定的esp32开发环境,实现其与微信小程序间的双向通信,涵盖Wi-Fi连接、数据收发与协议设计,为物联网应用提供低成本解决方案。
Android 选择联系人并返回手机号等信息(适配安卓11+)
【代码】Android 选择联系人并返回手机号等信息(适配安卓11+)
【老生谈算法】可视化通道流中瞬时涡量场的算法实现与MATLAB源码分析——瞬时涡量场
本文旨在探讨可视化通道流中瞬时涡量场的方法,并通过MATLAB编程实现这一过程。涡量场作为描述流体旋涡强度和方向的重要物理量,对于理解复杂流体运动具有重要意义。本文首先介绍了涡量场的基本概念,随后详细阐述了从测量数据收集到涡量场可视化的完整流程,并给出了相应的MATLAB源码。通过实际运行该源码,我们展示了涡量场的可视化结果,并讨论了其在流动结构识别和动力学行为理解中的应用。关键词: 涡量场;流体运动;MATLAB;可视化;PIV一、引言。
【升级U8+】不能将值 NULL 插入列 ‘VT_ID‘,表 ‘VoucherItems‘;列不允许有 Null 值。
不能将值 NULL 插入列 ‘VT_ID’,表 ‘UFDATA_006_2012.dbo.VoucherItems’;列不允许有 Null 值。INSERT 失败。
import android.provider.ContactsContract 出错
在操作通讯录的时候,发现导入import android.provider.ContactsContract会出错,后来百度了一下发现了有两种情况会导致该错: 情况一: android版本使用了是2.0以下,原因是android1.5里面没有android provider.ContactsContract包。android2.0以上才有。 情况二: andro
TVS 管击穿电压与钳位电压的工程解析
TVS二极管的核心参数击穿电压(VBR)和钳位电压(VC)常被混淆,但二者具有本质区别。VBR是
Android开发适配器系列:ListView使用自定义适配器(显示所有的联系人)
在开发中,我们经常使用到ListView这个控件。Android的AdivI也提供了许多创建ListView适配器的快捷方式。例如ArrayAdadivter、SimdivleAdadivter和SimdivleCursorAdadivter等。但你是否发现,如果采用这些系统自带的适配器,对于事件的响应只能局限在一个行单位。假设一行里面有一个按钮和一个图片控件,它们之间的响应操作是不一样的。若采用
用Obsidian搭建个人知识库:AI时代如何构建你的第二大脑
AI 时代,真正有价值的不只是获取知识,而是沉淀属于自己的知识体系。本文从 Obsidian 安装、目录设计、双向链接、插件推荐到 AI 联动、多端同步,手把手教你打造高效、可持续进化的个人知识库,让知识真正为你所用。🧠
STM32F407中文手册
STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。
android+隐藏邮件地址,如何获取Android设备的主要电子邮件地址
有几种方法可以做到这一点,如下所示。作为一种友好的警告,在处理帐户、个人资料和联系数据时,要小心和预先对用户进行处理。如果你滥用一个用户的电子邮件地址或其他个人信息,不好的事情可能会发生。方法A:使用会计经理(API第5级+)你可以用AccountManager.getAccounts或AccountManager.getAccountsByType若要获取设备上所有帐户名称的列表,请执行以下操作...
SAP B1中文培訓教材_SDK
公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说
android1.5所有ContentProvider
jdk-8u144-linux-x64.tar.gz.
该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包
OV9734 DATASHEET,完整规格书
OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等
如何通过AhMyth实现Android联系人远程管理:完整的通讯录访问指南
AhMyth是一款功能强大的跨平台Android远程管理工具,它提供了完整的联系人管理功能,让您能够远程访问和管理目标设备的通讯录。这款开源工具是目前GitHub上唯一维护的AhMyth版本,为安全研究人员和专业人士提供了可靠的Android设备管理解决方案。📱 ## 🔍 AhMyth联系人管理功能概述 AhMyth的联系人管理模块允许您远程读取Android设备的完整通讯录信息,包括联系
Android---requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
08-18 14:14:39.937 5950-5950/? E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.smartenergy_tech.dataeye, PID: 5950 java.lang.SecurityException: Permission Denial: opening provider co
6551



被折叠的 条评论
为什么被折叠?



