Android Content Providers(三)——Contacts Provider

接着上篇Android Content Providers(二)——Contacts Provider继续,接下来要说明的是顶层的Contacts,Contacts是聚合联系人表,在之前讨论的RawContacts是原始联系人表,在Android通讯录的架构中,分为聚合联系人、原始联系人和数据表三层。数据表主要存储数据和与原始联系人相关联的ID,Data表的字段Data在之前已经说明过,原始联系人RawContacts不直接存储数据,数据通过ID关联存储在数据表Data中,而关于聚合联系人,这里要先讲一下Android中的账户,Android可以绑定多个账户,在每一个账户下都可以有一份联系人信息,但是当我们进入通讯录,看到的其实就是聚合联系人,如果我们有多个账户,每个账户里都有同一个联系人,我们不希望看到同一个联系人重复出现很多次,所有有了聚合联系人表Contacts,Contacts同样通过ID与原始联系人表进行关联。下面看一下这三者之间的一个关系图:


顶层显示的是聚合联系人表Contacts,中间是原始联系人表RawContacts,这里有三个账户(两个Gmail,一个Twitter)分别存储这这个联系人的信息,最底层是Data表,存储着联系人的具体信息。通过这个图可以比较直观的看出Android系统通讯录的三层架构。


在使用过程中可以通过Intent的方式来进行通讯录的修改操作,通过下面这张表可以看到有几种直接调用系统铜须路的操作方式:

Task

Action

Data

MIME type

Notes

Pick a contact from a list

ACTION_PICK

One of:

Not used

Displays a list of raw contacts or a list of data from a raw contact, depending on the content URI type you supply.

CallstartActivityForResult(), which returns the content URI of the selected row. The form of the URI is the table's content URI with the row'sLOOKUP_IDappended to it. The device's contacts app delegates read and write permissions to this content URI for the life of your activity. See theContent Provider Basicsguide for more details.

Insert a new raw contact

Insert.ACTION

N/A

RawContacts.CONTENT_TYPE, MIME type for a set of raw contacts.

Displays the device's contacts application'sAdd Contactscreen. The extras values you add to the intent are displayed. If sent withstartActivityForResult(), the content URI of the newly-added raw contact is passed back to your activity'sonActivityResult()callback method in theIntentargument, in the "data" field. To get the value, callgetData().

Edit a contact

ACTION_EDIT

CONTENT_LOOKUP_URIfor the contact. The editor activity will allow the user to edit any of the data associated with this contact.

Contacts.CONTENT_ITEM_TYPE, a single contact.

Displays the Edit Contact screen in the contacts application. The extras values you add to the intent are displayed. When the user clicksDoneto save the edits, your activity returns to the foreground.


这里提供一种通过Intent来使用的方法,还有其他的使用方式,具体使用方式如下:

// Gets values from the UI

String name = mContactNameEditText.getText().toString();

String phone = mContactPhoneEditText.getText().toString();

String email = mContactEmailEditText.getText().toString();

String company = mCompanyName.getText().toString();

String jobtitle = mJobTitle.getText().toString();

// Creates a new intent for sending to the device's contacts application

Intent insertIntent =newIntent(ContactsContract.Intents.Insert.ACTION);

// Sets the MIME type to the one expected by the insertion activity

insertIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE);

// Sets the new contact name

insertIntent.putExtra(ContactsContract.Intents.Insert.NAME, name);

// Sets the new company and job title

insertIntent.putExtra(ContactsContract.Intents.Insert.COMPANY, company);

insertIntent.putExtra(ContactsContract.Intents.Insert.JOB_TITLE, jobtitle);


最后,在使用系统联系人的时候,需要加上两个权限:

<uses-permission android:name="android.permission.READ_CONTACTS">

<uses-permission android:name="android.permission.WRITE_CONTACTS">


详细文档可以参考:http://developer.android.com/guide/topics/providers/contacts-provider.html


关于这部分内容我还有些理解的不是很到位的地方,后续会继续学习,感兴趣的朋友可以一起研究。

欢迎关注我的新浪微博和我交流:@唐韧_Ryan


来源:网络


智能推荐

Android应用程序组件Content Provider应用实例(5)

         接下来再来看程序的配置文件AndroidManifest.xml: <?xml version="1.0" encoding="utf-8"?>   <manifest xmlns:android="ht...

Android学习笔记(四九):通过Content Provider访问数据

