Basic components of Android learning 06Android application

Source: Internet
Author: User
<span id="Label3"></p><p><p>An Android application can consist of several different components, the basic components of Android applications Include: Activity,service,broadcastreceiver,contentprovider and Intent.</p></p><p><p>Different components have different characteristics and their respective lifecycles, which are described below:</p></p><p><p></p></p><p><p>1.Activity components</p></p><p><p>Activity is the most common type of Android component, with each activity being a screen that provides the user with a visual interface for Interacting. An application can include one or more activity as needed, which typically inherits from the activity class under the Android.app package, and the operations between these activity are independent of each other.</p></p><p><p></p></p><p><p>1.1 Operating states (running State)</p></p><p><p>The activity is displayed at the front of the screen and has the focus to interact with the User's actions, such as providing information to the user, capturing the event that the user clicked on the button, and processing it.</p></p><p><p>1.2 Paused states (paused State)</p></p><p><p>At this point, the activity loses focus and is replaced by other operating state activity in the front of the screen display. If the state of the activity that conceals the suspended state activity does not cover the entire screen window or has transparency, the activity of the paused state is still visible to the user but cannot be interacted with</p></p><p><p>Inactive activity retains its state and other information about the member, and when the current System's memory is scarce, activity in the paused state is ended to get more resources.</p></p><p><p>1.3 Stop states (stopped State)</p></p><p><p>Inactive activity is not tightly focused, and is completely invisible, although other information such as status and members are retained, the inactive activity will end when the system needs it.</p></p><p><p>Activity life cycle Such as:</p></p><p><p></p></p><p><p></p></p><p><p>Activity displays can be declared in two ways, the first through an XML configuration file, and the second by setting the screen to an object that inherits from the view class</p></p><p><p>* Declaration via XML configuration file</p></p><p><p>The Activity's configuration file is located in the layout directory in the Res directory, and a profile is equivalent to a view container, which can add some built-in view under the Android platform, such as textview,imageview,button, You can also add a subclass object that inherits from the view class, and you can continue to add the view container</p></p><p><p>The layout file also specifies how the view object is arranged in the view container, such as linear layout (linearlayout), table layout (tablelayout), and so On.</p></p><p><p>In the new Activity class Code: Setcontentview (r.layout.main); Set the XML configuration file that you want to display</p></p><p><p>* Declaration of Sub-Class objects through view</p></p><p><p>Files configured with XML are easy to integrate with different view, but not enough for developers to be autonomous, especially when it comes to game programming, and often existing view in the Android system does not meet the Requirements. In this case, it is common to develop the user interface you want by inheriting a good extension view. Here's a table of Sub-Class objects to determine activity display content</p></p><p><p>Examples are relatively simple not to do demo</p></p><p><p>The activity Class code is as Follows:</p></p><p><p><br>public class Setactivityview extends Activity {<br><br>Mycontentview mycontentview;<br><br>@Override<br>public void OnCreate (Bundle Savedinstancestate) {<br>Super.oncreate (savedinstancestate);<br>Mycontentview = new Mycontentview (this);<br><br>Setcontentview (mycontentview);<br>}<br>}</p></p><p><p></p></p><p><p>The new view class code is as Follows:</p></p><p><p>public class Mycontentview extends view{</p></p><p><p>Public Mycontentview (context Context) {<br>Super (context);<br>}<br><br>@Override<br>protected void OnDraw (canvas Canvas) {//override callback method when the view class is drawn<br>Paint paint = new paint (); Create a brush<br>Paint.settextsize (18); Set Font size<br>Paint.setantialias (true); Set anti-aliasing<br>Paint.setcolor (color.red); Set Font Color<br>Canvas.drawtext ("this is achieved by inheriting a good extended view class implementation display", 0, +, paint); Draw Font to screen<br><br><br>}<br>}</p></p><p><p></p></p><p><p>2. Service Components</p></p><p><p>Unlike activity, the service does not provide a presentation layer to interact with the User. A service is an Android component that runs in the background, and when an application needs to do some computation or data processing that does not require a foreground display, it can start a service to complete, and each service inherits from the service class under the Android.app package.</p></p><p><p>The service is typically started by an activity or other context object, and when the service is started, the service will run in the background, even if the activity that started the service or the life cycle of other components has Ended. The service continues to run until the end of its own life Cycle. Each service should be declared in Androidmainfest.xml. There are two ways to start a service, and the corresponding life cycle is Different.</p></p><p><p>2,1 starting with the StartService method</p></p><p><p>When the system calls the StartService method, it calls its OnCreate method and OnStart method to start if the service has not yet Started. When other context objects call the StopService method, the service calls its own Stopselt or StopService method to stop the service operation</p></p><p><p>2.2 Starting with the Bindservice method</p></p><p><p>When the system calls the Bindservice method, if the service is not started, the OnCreate method is called to complete the initialization work, and then the service and the context object (such as Activity) are bound. When the bound context object is destroyed, the service that is tied to it also stops Running.</p></p><p><p></p></p><p><p>It is important to note that although the service is started in two ways, it can be bound to the context object regardless of how the service was Started.</p></p><p><p></p></p><p><p>3.Broadcast Receiver Components</p></p><p><p>Broadcast reciver, like service, does not provide a presentation layer that interacts with the user, which is a component responsible for receiving broadcast messages and reacting to Messages. There are many more such broadcasts in Android systems, such as low battery or low signal, and the system will send out a broadcast Notification.</p></p><p><p>If the application needs to respond to a broadcast message, it should register the corresponding broadcast receiver object, which inherits from the Broadcastreceiver class, which is located under the Android.content Package. This allows the application to accept and respond when a particular broadcast is issued by the system or another application, such as initiating activity</p></p><p><p>3.1 Broadcastreceiver How to publish a broadcast</p></p><p><p>It's easier to publish a broadcast, just create a intent object where you need it, encapsulate the memory of the information and the information used to filter it, By calling the Context.sendbroadcast method, the Context.sendorderedbroadcast method or Context.sendstickybroadcast method broadcasts the intent Object. Here are three ways to publish a broadcast:</p></p><p><p>typically, a intent that is sent out using Sendbroadcast or Sendstickybroadcast will execute its OnReceive method for all Broadcastreceiver that meet the CRITERIA. however, there is no guarantee that there are multiple orders that satisfy the Sendorderedbroadcast method to send out its execution onreceive methods. The intent, which is sent out by the Sendorderedbroadcast method, executes the OnReceive method according to the order of precedence Intentfilter set at Broadcastreceiver registration Time. The order in which the broadcastreceiver of the same priority executes the OnReceive method is not Guaranteed.</p></p><p><p>Sendstickybroadcast The main difference is that the intent will persist after sending, and will return the intent object directly to the newly registered receiver at a later call to the Registerreceiver registered Receiver.</p></p><p><p></p></p><p><p>3.2 Broadcastreceiver How to receive broadcasts</p></p><p><p></p></p><p><p>The broadcast entity that is published is intent, so it needs to be filtered by the Intentfilter object when it receives the Broadcast. Broadcastreceiver life cycle is relatively simple, in fact, there is only one callback method---onreceive, the method is called when the application receives the broadcast to itself, so the use of broadcastreceiver is relatively simple, The OnReceive method is also required to be properly rewritten, where appropriate to register the Broadcastreceiver can be</p></p><p><p>There are two ways to register Broadcastreceiver objects:</p></p><p><p>3.2.1 is declared in the Androidmainfest.xml File. The registration information is wrapped in the <receiver> </receiver> tab, and the filter rules are set in the <intent-filter> tab.</p></p><p><p>3.2.2 creates and sets the Intentfilter object in the Code. The Intentfilter object contains the filtering rules for the broadcast, and then calls the Context.registerreceiver method and the Context.unregisterreceiver method to register and unregister it where needed. If registered in this way, the Broadcastreceiver will cease to exist when the context object is Destroyed.</p></p><p><p></p></p><p><p></p></p><p><p>4 Content Provider Components</p></p><p><p>Content Provider differs greatly from other application components, and content Provider is primarily used for data sharing between different applications. Under the Android platform, each application has a separate memory space, and if an application needs to use data from another application, the ContentProvider object must be Used.</p></p><p><p>Each contentprovider inherits from the ContentProvider class under the Android.content package, and its function is to provide its own data to external applications, and the provided data can be stored as Android files, SQLite database files or other legitimate formats.</p></p><p><p>ContentProvider provides data and access to the data interface, the real access to the data is the Contentresolver object, the object can communicate with the ContentProvider object to achieve the purpose of sharing Data.</p></p><p><p></p></p><p><p></p></p><p><p>5.<strong>Intent--connection Components</strong></p></p><p><p></p></p><p><p>Of the 4 basic components above, except that the content Provider is activated through content Resolver, the other 3 components of Activity, Service, and broadcast Receiver are caused by Intent asynchronous message excitation The Living.</p></p><p><p>The Intent passes messages between different components, passing the request intent of one component to another Component. therefore, Intent is the object that contains the specific request Information.</p></p><p><p>For different components, The Intent contains different message content, and different components are activated differently, and different types of components have different ways of passing Intent.</p></p><p><p>Intent is a runtime binding mechanism that connects two different components during a Program's Run. With Intent, your program can send Android to a request or wish, and Android will choose the right component to handle the request, depending on the intended content.</p></p><p><p>Activating a new activity, or having an existing activity perform a new operation, can be called by calling the following two methods (these two rollup methods require an incoming Intent parameter called Activity Action Intent):</p></p><p><p>1. context.startactivity ()</p></p><p><p>2. Activity.startactivityforresult ()</p></p><p><p>To start a new service, or to pass a new instruction to an existing service, you can invoke the following two ways:</p></p><p><p>1. Context.startservice ()</p></p><p><p>2. Context.bindservice ()</p></p><p><p>L Send broadcast Intent (all registered BROADCASTRECEIV with matching intenfilter will be activated), you can call the following three Ways:</p></p><p><p>1. Context.sendbroadcast ()</p></p><p><p>2. Context.sendorderbroadcast ()</p></p><p><p>3. Context.sendstickbroadcast ()</p></p><p><p>Intent once issued, Android will find exactly one or more of the matching Activity, Service, or broadcastreceiver response. therefore, different types of Intent messages do not overlap, and broadcastintent messages are sent only to broadcastreceiver and never to the Activity or Server. Messages delivered with StartActivity () can only be sent to Activity, and Intent passed by StartService () can only be sent to the Service.</p></p><p><p>Basic components of Android learning 06Android application</p></p></span>

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.