Data sharing ContentProvider between applications of Android system programming introduction series

The content provider ContentProvider, together with the interface activity, service and broadcast receiver mentioned above, are called the four components of Android. They all need self-defined subclasses to inherit the above component classes and register statically in the manifest file or dynamically in the logical code before they can be used normally.

The android.content.contentprovider content provider class is a component class used to share data content with other applications. In the article file sharing between applications, the fileprovider file provider registered for Android 7.0 or above is the use case of the defined ContentProvider.

Before creating a ContentProvider content provider, first ensure that there is data to be shared. This data may come from user interaction or from the results returned by the remote server. After receiving a series of data, the application can adopt different data local saving strategies according to different data structures, including SharedPreferences in the form of ordinary key value pairs, sqlitedatabase in object structure, or ordinary binary file.

After that, you can create a custom content provider class inherited from android.content.contentprovider, and complete the reading and writing of locally saved data in the relevant methods of its implementation. Other applications can read and write the data to be shared by the application by accessing the relevant methods of the content provider.

Since the content provider ContentProvider interacts with other application classes by means of the path locator URI class, in the created content provider ContentProvider, the relevant URIs allowed to be accessed by this class must be defined and authorized first.

In the created content provider ContentProvider, you need to locate the matching class with the help of android.content.urimatcher path. You can convert the incoming URI type into a common int type to facilitate matching and discrimination within the content provider.

Generally, the urimatcher object defined as a static global variable type within the content provider has only one parameter construction method urimatcher (int code), and the parameter value is urimatcher.no_ MATCH=-1。

After that, you can call the adduri (string authority, string path, int code) method of the static object to add a pair of path locators to match the int value. The parameter authority is a fixed authorization string, and the parameter value must also be changed when registering the content provider ContentProvider. The parameter path is the sub path appended to the authority parameter to distinguish different matching paths. The resulting data URI format is content://authority/path/id 。 The parameter code is the bound int value, which is the same as each URI format content://authority/path The path corresponding to URI.

Invalid Sign

The following methods must be implemented in the ContentProvider to manipulate local data.

Like other components, the system will call this method only when the content provider is first loaded and created by the system. Relevant global variables can be initialized in this method, but time-consuming operations are not recommended.

Called by other applications that use the content provider. The incoming parameter URI specifies a data location as a path locator and returns the MIME type corresponding to the data. Usually, when the URI points to a file, the corresponding MIME type is returned according to different file types. In other cases, return null.

Called by other applications that use the content provider. Insert a piece of data under the path specified by the specified parameter URI, and its content format is content://authority/path 。 The parameter values is the data content to be inserted, which is similar to the insertion method in the database. The data is saved in the form of key value pairs. Finally, after the data is successfully inserted, the path locator URI object corresponding to the data needs to be returned.

Called by other applications that use the content provider. Parameter URI specifies the location of the data to be deleted when the content format is content://authority/path/id Type, the specified location data will be deleted, and the remaining two parameters are invalid; When the content format is content://authority/path Type, the matching data found according to other parameters under the path will be deleted. If the remaining parameters are null, all data under the path will be deleted. The parameter selection is the specified deletion condition, which conforms to the SQL statement, but the variable parameter can be used? Instead, specify the specific parameter value in the subsequent parameter; The parameter selectionargs is the parameter value array, and its length is the same as that in the parameter selection? Consistent with the quantity. Finally, it returns the total number of data pieces deleted by the data tag of type int.

Called by other applications that use the content provider. The parameter URI specifies the location of the data to be modified, which is similar to the parameter I in the delete data method above, when the content format is content://authority/path/id Type, its specified location data will be updated, and the last two parameters are invalid; When the content format is content://authority/path Type, the matching data under the path will be updated according to the last two parameters. If the last two parameters are null, all data under the path will be updated. Parameter values is the data content to be inserted, which is similar to parameter 2 in the add data method. Parameter selection and parameter selectionargs are also similar to parameter 2 and parameter 3 summarized in the delete data method. They can match qualified data. Finally, it returns the total number of modified data for the data tag of type int.

Called by other applications that use the content provider. Parameter URI is the specified query path, which is similar to parameter 1 in the data adding method. The format is content://authority/path 。 The remaining parameters are similar to those in the query method in the database, and can match the data according to the relevant contents. Finally, the data of android.database.cursor cursor pointer type is returned. You can also refer to the query results in the database.

The final customized content provider needs to be statically registered in the manifest file before it can be used in other locations. Use the < provider > < / provider > tag in the < Application > < / Application > tag to specify the content provider. In this tag, the Android: name attribute binds the global class name of the content provider defined in the code. The attribute Android: authorities specifies the authorization name, which is consistent with the authority parameter passed in by the add path matcher method of the urimatcher object in the code. The property Android: exported determines whether the content provider can be used by other applications by setting the boolean type value. In addition, there are other attribute values. Please refer to the official < provider > introduction.

In other applications or other locations of the current application, you can obtain the object of android.content.contentresolver content resolution class with the help of getcontentresolver () method of context object. Through the operation on the content parsing class object, the user-defined content provider is affected. There are a series of methods in the content resolution class contentresolver, which correspond to the addition, deletion, modification, query and other methods in the user-defined content provider ContentProvider implemented above, so I won't repeat them.

Generally, the definition scenario of content providers is to provide messages or contact information to other applications in SMS and call applications. The data accessor only needs to obtain the corresponding URI.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>