Android Note: Content provider contents Provider

Source: Internet
Author: User
Tags sqlite database

ContentProvider isprimarily used to enable the sharing of data between different applications.

There are two general types of content providers, one of which is to use an existing content provider to read and manipulate the corresponding program
Data, the other is to create your own content provider to provide external access to our program's data.

The first method: use Contentresolver to read and manipulate data in the corresponding program

1. Parse the content URI string into a URI object.

A URI (Uniform Resource identifier) refers to a Uniform resource identifier
Uri uri = URI. Parse ("Content://com.example.databasetest.provider/book");

2. Perform the operation of the increase and deletion check as follows:

cursor cursor = getcontentresolver (). query (Uri,projection,selection,selectionargs,sortorder);

if (cursor! = NULL) {
while (Cursor.movetonext ()) {
String column1 = cursor.getstring (Cursor.getcolumnindex ("Column1"));
int column2 = Cursor.getint (Cursor.getcolumnindex ("Column2"));
}
Cursor.close ();
}

Insert (increment): Instantiate contentvalues , call the Put method to add a key-value pair, and then callthe Get Contentresolver Insert method.

Contentvalues values = new Contentvalues ();
Values.put ("Column1", "text");
Values.put ("Column2", 1);
Getcontentresolver (). Insert (URI, values);

contentvalues contentresolver.

Values.put ("pages", 1216);
Values.put ("Price", 24.05);
Getcontentresolver (). Update (URI, values, NULL, NULL);

call method to delete data

getcontentresolver (). Delete (URI, NULL, NULL);

The second way: Create your own content provider

A. Create a class that inherits the ContentProvider parent class.rewrite OnCreate (), query (), update (), insert (), delete (), GetType () method

B. Define a class variable named Content_uri, which is a public static final URI type, and you must specify a unique string value for it, the best scenario is to use the full name of the class, such as:
public static final Uri Content_uri = Uri.parse ("Content://com.google.android.mycontentprovider");

C. Define the name of the data column you want to return to the client. If you are using an Android database, you must define a column called _id, which is used to represent the uniqueness of each record.

d. Create your data storage system. Most content provider use an android file system or SQLite database to keep data, but you can also store it in any way you want.

E. If you want to store byte data, such as a bitmap file, the data column is actually a URI string representing the actual saved file, through which to read the corresponding file data. The content provider that handles this data type needs to implement a field named _data, and the _data field lists the exact path to the file on the Android file system. This field is not only intended for use by clients, but is also available to contentresolver. The client can call the Contentresolver.openoutputstream () method to handle the file resource that the URI points to, and if it is contentresolver itself, it can access the data file directly because it has higher permissions than the client.

F. Declare a variable of public static string that specifies the data column to be returned from the cursor.

g. The query returns an object of type cursor. All methods that perform write operations, such as insert (), update (), and delete (), are monitored. We can notify listeners about data updates by using the Contentresover (). Notifychange () method.

H. Use the <provider> tag in Androidmenifest.xml to set the content provider.

i. If the type of data you are dealing with is a newer type, you must first define a new MIME type for the Contentprovider.getype (URL) to return. There are two forms of mime types: one for the specified single record, and one for multiple records. A common format is given here:

Vnd.android.cursor.item/vnd.yourcompanyname.contenttype (MIME type for individual records)
For example, a URI that requests train information, such as content://com.example.transportationprovider/trains/122, might return typevnd.android.cursor.item/ Vnd.example.rail such a MIME type.

Vnd.android.cursor.dir/vnd.yourcompanyname.contenttype (MIME type for multiple records)
For example, a URI that requests all train information, such as Content://com.example.transportationprovider/trains, might return vnd.android.cursor.dir/ Vnd.example.rail such a MIME type.

Resources:

1. "First line code" content provider;

2.CSDN Blog: http://blog.csdn.net/chuyuqing/article/details/39995607

Android Note: Content provider contents Provider

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.