Android Fine Note--contentprovider

Source: Internet
Author: User

Uncommon access Methods for provider

    • Batch Access: Accesses one of the ContentProvider modes, which allows you to perform multiple operations on provider at the same time, and supports simultaneous manipulation of multiple tables. When using, first construct a contentprovideroperation sequence, These operations are then distributed to Provdier using the Contentresolver.applybatch method, which is required to pass in the provider authority instead of the URI of a table when calling the Applybatch parameter, so that different operation queries can be made differently The table.
    • When your app A does not have permission to access a provider, you can access it by intent method. For example, select a contact, in a can send a intent, set the appropriate action and MIME type, and then launch such as the contact app for users to select a contact, with the selection, A onactivityresult in application A will receive the returned intent, the URI is obtained by GetData method, which points to the user's selected contact, and the contact application is set by the intent of the returnedFLAG_GRANT_READ_URI_PERMISSION以及FLAG_GRANT_WRITE_URI_PERMISSION两个分别给接收该intent的Activity赋予临时读/写该Uri指向的数据的权限,该权限直到该Activity finish之后就失效。另外一中方法就是通过发送Intent,并把需要的额外数据放入Intent中,然后交由另外一个具有该权限的应用去完成你想对该provider的操作。

User-defined MIME

    • For user-defined, also known as vendor-specific, MIME type, MIME type fixed format is Type/subtype, for custom mime, Its type is fixed to: for multiple lines: Vnd.android.cursor.dir, for single-line: Vnd.android.cursor.item, and for subtype type in compliance with the Vnd.<name>.<type > "In the form of a user-defined scope. Name must be globally unique, typically a company name or registration, and type should have a one-to-two relationship with the URI type. For example, for a provider with a authority of com.willhua.trains, the corresponding table is List1 and List2 respectively, then for Uri,content://com.willhua.trains/list1, The corresponding MIME type can be set to Vnd.android.cursor.dir/vnd.willhua.list1, for uri,content://com.willhua.trains/list2/ 5, its corresponding MIME type can be set to Vnd.android.cursor.dir/vnd.willhua.list2.

Design data Structure Tips

    • It is best to have an int column named _id as primary key, so that it can be used as a foreign key to map data from other tables, and if you want to link provider query results to the ListView, you must have _id columns ( basecolumns._id;
    • Use the Bolb (Binary large object) type to store data with large or varying data volumes , such as protocol buffer or JSON data
    • You can also use BLOBs to make a schema-independent table. In general, the PrimaryKey of an int, a column representing the MIME type, one or more blob columns, the data in the BLOB can be interpreted according to the MIME type, so that the data of different shchema types is stored in the same table. Conatactscontract.data is one such example of a table.

Uri

    • The content URI is divided into sections, starting with the schema part, the content://, which is the same, and then the provider authority, Contentrsolver is to determine which provider to use based on the known provider table in the URI section of the System (resolver), and to invoke the appropriate provider for the Query method; then the path or table And finally, if the URI that represents a single line is prefixed with the row ID.
    • It is important to note that the path or table part is not qualified to have only one fragment or hierarchy, such as CONTENT://COM.WILLHUA.BLOG/TABLEA/DATAA/8 or content://com.willhua.blog/tablea/ Datab, such a URI is also possible, where Tablea/dataa and Tablea/datab represent the path portion, and the table represented by Dataa and datab, such a way to better display the table's hierarchical relationship.
    • When using Urimatcher to determine the URI type using wildcards, the symbol ' * ' denotes a valid character of any length, ' # ' denotes a numeric character of any length

The realization of ContentProvider

    • Query (): You should return Getcount==0 's cursor instead of NULL for any items that do not match the query results. Null is returned only if an error occurred during the query. If the data store used by provider is not SQLite, you can use some of the cursor's subclasses, such as Matrixcursor, as the return value.
    • Insert (): The returned value remembers the URI of the table and then append the ID of the newly inserted row.
    • Delete (): Sometimes you might consider using a delete column to indicate whether the row was deleted instead of actually removing it from the table.
    • Update ()
    • OnCreate (): The execution time of this function should be reduced as much as possible to improve the response speed. If you are using SQLite as the data store, it is best to use sqliteopenhelper, which will delay the creation of the SQL table until the database is opened for the first time. When you first call Getwritabledatabase, the helper's OnCreate function is called.
    • GetType (): Used to return a MIME-formatted string to describe the data type that is returned based on the content URI parameter.
    • Getstreamtypes (): If provider provides files, you should implement this function. Used to return a sequence of MIME types corresponding to the content URI. For example, a provider provides. jpg,. png two pictures, if another app calls getstreamtypes the incoming filter parameter is "image/*", then Getstreamtypes should return {"Image/jpg", " Image/png "}. If there is no match for the filter, then NULL is returned.

Provider Permissions Control

  • For data that is placed on internal storage (internal), it is only accessible by the application by default. But if the data were to be used as a provider repository, and without additional permission controls on provider, then other applications would have full access to the data through provider, which would be equivalent to completely opening up a private data. This is unreasonable. Therefore, we should do some access control to provider as needed.
  • The permission settings for provider can be divided into three levels : Full permissions for the entire provider, using provider Android:permission control, which represents full read and write access to the entire provider , separate read or write permission, use provider's android:readpermission and android:writepermission to control the read and Write permission control of the whole provider respectively, and the permission control at the path level. Using the sub-elements <path-permission> control of provider, you can control the read-write, read, write or three permissions for a specific URI;
  • The priority of these three permissions increases progressively, that is, if a data is set at the same time the entire provider permission and path level permissions, then the external application must have the path level of permission to access this data.
  • Temporary permissions , using Provider's Android:granturipermission property (default false) and child elements <grant-uri-permission> To control temporary permissions to the entire provider or to a specific URI, respectively. Access with temporary permissions ignores the previously mentioned level three permission requirements. Temporary permissions on a URI can be retracted by content.revokeuripermission (). In intent, flag_grant_read_uri_permission and flag_grant_write_uri_permission are used to grant temporary read and write permissions to other applications.
  • If provider does not specify the required permissions, then other applications will not be able to access this provider. However, other components in the provider application are always ignoring the required permission requirements and have full read and write access to the provider.

Android Fine Note--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.