In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor gives you a detailed introduction of "how to create your own content provider in Android". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to create your own content provider in Android" can help you solve your doubts.
To create a content provider
As mentioned earlier, if you want to implement the ability to share data across programs, the officially recommended way is to use a content provider, which can create your own content provider by creating a new class to inherit ContentProvider. There are six abstract methods in the ContentProvider class, and we need to rewrite all of them when we inherit it with a subclass. The new MyProvider inherits from ContentProvider. The code is as follows:
Public class MyProvider extends ContentProvider {@ Overridepublic boolean onCreate () {return false;} @ Overridepublic Cursor query (Uri uri, String [] projection, String selection, String [] selectionArgs, String sortOrder) {return null;} @ Overridepublic Uri insert (Uri uri, ContentValues values) {return null;} @ Overridepublic int update (Uri uri, ContentValues values, String selection, String [] selectionArgs) {return 0;} @ Overridepublic int delete (Uri uri, String selection, String [] selectionArgs) {return 0;} @ Overridepublic String getType (Uri uri) {return null;}}
I believe you are already very familiar with most of these six methods. Let me give you a brief introduction.
1. OnCreate ()
Called when the content provider is initialized. Operations such as database creation and upgrade are usually completed here. Returning true indicates successful initialization of the content provider, while returning false indicates failure. Note that the content provider is initialized only if there is a ContentResolver trying to access the data in our program.
2. Query ()
Query data from the content provider. The uri parameter is used to determine which table to query, the projection parameter is used to determine which columns to query, the selection and selectionArgs parameters are used to constrain which rows of the query, the sortOrder parameter is used to sort the results, and the query results are stored and returned in the Cursor object.
3. Insert ()
Add a piece of data to the content provider. Use the uri parameter to determine which table to add to, and the data to be added is stored in the values parameter. When the addition is complete, a URI is returned to represent the new record.
4. Update ()
Update existing data in the content provider. Use the uri parameter to determine which table to update the data in, the new number is saved in the values parameter, the selection and selectionArgs parameters are used to constrain which rows are updated, and the number of affected rows is returned as a return value.
5. Delete ()
Delete data from the content provider. The uri parameter is used to determine which table data is deleted, the selection and selectionArgs parameters are used to constrain which rows are deleted, and the number of deleted rows is returned as a return value.
6. GetType ()
The corresponding MIME type is returned based on the content URI passed in. As you can see, almost every method has the parameter Uri, which is passed when calling the add, delete, modify and search method of ContentResolver. Now we need to parse the incoming Uri parameters to analyze the tables and data that the caller expects to access.
In retrospect, a standard content URI is written as follows:
Content://com.example.app.provider/table1
This means that the caller expects to access the data in the table1 table of the com.example.app application. In addition to this, we can also add an id after the content URI, as shown below:
Content://com.example.app.provider/table1/1
This means that the caller expects to access the data with an id of 1 in the table1 table of the com.example.app application. There are only the above two main formats of content URI. Ending with a path indicates that you expect to access all the data in the table, and ending with id means that you expect to access the data in the table with the corresponding id. We can use wildcards to match the content URI of these two formats respectively, as follows.
1. *: indicates that any character of any length is matched
2. #: represents a number that matches any length, so a content that can match any table can be written in URI format:
Content://com.example.app.provider/*
A content that can match any row of data in the table1 table can be written in URI format as:
Content://com.example.app.provider/table1/#
Then, with the help of the class UriMatcher, we can easily implement the function of matching content URI. UriMatcher provides an addURI () method that takes three parameters and passes in permissions, paths, and a custom code, respectively. In this way, when you call the match () method of UriMatcher, you can pass in a Uri object, and the return value is some custom code that matches the Uri object. With this code, we can determine which table the caller expects to access. Modify the code in MyProvider as follows:
Public class MyProvider extends ContentProvider {public static final int TABLE1_DIR = 0x public static final int TABLE1_ITEM = 1x public static final int TABLE2_DIR = 2x public static final int TABLE2_ITEM = 3x static UriMatcher uriMatcher; static {uriMatcher = new UriMatcher (UriMatcher.NO_MATCH); uriMatcher.addURI ("com.example.app.provider", "table1", TABLE1_DIR); uriMatcher.addURI ("com.example.app.provider", "table1/#", TABLE1_ITEM) UriMatcher.addURI (com.example.app.provider, table2, TABLE2_ITEM); uriMatcher.addURI (com.example.app.provider, table2/#, TABLE2_ITEM);}. @ Overridepublic Cursor query (Uri uri, String [] projection, String selection, String [] selectionArgs, String sortOrder) {switch (uriMatcher.match (uri)) {case TABLE1_DIR:// query all data in table1 Table break;case TABLE1_ITEM:// query single data in table1 Table break;case TABLE2_DIR:// query all data in table2 Table break;case TABLE2_ITEM:// query single data break;default:break; in table2 Table}... }. }
You can see that four integer constants have been added to MyProvider, where TABLE1_DIR means to access all data in the table1 table, TABLE1_ITEM means to access a single piece of data in the table1 table, TABLE2_DIR means to access all the data in the table2 table, and TABLE2_ITEM means to access a single piece of data in the table2 table. Then we create an instance of UriMatcher in the static code block and call the addURI () method to pass in the URI format of the content we expect to match. Note that the path parameters passed here can use wildcards. Then when the query () method is called, the incoming Uri object is matched through the match () method of UriMatcher, and if it is found that something in the UriMatcher URI format successfully matches the Uri object, the corresponding custom code is returned, and then we can determine what data the caller expects to access.
The above code only takes the query () method as an example to demonstrate, in fact, the implementation of insert (), update (), delete () is similar, they all carry the parameter Uri, and then also use the match () method of UriMatcher to determine which table the caller expects to visit, and then operate on the data in the table accordingly.
In addition, there is another method that you will be familiar with, that is, the getType () method. It is a method that all content providers must provide to get the MIME type corresponding to the Uri object. The MIME string corresponding to a content URI is mainly composed of three parts, which are formatted as follows by Android.
1. Must start with vnd.
two。 If the content URI ends with a path, it is followed by android.cursor.dir/,. If the content URI ends with id, it is followed by android.cursor.item/.
3. Finally, connect the vnd...
So, for the content URI of content://com.example.app.provider/table1, it corresponds to the MIME
The type can be written as:
Vnd.android.cursor.dir/vnd.com.example.app.provider.table1
For the content URI of content://com.example.app.provider/table1/1, the corresponding MIME type can be written as follows:
Vnd.android.cursor.item/vnd. Com.example.app.provider.table1
Now we can continue to refine the content in MyProvider, this time to implement the logic in the getType () method, the code is as follows:
Public class MyProvider extends ContentProvider {. @ Overridepublic String getType (Uri uri) {switch (uriMatcher.match (uri)) {case TABLE1_DIR:return "vnd.android.cursor.dir/vnd.com.example.app.provider.table1"; case TABLE1_ITEM:return "vnd.android.cursor.item/vnd.com.example.app.provider.table1"; case TABLE2_DIR:return "vnd.android.cursor.dir/vnd.com.example.app.provider.table2" Case TABLE2_ITEM:return "vnd.android.cursor.item/vnd.com.example.app.provider.table2"; default:break;} return null;}} here, this article "how to create your own content provider in Android" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it. If you want to know more about related articles, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.