About Android content providers

Source: Internet
Author: User

In Android applications, we can use explicit intent (Explicit Intent) to directly access the activity of other applications, but this is limited to the scope of activity, and if you need to use data from other applications, you need to use another component. This is known as content provider (Provider).

1. Introduction to content providers (Porviders)

content providers are primarily used to implement data sharing among different applications, providing a complete set of mechanisms that allow one program to access data from another program while guaranteeing the security of the data being visited . Currently, using content providers is a standard way for Android to share data across programs.

There are two general uses of content providers, one is to use existing content providers to read and manipulate data in the corresponding program, and to create their own content providers to provide external access to the data of our programs.

Official website definition: Content providers is responsible for managing access to structured data, contentproviders encapsulating data, and providing a set of mechanisms for defining data security. Contentproviders is a set of interfaces for data access between different processes. Contentproviders provides a secure set of access mechanisms for data cross-process access, providing a reliable guarantee for data organization and secure access.

Each content provider class uses a URI (Universal Resource Identifier, Universal Resource Identifier) as a separate identity, in the form of: content://com.example.app.provider/ Table1. Other applications access different content providers through different URIs and get/manipulate the data inside.

2, the role of contentproviders?

Android uses ContentProvider to manage data such as audio, video, images, and contacts. You can also access the SQLite database via ContentProvider.

You need to use contentproviders in the following situations:

    • You want to provide complex data or files for other applications
    • You want to allow users to copy complex data from your application to other applications
    • You want to use the search framework to provide custom query suggestion functionality

3, Content Provider method Introduction

(1) onCreate ()

Called when the content provider is initialized. Operations such as creating and upgrading a database are usually done here, and returning True indicates that the content provider initialized successfully and that returning false indicates failure. Note that the content provider is initialized only if there is a ontentresolver attempt to access the data in our program.

(2) query ()

Query the data from the content provider. Use the URI parameter to determine which table to query, the projection parameter to determine which columns to query, the selection and Selectionargs parameters to constrain which rows to query, and the SortOrder parameter to sort the results. The results of the query are stored in the cursor object.

(3) Insert ()

Add a piece of data to the content provider. Use the URI parameter to determine the table to add, and the data to be added is saved in the values parameter. When added is complete, returns a URI that represents the new record.

(4) Update ()

Updates the data in the content provider. Use the URI parameter to determine which table data is updated, the new data is saved in the values parameter, the selection and Selectionargs parameters are used to constrain which rows are updated, and the number of rows affected is returned as the return value.

(5) Delete ()

Deletes data from the content provider. Using the URI parameter to determine which table data is deleted, the selection and Selectionargs parameters are used to constrain which rows are deleted, and the number of rows deleted is returned as a return value.

(6) GetType ()

Returns the corresponding MIME type based on the content URI passed in.

Let's do the actual.

Then "Android SQL statement Implementation of database additions and deletions" in the article, the new Persondbprovider in the Src\com\wuyudong\db directory, and inherit from ContentProvider

The code in Persondbprovider.java is as follows:

 Packagecom.wuyudong.db;ImportAndroid.content.ContentProvider;Importandroid.content.ContentValues;ImportAndroid.database.Cursor;ImportAndroid.net.Uri; Public classPersondbproviderextendsContentProvider {@Override Public BooleanonCreate () {//TODO auto-generated Method Stub        return false; } @Override PublicCursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) { //TODO auto-generated Method Stub        return NULL; } @Override PublicString getType (Uri uri) {//TODO auto-generated Method Stub        return NULL; } @Override Publicuri insert (URI uri, contentvalues values) {//TODO auto-generated Method Stub        return NULL; } @Override Public intDelete (Uri Uri, String selection, string[] selectionargs) {//TODO auto-generated Method Stub        return0; } @Override Public intupdate (URI Uri, contentvalues values, String selection, string[] selectionargs) {//TODO auto-generated Method Stub        return0; }}

Add the following code to the Androidmanifest.xml:

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.wuyudong.db"Android:versioncode= "1"Android:versionname= "1.0" >    <InstrumentationAndroid:name= "Android.test.InstrumentationTestRunner"Android:targetpackage= "Com.wuyudong.db" />    <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <uses-libraryAndroid:name= "Android.test.runner" />        <ActivityAndroid:name= "Com.wuyudong.db.MainActivity"Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>    <provider  android:name= "Com.wuyudong.db.PersonDBProvider"  Android:authorities= "Com.wuyudong.db.personprovider" ></  Provider>                  </Application></Manifest>

About Android content providers

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.