在Android中如果你的application想独享数据,请用SQLite 、share;如果你的application想共享数据,请用ContextProvide。在 ContentProvider内部你可以用Android支持的任何手段进行数据的存储和操作,可能比较常用的方式是基于Android的SQLite 数据库。使用ContentProvider,首先要在AndroidManifest.xml中注册,这样其他application可以通过获取 ContentResolver通过Uri来操作上一程序的数据。
实现自己的ContentProvider只要继承android的ContentProvider,然后重载其几个基本方法即可,下面列出几个主要的方法
-
onCreate()
which is called to initialize the provider -
query(Uri, String[], String, String[], String)
which returns data to the caller -
insert(Uri, ContentValues)
which inserts new data into the content provider -
update(Uri, ContentValues, String, String[])
which updates existing data in the content provider -
delete(Uri, String, String[])
which deletes data from the content provider -
getType(Uri)
which returns the MIME type of data in the content provider
这里主要说一下getType(uri)。根据传入的uri,该方法按照MIME格式返回一个字符串唯一标识该uri的类型。所谓uri 的类型,就是描述这个uri所进行的操作的种类,比如content://xx/a与 content://xx/a/1不是一个类型(前者是多值操作,后者是单值),但content://xx/a/1和content://xx/a/2 就会是一个类型(只是id号不同而已)。
android的sdk中是这样描述Uri的。
Here is a recap of the important parts of a content URI:
- Standard prefix indicating that the data is controlled by a content provider. It's never modified.
-
The authority part of the URI; it identifies the content provider. For third-party applications, this should be a fully-qualified class name (reduced to lowercase) to ensure uniqueness. The authority is declared in the
<provider>
element'sauthorities
attribute:<provider android:name=".TransportationProvider" android:authorities="com.example.transportationprovider" . . . >
-
The path that the content provider uses to determine what kind of data is being requested. This can be zero or more segments long. If the content provider exposes only one type of data (only trains, for example), it can be absent. If the provider exposes several types, including subtypes, it can be several segments long — for example, "
land/bus
", "land/train
", "sea/ship
", and "sea/submarine
" to give four possibilities. -
The ID of the specific record being requested, if any. This is the
_ID
value of the requested record. If the request is not limited to a single record, this segment and the trailing slash are omitted:content://com.example.transportationprovider/trains
B:URI的标识,它定义了是哪个Content Provider提供这些数据。对于第三方应用程序,为了保证URI标识的唯一性,它必须是一个完整的、小写的 类名。这个标识在 元素的 authorities属性中说明:一般是定义该ContentProvider的包.类的名称;"content://com.example.transportationprovider
C:路径,不知道是不是路径,通俗的讲就是你要操作的数据库中表的名字,或者你也可以自己定义,记得在使用的时候保持一致就ok了;"content:/com.example.transportationprovider/tablename"
D:如果URI中包含表示需要获取的记录的ID;则就返回该id对应的数据,如果没有ID,就表示返回全部; "content://com.example.transportationprovider/tablename/#" #表示数据id
引用前辈们总结的几个创建ContentProvider是常用的方法:
UriMatcher:用于匹配Uri,它的用法如下:
1.首先把你需要匹配Uri路径全部给注册上,如下: //常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码(-1)。 UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); //如果match()方法匹配content://com.changcheng.sqlite.provider.contactprovider/contact路径,返回匹配码为1 uriMatcher.addURI(“com.changcheng.sqlite.provider.contactprovider”, “contact”, 1);//添加需要匹配uri,如果匹配就会返回匹配码 //如果match()方法匹配 content://com.changcheng.sqlite.provider.contactprovider/contact/230路径,返回匹配码为2 uriMatcher.addURI(“com.changcheng.sqlite.provider.contactprovider”, “contact/#”, 2);//#号为通配符
2.注册完需要匹配的Uri后,就可以使用uriMatcher.match(uri)方法对输入的Uri进行匹配,如果匹配就返回匹配码,匹配码是调用 addURI()方法传入的第三个参数,假设匹配 content://com.changcheng.sqlite.provider.contactprovider/contact路径,返回的匹配码为1。
ContentUris:用于获取Uri路径后面的ID部分,它有两个比较实用的方法:
· withAppendedId(uri, id)用于为路径加上ID部分
· parseId(uri)方法用于从路径中获取ID部分