The content provider mechanism in Android supports data storage and reading in multiple applications. This is the only way to share data across applications. In the Android system, there is no public memory area for multiple applications to share and store data.
Android provides some major data types of content providers, such as audio, video, images, and private address book. You can find some content providers provided by Android in the Android. provider package. You can obtain these content providers and query the data they contain, provided that you have obtained the appropriate read permission.
If you want to disclose your data, you can use either of the following methods:
1. Create your own content provider and inherit the contentprovider class;
2. If your data structure is consistent with that of the existing content provider, you can write the data to the existing content provider, provided that you have the permission to write the content provider. For example, add the member communication information in OA to the system's contact content provider.
All content providers must implement the same interface to query content providers and return data, including adding, modifying, and deleting data. First, you need to obtain a contentresolver instance. You can use the activity member method getcontentresovler:
Contentresolver Cr = getcontentresolver ();
The contentresolver instance can find the specified content provider and obtain the data of the content provider. When the contentresolver query process starts, the android system will determine the specific content provider required for the query and determine whether it starts and runs it. The Android system initializes all content providers and does not need to be created by the user. In fact, none of the content provider users can directly access the content provider instance. They can only use contentresolver to proxy in the middle. The data model content provider displays data similar to a single database table. Where:
Each row has a numeric field with a unique value named _ id, which can be used to locate the specified record in the table;
The data structure returned by the content provider is similar to the JDBC resultset. In Android, It is a cursor object.
Each content provider defines a unique public Uri, which is used to specify its dataset. A content provider can contain multiple datasets (multiple tables can be considered). In this way, multiple Uris must correspond to each dataset. These Uris must begin with the following format:
Content ://
Indicates that this URI specifies a content provider. If you want to create your own content provider, you 'd better set the custom URI to a constant of the class, which simplifies others' calls and will be easy to update the URI later. Android defines the content_uri constant for Uri, for example:
Android. provider. Contacts. Phones. content_uri
Android. provider. Contacts. Photos. content_uri
Note that contacts in the preceding example is not supported in Android 2.0 or later versions. To query a content provider, you need the following information:
Define the URI of the content provider
Field name of the returned result
Data Types of these fields
To query a specific record (ROW) of the content provider dataset, you also need to know the value of the Record ID. The construction query is the input of Uri and other parameters, where URI is required and others are optional. If the system can find the content provider corresponding to the URI, a cursor object is returned. You can use contentresolver. Query () or activity. managedquery. The method parameters are the same, and the query process and return value are the same. The difference is that through activity. the managedquery () method not only obtains the cursor object, but also manages the life cycle of the cursor object. For example, when the activity is paused (pause), it is uninstalled, re-Query when activity restart. You can also manage a cursor object that is not in activity management by calling activity. startmanagincursor. Similar to this:
Cursor cur = managedquery (myperson, null );
The first parameter myperson is a URI-type instance. If you want to query records of a specified row, you need to use the _ id value. For example, if the id value is 23, the URI will be similar:
Content: //.../23
Android provides a convenient method, so developers do not need to splice the above Uri, such:
Uri myperson = contenturis. withappendedid (people. content_uri, 23 );
Or
Uri myperson = URI. withappendedpath (people. content_uri, "23 ");
The difference between the two is that an id value of the integer type is received, and a string type is received. Other parameters:
Names, which can be null, indicates taking all the columns of the dataset, or declaring a string array, storing the column name in the array, such as: people. _ id. Generally, column names have constant mappings in the content provider;
The filter for returned results is similar to the WHERE clause in SQL. The difference is that there is no where keyword. If null is returned, it indicates no filtering, such as name = ?;
The parameter of the preceding filter is a string array, which is used in the preceding condition? Placeholder value;
The sorting parameter is similar to the order by clause in SQL. However, you do not need to write the order by clause, such as name DESC. If it is not sorted, enter null.
The returned value is a cursor object. The cursor is located before the first record. The following example applies to Android 2.0 and later versions. You can obtain the name field from the android address book:
Cursor cursor = getcontentresolver (). Query (
Contactscontract. commondatakinds. Phone. content_uri, null, null,
Null,
Null );
Returned value content _ id _ count name number
44 3 Alan vain 212 555 1234
13 3 bully pulpit 425 555 6677
53 3 Rex cars 201 555 4433
The returned content is similar. Different content providers have different columns and names, but there are two identical columns. the one mentioned above is _ id, which is used to uniquely identify a record, there is also a _ count used to record the size of the entire result set. We can see that the value of _ count in the figure above is the same. If the ID is used during query, only one record is returned. In other cases, there are usually multiple records. Similar to the JDBC resultset, You need to manipulate the cursor to traverse the result set. In each row, you can obtain the column value through the column name. You can use getstring (), getint (), getfloat (). For example:
While (cursor. movetonext ()){
Builder
. Append (
Cursor
. Getstring (cursor
. Getcolumnindex (contactscontract. commondatakinds. Phone. display_name )))
. Append ("-");
}
Different from JDBC, you cannot directly obtain the column value through the column name. You can only first obtain the integer index value of the column by the column name, and then locate and obtain the column value through the index value. You can use content provider to edit data as follows:
Add new records;
Add a new value to an existing record;
Update multiple existing records in batches;
Delete a record.
All editing functions are implemented through contentresolver. Some content providers require more strict permissions and write permissions. If they do not, an error is reported. To add a record to the content provider, you must first set a map-like key-value pair in the contentvalues object. Here, the key value corresponds to the column name in the content provider, the value of the key-value pair, which is the expected type of the corresponding column. Then, call the contentresolver. insert () method to pass in the contentvalues object and the URI of the corresponding content provider. The returned value is the URI object of the new record. In this way, you can use this URI to obtain the cursor object containing this record. For example:
Contentvalues values = new contentvalues ();
Values. Put (people. Name, "Abraham Lincoln ");
Uri uri = getcontentresolver (). insert (people. content_uri, values );
If the added value of the original record already exists, you can add a new value to the record or edit an existing value. First, you must go to the original value object, and then clear the original value, and then add the record as above:
Uri uri = URI. withappendedpath (people. content_uri, "23 ");
Uri phoneuri = URI. withappendedpath (Uri, people. Phones. content_directory );
Values. Clear ();
Values. Put (people. Phones. type, people. Phones. type_mobile );
Values. Put (people. Phones. Number, "1233214567 ");
Getcontentresolver (). insert (phoneuri, values );
Update values in batches to update the values of a group of records. For example, NY is renamed eew York. You can call the contenresolver. Update () method. Delete a record. If you delete a single record, call the contentresolver. Delete () method, Uri parameter, and specify it to a specific row. To delete multiple records, call the contentresolver. Delete () method. Specify the content provider by using the URI parameter and add a where clause condition similar to SQL. This is similar to the above, without the where keyword. To create a content provider, you must:
Set the storage system. Most content providers use files or SQLite databases, but you can store data in any way. Android provides sqliteoopenhelper to help developers create and manage sqlitedatabase.
Inherits contentprovider and provides access to data.
Declare content provider in the manifest file.
To inherit the contentprovider class, you must define a subclass of the contentprovider class. You must implement the following method: Query ()
Insert ()
Update ()
Delete ()
GetType ()
Oncreate () query () method. The returned value is the cursor instance, which is used to iterate the request data. Cursor is an interface. Android provides some read-only cursor implementations for this interface (unlike the JDBC resultset, which also provides writable optional features. For example, sqlitecursor can iterate data in the SQLite database. You can obtain the cursor instance through the query () method of the sqlitedatabase class. There are other cursor implementations, such as matrixcursor, used when data is not stored in the database. Because the content provider may be called by multiple contentresolver objects in different processes and threads, thread security must be considered for implementing content provider. As a good habit, in code that implements data editing, call the contentresolver. policychange () method to notify listeners that listen for data changes. When subclass is implemented, there are still some steps to simplify the use of the content provider client: defining a public static final URI constant named content_uri:
Public static final URI content_uri =
Uri. parse ("content: // com. example. codelab. transportationprovider ");
If there are multiple tables, they also use the same content_uri, but their paths are different.
That is to say, the red frame is consistent. Defines the returned column name, public static final, and column name value. For example, the SQLite database is used as the storage, and the column name of the corresponding table is defined. In this document, you need to write the Data Types of each column for easy reading. If you want to process a new MIME data type, such as using intent and mimetype with data, you need to process it in the contentprovider. GetType () method. After the content provider is declared to create a content provider, it must be declared in the manifest file to be known by the Android system. Only when other applications call the content provider can they create or call it. The syntax is similar:
<Provider Android: Name = "com. easymorse. CP. mycontentprovider"
Android: Authorities = "com. easymorse. CP. mycp"> </provider>
Android: name indicates the full name of the contentprovider inheritance class. Android: Authorities: Write Part B of the content_uri constant (see the figure above ). Do not add C and D to authorities. Authorities are used to identify contentprovider. C and D are actually used internally by contentprovider.