Android contentprovider uses sqlitehelper to share data

Source: Internet
Author: User

There are many examples of using contentprovider to share data and files on the Internet. I will list them one by one. I will not bother with what others have written:

Important points:

1. contentprovider basically does not need to interact with its objects. The system will automatically wake up all registered providers.

2. The oncreate method of contentprovider is not always called by the system. <many people on the Internet are misleading. I personally tested it. It will only be called when you first install this APK.>

  <Sqlitehelper oncreate method is not called!>

3. The oncreate method of sqlitehelper is the same. It is not the first time that an application is installed. It is called without the database. Instead, it needs to be manually called through a new sqlitehelper object.

Getreadabledatabase

4. In my opinion, if you find anything inappropriate, please correct me. If you have time, add the flowchart and bring out the source code!

Http://blog.csdn.net/liuhe688/article/details/7050868 <tutorial 1>

Http://www.cnblogs.com/linjiqin/archive/2011/05/28/2061396.html & lt; tutorial 2 & gt;

Of course, other people's things are others'. After learning, You Have To summarize:

Certificate ------------------------------------------------------------------------------------------------------------------------------------

First, contentprovider is a bridge between data storage and retrieval between applications. Its role is to allow data sharing among multiple applications.

Use of contentprovider:

1. You must register in androidmanifest. xml.

<Provider Android: Name = ". provider. classname" <! -- Must be consistent with the provider class under the package --> Android: Authorities = "localshelves" <! -- Share required. The authorization must be unique. If multiple applications do not know which provider to use, the authorization must be unique. -->/>

2. Create a provider class

In the above 1 and 2 tutorials, we should make it very clear, not to mention, the general idea is

Package org. curiouscreature. Android. shelves. provider; public class youprovider extends contentprovider {private final static string local_db_name = "yourdbname. DB"; private final static int db_version = 1;
// Introduce mopenhelper to create the data table private sqliteopenhelper mopenhelper for the first time;
// Used to match future query items. Based on the matched query items, make the corresponding query Private Static final urimatcher uri_matcher;
// The authorized "Domain Name" must be unique and consistent with the private final static string authority = "localshelves" registered in androidmanifest ";
// The following lists the status codes of different query items: Private Static final int search = 1; Private Static final int names = 2; Private Static final int names_id = 3; static {uri_matcher = new urimatcher (urimatcher. no_match );
// Register each item before it can be used later and then match uri_matcher.adduri (authority, searchmanager. suggest_uri_path_query, search); uri_matcher.adduri (authority, searchmanager. suggest_uri_path_query + "/*", search); uri_matcher.adduri (authority, "name", names); uri_matcher.adduri (authority, "Name/#", names_id );} @ override public Boolean oncreate () {// todo auto-generated method stub log. W ("PDB", "run here ");
// This part has been around for a long time and everything has been done, that is, the table cannot be created. In the past, the oncreate () method of sqlitehelper will not be automatically called, and only getreadabledatabase () will be used () the mopenhelper = new databasehelper (getcontext (); mopenhelper. getreadabledatabase (); mopenhelper. getwritabledatabase (); Return true;} @ override public cursor query (URI Uri, string [] projection, string selection, string [] selectionargs, string sortorder) {// todo auto-generated method stub return NULL;} @ override
// Query type definition Public String GetType (URI) {Switch (uri_matcher.match (URI) {Case books: Return "Vnd. android. cursor. DIR/vnd.org. curiouscreature. provider. yourprovider "; Case book_id: Return" Vnd. android. cursor. item/vnd.org. curiouscreature. provider. yourprovider "; default: Throw new illegalargumentexception (" unknown Uri "+ URI) ;}@ override public URI insert (URI Uri, contentvalues values) {// todo auto-generated method stub return NULL;} @ override public int Delete (URI Uri, string selection, string [] selectionargs) {// todo auto-generated method stub return 0;} @ override public int Update (URI Uri, contentvalues values, string selection, string [] selectionargs) {// todo auto-generated method stub return 0;} // internal class, Java knowledge, you can go to this blog to view the Private Static class databasehelper extends sqliteopenhelper {/*** @ funtion constructor * @ Param context */Public databasehelper (context) {super (context, local_db_name, null, db_version); // todo auto-generated constructor stub} // The statement for creating a table is written here. The oncreate () method is not called because it is not a constructor, will not be automatically called. You need to manually call @ override public void oncreate (sqlitedatabase dB) {// todo auto-generated method stub log. W ("Table create tag", "yessss"); db.exe csql ("........ A few words are omitted here ");} @ override public void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {// todo auto-generated method stub db.exe csql ("Drop table if exists XXXX"); oncreate (db );}}}

3. Obtain the provider and perform the following operations:

Add the following code as needed and then perform related operations

ContentResolver cr = this.getContentResolver();  

OK. The basic steps are as follows: finish the job, go home for dinner, and fill in the flowchart when you are free!

 

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.