Android Learning Note Five. 2. Other components

Source: Internet
Author: User

ContentProvider content provider. Is an app that uses Android to share data with third parties, contacts in Android, SMS (SMS Records), and so on, are provided out of this way.

2. Function:

The data provided in the application using ContentProvider is not used directly, but is implemented through contentresolver , Contentresolver contains query/insert/delete/ The Update method is used to manipulate the data, but it is internally called the ContentProvider implementation class to actually manipulate it.

examples of using ContentProvider to get SMS data

The use of the above process is divided into three steps:

① Specifies the access URI,

② the Contentresolver object by Getcontentresolver (), and then makes an additional deletion check.

③ gets the data/results to be processed.

The above is a brief description of what is used in the following:

①, Uri: The first place to use is when the intent to match, in fact, the URI is the data to match:

content://com.example.project:200/folder/subfolder/etc
\---------/  \---------------------------/ \---/ \--------------------------/
Scheme Host Port Path
\--------------------------------/
Authority

Now you should know what those attributes in data flag mean, look at the data flag.
<data android:host= "string"
Android:mimetype= "string"
Android:path= "string"
Android:pathpattern= "string"
android:pathprefix= "string"
Android:port= "string"
Android:scheme= "string"/>

There is a requirement for URIs when using ContentProvider: that is, the scheme must be content, for the reason of 2.

Note: The URI rules that are related to matching contentprovider are described in the custom ContentProvider section.

②, with query () as an example, the source of the method that the Contentresolver object will eventually invoke is shown below

Contentresolver Source

One can see that the first step is to detect if the incoming URI is empty, and the second is to detect the scheme of the incoming URI to determine if it is a content

Cquireunstableprovider Source (scheme for detecting URIs)

You can also see that the ContentProvider object is called for a real query operation. This is done to simplify the operation of the ContentProvider, and to conduct security testing.

③, additions and deletions the most important thing is to query the results of the cursor processing. A cursor is a collection of data that can fetch a row and then read the data in it. The class is an interface class.

2. Custom ContentProvider: This is a way for your project to provide data externally, and the best way is to create it with Android-provided templates, which are referenced by Android sample:Notepad. There are 3 files in this project that are more important and detailed:

1, Nodepad.java: This class defines some access-related properties, such as the URI, the name of the column, and so on. This class should be used in conjunction with ContentProvider.

Notepad.java

2, Notepadprovider.java: This class is a custom ContentProvider, need to inherit the ContentProvider class and at least implement the addition and deletion of the method and the construction method. In this example, you can see that the following classes are used:

①databasehelper: A database helper class that inherits from Sqliteopenhelper.

Databasehelper Source

②urimatcher: The purpose of this object is to specify the matching rules for the URI that matches the ContentProvider.

urimatcher Part Source code

③sqlitequerybuilder: This class is used to build query statements.

3, Androidmanifest.xml: Here to note is the provider declaration in the Manifest file section

manifest file

One of the more important is the declaration of android:authorities and permissions, in fact ContentProvider has very complex rights management, see: Http://iwillbemyself.blog.163.com/blog/static /1702232282012115104056917/,

It is also important for custom ContentProvider to notify the appropriate Contentobserver (content Watcher) when the database for the operation has changed.

Second, Broadcastreceiver broadcast receiver, broadcast is the way to pass events in Android, there are two aspects: Broadcast sender and broadcast receiver.

1, the broadcast sender can send two kinds of broadcasts: synchronous broadcast and orderly broadcast , the difference is

① the difference in the order of reception: Synchronous broadcast synchronous reception, ordered broadcasts can set the receive order by specifying the priority level

The difference between whether the ② can be interrupted: Because ordered broadcasts are sequential, it is possible to interrupt the broadcast and not allow subsequent receivers, while synchronous broadcasts cannot be interrupted.

2. Broadcast receiver: The specific content sent by the broadcast is an implicit intent object (because it is usually used for another application), and the receiver implements it in two steps.

The ① is configured in the manifest file,

② inherits the Broadcastreceiver object and overrides the OnReceive method, in which the functionality can be implemented.

Ordered broadcasts determine the order of receipt by specifying a priority, from -1000~1000,

① if the same priority is in the same app, the first one in the manifest file receives the

② if the priority in different apps is the same, the first installed app receives

Reference: http://blog.csdn.net/xiazdong/article/details/7768807,

Http://www.runoob.com/w3cnote/android-tutorial-broadcastreceiver.html

Android Learning Note Five. 2. Other components

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.