在上次笔记中,我们编写了自己的Provider,这次笔记,我们将通过Content Provider的Uri接口对数据进行访问,重写Android学习笔记(四二)中例子。在这里我们不在充分描述相关UI如何编写,可以到笔记(四二)中详细查看,重点讲述如何实现数据的访问。 读取信息 读取信息方式,在笔记(四七)中已经介绍,代码如下 private voidread(){ /* 通过managedQue...

Android系统数据共享---Content Provider学习小结

Content Provider作为Android应用程序中的四大组件之一,主要是为了实现在各应用程序之间数据共享,增强应用程序的复用,例如,在开发过程中,需要获取手机中的通讯录信息,这时完全不需要自己重新开发读取数据的整个过程, 而是直接访问系统自带的Content Provider对象来直接获取数据(此例子只是说明有现成的能满足需要的ContentProvider即可随时“拿来&r...

Pro Android学习笔记(五) 了解Content Provider(上)

                Content Provider是抽象数据封装和数据访问机制,例如SQLite是Android设备带有的数据源,可以封装到一个content provider中。要通过content provider进行读写,需要使用...

安卓初认识——Android-内容提供者(Content Provider)、Android碎片(Fragment)

Android-内容提供者(Content Provider) 内容提供者组件通过请求从一个应用程序向其他的应用程序提供数据。这些请求由类ContentResolver的新方法来处理。内容提供这可以使用不同的方式来存储数据。数据可以被存放在数据库、文件、甚至是网络。 有时候需要在应用之间共享数据。 内容提供者可以让内容集中,必要时可以有多个不同的应用程序来访问。内容提供这的行为和数据库很像,可以查...

猜你喜欢

【java多线程】守护线程、线程停止、volatile的深入了解

文章目录 线程的优雅停止 守护线程 volatile关键字 线程的优雅停止       在多线程的操作之中如果要启动多线程肯定使用的Thread类中的start()方法,而对于咱们的多线程需要进行停止处理,原来的Thread类提供有stop()方法,但是对于这些方法从JDK1.2版本就已经将其废除了,而且一直到现在也不建议出现在你的代码中...

慕课嵌入式开发及应用(第二章.C#快速入门与通信编程方法)

慕课苏州大学.嵌入式开发及应用.第二章.入门与软件框架.C#快速入门与通信编程方法 0 目录 2 入门与软件框架 2.9 C#快速入门与通信编程方法 2.9.1 课堂重点 2.9.2 测试与作业 3 下一章 0 目录 2 入门与软件框架 2.9 C#快速入门与通信编程方法 2.9.1 课堂重点 2.9.2 测试与作业 关于PC机和MCU之间的通信,说法错误的是? A.PC机也叫上位机,MCU端叫下...

yolov4论文解读

YOLO v4它来了,速度效果双提升,研究者对比了 YOLOv4 和当前最优目标检测器,发现 YOLOv4 在取得与 EfficientDet 同等性能的情况下,速度是 EfficientDet 的二倍!此外,与 YOLOv3 相比,新版本的 AP 和 FPS 分别提高了 10% 和 12%。迅速引起了 CV 社区的关注。 YOLO v4 论文:https://arxiv.org/abs/2004...

注释,无处不在的注释

十年前的2004年 , Java 1.5开始提供注释。 很难想象没有此功能的代码。 实际上,首先引入了注释,以减轻开发人员编写繁琐的样板代码的痛苦,并使代码更具可读性。 考虑一下J2EE 1.4(没有可用的注释)和Java EE5。注释的采用通过消除所有配置XML大大简化了Java EE应用程序的开发。 即使在今天,更多的注释仍被添加到最新版本的Java EE中。 这样做的目的是减轻开发人员的负担...

eclipse项目转移至idea

转移过来的项目主要jdk版本设置以及Tomcat的部署就好,基本上都可以搜的到,这次记录的主要是配置完成之后运行的报错问题,如图: 很多都是说jdk未设置统一,查了很久才找到真正的解决办法: 1,切换ISO-8859-1,之后再切换回UTF-8 2,上一步还是报错的话就按下图清除缓存即可解决 File–Invalidate Caches/Restart…...

问答精选

Find row name of a Table given max value

I have a table, tble from a dataframe, dfas below: So it produces values as follows: I want to define a variable that returns the row name with the max value in the table. For this example, it would b...

PDO bind params placeholders PostgreSQL conflict

I am using postgres for a database, and noticed, that PDO reserves some of Postgres syntax features for variable binding, for example: checking if hstore has a key SELECT * FROM t WHERE store ? 'key' ...

Is there a way to make dynamically queries using SvelteKit load function?

I am stuck trying to make Strapi's filter parameter dynamic in SvelteKit. Using insomnia, the backend, filters posts based on the parameter, as usual. I am trying to use the page object from the svelt...

My Search box is only searching only uppercase

I am having a problem in making my project. I have made a search box which is searching only uppercase, i don't want to make it case sensitive. Can some one help me. I will be thankful to you. here is...

How hard would it be to translate a programming language to another human language?

Let me explain. Suppose I want to teach Python to someone who only speaks Spanish. As you know, in most programming languages all keywords are in English. How complex would it be to create a program t...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答