Android Four components and intent

Source: Internet
Author: User

One, Android has four components: Activity, Service, Broadcastreceiver, ContentProvider.

1. Activity

Create a new activity through startactivity and pass the information through intent to the new activity.

If you need to return a value, use Startactivityforresult and override the Onactivityresult method. In the activity being invoked, use Setresult to pass the information back to the callback utility.

2. Service

Use StartService, Bindservice, Unbindservice, StopService to operate on the service. Passing information through intent.

These 4 methods call order and considerations that can be viewed logs Android service and Android service after executing unbind after performing bind problem again

3, Broadcastreceiver

Use Sendbroadcast, Registerreceiver, Unregisterreceiver.

Register is a method in Android.content.ContextWrapper, and activity and service inherit from Contextwrapper, so both activity and service can register receiver.

There are two ways to register receiver: You can not use Registerreceiver and Unregisterreceiver, Register receiver with Manifest.xml configuration, this registration method is called static registration, after the activity declaration period, Broadcastreceiver can still receive the information sent to it, if using Registerreceiver and Unregiste Rreceiver, which is dynamic registration, Broadcastreceiver's declaration cycle follows the activity or service's declaration cycle.

When the activity is destroyed, if the unregisterreceiver is not executed, the system will prompt you to forget to unregister.

Sendorderedbroadcast can send an ordered broadcast, each broadcast sets the priority attribute in Intentfilter, and the order of execution of the broadcast is guaranteed by the precedence, the higher the priority value (1000 to 1000) the greater the precedence.

4, ContentProvider

It is now only through EoE video that we learn how to invoke other components to provide information.

Second, Intent

Intent is the way that information is passed between components.

During this period of learning, most of the time the intent object is constructed using the intent (Context, <class?>) method, which can be used to precisely call the activity, Service, broadcast and other components. But the shortcomings are also obvious, for example, can not be issued in batches broadcast (inheritance may be achieved, need to practice to verify). For such requirements, can be implemented by intent (String action), defined in the manifest Intentfilter, multiple broadcast corresponding to the same action intentfilter, you can achieve the function of bulk send.

Android Four components and intent

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.