阶段笔记浅谈ContentProvider&ContentResolver&SQLite

来源:互联网 发布:信息收集软件 编辑:程序博客网 时间:2024/06/17 01:04

在此附以Android API官网查询地址https://developer.android.com/reference/packages.html

内容提供器主要实现应用程序之间的数据传输。

客户端采用ContentResolver接收ContentProvider传来的数据

ContentResolver:

实例化ContentResolver:

ContentResolver cr = getContentResolver();

官方解释:

getContentResolver()                   
Return a ContentResolver instance for your application's package.

注意! getContentResolver()来源于android.content.Context

实例化ContentValues:

代码:

ContentValues values = new ContentValues();

values.put(KEY,VALUE);

解析:

put(String key, String value)                   
Adds a value to the set.

URI的解释:

由于是在不同的应用程序之间进行数据传输,所以URI包括了两个部分权限和路径,权限(一般采用应用程序的包名,例如包名是com.test.app,那么权限就是com.test.app.provider)区分不同应用,路径(放在权限的后面,例如一个应用有两个数据表table1&table2)区分同一应用下的不同的数据表。

在此只说一下内容URI的格式,如下:

content://com.test.app.provider/table1,

content://com.test.app.provider/table2,

两个内容URI就完成了,清晰的就知道访问的是那个应用的哪个数据表了。

当然还没结束,还需要将URI解析成Uri对象:

Uri uri = Uri.parse("content://com.test.app.provider/table1");

解析:

parse(String uriString)                   
Creates a Uri which parses the given encoded URI string.

Cursor(光标)的解释:

This interface provides random read-write access to the result set returned by a database query.

综合上述:

实现数据的查询操作:

private Cursor cursor ;

cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,
                    null,null,MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

 解析:

query(Uri uri,String[] projection,String[] selection,String selection,String[] selectionArgs,String sortOrder)

Query the given URI,returning a Cursor over the result set. 

实现数据的插入

cr.insert(uri,values);

解析:

insert(Uri url, ContentValues values)                   
Inserts a row into a table at the given URL.

ContentProvider:

 Content providers are one of the primary building blocks of Android applications, providing content to applications. 

 

onCreate()                   
Implement this to initialize your content provider on startup.初始化内容提供器。

 

update(Uri uri, ContentValues values, String selection, String[] selectionArgs)                   
Implement this to handle requests to update one or more rows.

 

query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal)                   
Implement this to handle query requests from clients with support for cancellation.

 

insert(Uri uri, ContentValues values)                   
Implement this to handle requests to insert a new row.

 

getContext()  返回的是Context类,           
Retrieves the Context this provider is running in.

 

这个提供程序运行时检索上下文。

 

Context:

Interface to global information about an application environment.  This is an abstract class whose implementation is provided by the Android system.  It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

 

说到ContentProvider就要谈一下SQLiteDatabase了

SQLiteDatabase:

Contains the SQLite database management classes that an application would use to manage its own private database.
Applications use these classes to manage private databases. If creating a content provider, you will probably have to use these classes to create and manage your own database to store content. See Content Providersto learn the conventions for implementing a content provider. If you are working with data sent to you by a provider, you do not use these SQLite classes, but instead use the generic android.database classes.

大体意思是:需要SQLiteDatabase来实现私有数据库的搭建,存的是Provider content。

SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks.

意思:SQLiteDatabase实现数据库的增删改查的操作。

执行SQL语句的方法:

execSQL(String sql)                   
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

 

 

SQLiteOpenHelper:

    A helper class to manage database creation and version management.

      一个助手类管理数据库实现创建和版本的升级

    You create a subclass implementing onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int) and optionally onOpen(SQLiteDatabase), and this class takes care of opening the database if it exists, creating it if it does not, and upgrading it as necessary. Transactions are used to make sure the database is always in a sensible state.

    大体意思:需要子类继承它的onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int)方法,可选方法onOpen(SQLiteDatabase),数据库存在则打开它,不存在就创建然后打开数据库。

方法:

onCreate(SQLiteDatabase db)                   
Called when the database is created for the first time.数据库初次创建时调用,可以创建数据库。

onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)                 
Called when the database needs to be upgraded. 数据库版本更新时调用。

onOpen(SQLiteDatabase db)                   
Called when the database has been opened.

getWritableDatabase()             
Create and/or open a database that will be used for reading and writing.该方法允许读写数据库,返回的是SQLiteDatabase

可以这样:

     

<pre class="java" name="code">class DBHelper extends SQLiteHelper{             public DBHelper(Context context){}             onCreate(SQLiteDatabase db) ;             onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) ;       }class DBProvider extends ContentProvider{                          private DBHelper dbOpenHelper;             public boolean onCreate(){                   dbOpenHelper = new DBHelper(getContext());               }}            SQLiteDatabase db = dbOpenHelper.getWritableDatabase();      然后进行 db.delete();    db.insert();     db.query();等操作。


 

SQLiteDatabase  中有数据库操作的语句

ContentProvider  中也有数据库的操作(也是操作SQLiteDatabase 中的增删改查)

上述总结:

具体的流程就是,先创建SQLite数据库,然后在ContentProvider中操作(增删改查)数据库,最后在ContentResolver中提取ContentProvider提供的内容。

 

 

 

 

 

 

 

 

    

0 0
原创粉丝点击