BaseProject快速构建自己的APP

Android四大组件对大家来说都不陌生,但是相对于Activity,Service,广播来说,ContentProvider好像很容易被忽略,因为他确实在普通项目中很少使用,不使用它也能很好的完成项目需求,但是ContentProvider我觉得是Android中最牛逼的设计之一了,很多的数据共享都是用它来实现的,最常用的是android获取手机联系人就是用的ContentProvider来实现的.


ContentProvider介绍


以下内容摘自谷歌:

Content providers are one of the primary building blocks of Android applications, providing content to applications. They encapsulate data and provide it to applications through the single ContentResolver interface. A content provider is only required if you need to share data between multiple applications. For example, the contacts data is used by multiple applications and must be stored in a content provider. If you don't need to share data amongst multiple applications you can use a database directly via SQLiteDatabase.

谷歌翻译:

内容提供者是Android应用程序的主要组成部分之一,为应用程序提供内容。 它们封装数据,并通过单个ContentResolver接口将其提供给应用程序。 只有在需要在多个应用程序之间共享数据时,才需要内容提供者。 例如,联系人数据由多个应用程序使用,并且必须存储在内容提供者中。 如果您不需要在多个应用程序之间共享数据,则可以直接通过SQLiteDatabase使用数据库。

白话翻译:

ContentProvider用来实现不同App间的数据共享,就好比手机联系人,它本身的实现方式就使用了ContentProvider,它本身属于“内容的提供者”,它实现机制就是为了数据共享. 因此,手机联系人是内容提供者,是数据的来源,其他第三方的数据需要使用它的数据,可以通过 ContentResolver接口(数据通道)共享给其他应用程序.谷歌还说了,如果不需要共享数据,那就直接使用SQLite数据库吧.


ContentProvider"内容提供者"实现步骤


ContentProvider组成部分


ContentProvider主要通过Uri(统一资源定位符)来向外共享数据的.
Uri由三部分组成(Uri可以类比于http://www.baidu.com,相对于绝对地址,就是数据存储的位置)
1.scheme

  ContentProvider(内容提供者)的scheme已经由Android所规定.
  scheme为:content://

2.主机名

  主机名(或叫Authority)用于唯一标识这个ContentProvider,比如这里          com.contentproviderdb.TestContentProvider,外部调用者可以   根据这个标识来找到它。

3.path 

   路径(path)可以用来表示我们要操作的数据,路径的构建应根据业务而定,今天demo我新建的User表
   /User
  上面三个部分是主要的,还可以有更详细的,比如还包含数据id /User/id。
  
  如果要把一个字符串转换成Uri,可以使用Uri类中的parse()方法,如下:
  
  Uri uri = Uri.parse("content://com.contentproviderdb.TestContentProvider/User").

 然后再介绍一下UriMatcher类使用:
 因为Uri代表了要操作的数据,所以我们经常需要解析Uri,并从Uri中获取数据。Android系统提供了两个用于操作Uri  的工具类,分别为UriMatcher和ContentUris 。掌握它们的使用,会便于我们的开发工作。
 UriMatcher类用于匹配Uri,它的用法如下:

  if(uriMatcher.match(uri) == USER)

 首先第一步把你需要匹配Uri路径全部给注册上,如下:
  uriMatcher.addURI("com.contentproviderdb.TestContentProviderr","User",USER);
   注册完需要匹配的Uri后,就可以使用sMatcher.match(uri)方法对输入的Uri进行匹配,如果匹配就返回匹配码,匹配  码是调用addURI()方法传入的第三个参数。

https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499949520

https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499928767
https://buluo.qq.com/p/detail.html?bid=92799&pid=7168867-1503839527
https://buluo.qq.com/p/detail.html?bid=92799&pid=7168867-1503839527
https://buluo.qq.com/p/detail.html?bid=92799&pid=7168867-1503839513
https://buluo.qq.com/p/detail.html?bid=92799&pid=7168867-1503838541
https://buluo.qq.com/p/detail.html?bid=92799&pid=7168867-1503838527
https://buluo.qq.com/p/detail.html?bid=92799&pid=7168867-1503834821
https://buluo.qq.com/p/detail.html?bid=92799&pid=7168867-1503834799
https://buluo.qq.com/p/detail.html?bid=92799&pid=7168867-1503832624
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499949550
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499949536
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499949520
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499949506
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499949491
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499945867
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499945852
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499937902
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499937890
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499936990
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499936972
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499936955
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499936942
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499928754
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499928735
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499928383
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499928368
https://buluo.qq.com/p/detail.html?bid=92799&pid=4685111-1499928350

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值