Use of Android shared data ContentProvider

Source: Internet
Author: User
Tags sqlite database

The role of Contentresolver and ContentProvider

ContentProvider is responsible for:

Organizing Application Data

Providing shared data to other applications

Contentresolver is responsible for

Get the data provided by ContentProvider

Delete and change the data

There are two ways to share your data with other applications: Create your own content Provider (that is, subclasses that inherit from content Provider) or add your own data to the existing content Provider,  The latter needs to ensure that the existing content provider is the same as its own data type and has permission to write to that content provider. (1) Set up data storage System

The data storage system can be arbitrarily determined by the developer, generally speaking, most of the content provider through the Android file storage System or SQLite database to establish their own data storage system.

(2) Extended ContentProvider class

       develop a subclass code that inherits from the ContentProvider class to extend the ContentProvider class, The main work in this step is to share the data wrapper and show it externally in a form that Contentresolver and   cursor objects can access. Specifically, you need to implement the 6 abstract methods in the ContentProvider class.        Cursor query (Uri uri, string[] projection, String selection, string[] Selectionargs, String SortOrder): Returns the queried data as a cursor object.         uri insert (URI uri, contentvalues values): Inserts a new data record into the Content provider, Contentvalues is the column name and column value mappings for the data record.         int update (URI uri, contentvalues values, String selection, string[] Selectionargs): Update data records that already exist in the content provider.         int Delete (URI Uri, String selection, string[] Selectionargs): from content Delete data records in provider.         String getType (URI uri): Returns the data (MIME) type in the content provider.         boolean onCreate (): Called when Content Provider is started.          The above methods will be called in the Contentresolver object, so a good implementation of these abstractions will provide a perfect external interface for contentresolver. In addition to implementing abstract methods, there are some things you can do to improve usability.           in short, your app defines a  contentprovider subclass, and implement the methods (in these methods to share their own data such as sqlite or files, etc.), the ContentProvider "contribution" out. Other apps will be able to make additions and deletions to this contentprovider operation.           defines a static constant for a URI type named Content_uri. A unique URI string must be defined for the constant object, and the general practice is to make the full name of the ContentProvider subclass a URI string, such as:  "Content://wyf.wpf.myprovider". (There may be many apps on Android that provide ContentProvider, each with its own URI, not the same name)         define the column names for each field. If the database storage system is SQLite database, the data table column name can take the column name of the table in the database. A "_id" field should be defined to uniquely identify a record, regardless of whether there are other fields in the datasheet that uniquely identify a record. The schema uses the "INTEGER PRIMARY KEY autoincrement" Automatic Update to typically define these column name strings as static constants, such as the "_id" field name defined as a static string object named "_id"   a value of "_id". (regardless of whether the source data you share to ContentProvider is SQLite or a file, it will be sorted into SQLite data form to provide other apps  )    (3) Declare the content provider component in the application's Adnroidmanifest.xml file.  

Creating good one content provider must be declared in the application's androidmanifest.xml, otherwise the content provider will not be visible to the Android system. (Emma, you good deeds, you have to say it, or else you can know) if a class named MyProvider extends the ContentProvider class, the code that declares the component is as follows: <provider name= " Wyf.wpf.MyProvider "authorities=" Wyf.wpf.myprovider "...../> <!--to <provider&gt Tag Add name, authorities property--where the Name property is the full class name of the ContentProvider subclass, the authorities property uniquely identifies a contentprovider. You can also set their operation permissions through Setreadpermission () and Setwritepermission ().         Of course, you can also add the android:readpermission or Android:writepermission attribute in the XML above to control its permissions. Note: Because ContentProvider can be called by different processes and threads, the methods that are implemented after inheriting ContentProvider must be thread-safe.

Reference resources: http://blog.csdn.net/berber78/article/details/17680697

Use of Android shared data ContentProvider

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.