Android Development 3: Introduction and life cycle of four basic components

Source: Internet
Author: User

Android Development 3: Introduction and life cycle of four basic components

The four basic components of Android are Activity,service services, content provider contents providers, Broadcastreceiver broadcast receivers,.

Life cycle refers to process cycle – Create to end process process category foreground process: process visible process that is currently running in foreground: displayed in foreground, but the user is not interacting with the service process: a process that provides service to the user in a background process: This process is typically for caching mechanisms Components describes an activity (presentation layer) application in which an activity is usually a separate screen that shows that some controls can also listen and handle the user's events to respond. Activity uses view to implement the GUI in the application (the user interacts directly with the GUI and the application). The visible content within the Activity window is provided through the base Class View. Use the Activity.setcontentview () method to set the View object in the current activity.
Each view object controls a rectangular space within the window;
View is a hierarchical structure in which the layout properties in the Parent view are inherited by the quilt view;
The rectangular space represented by the child view object at the bottom of the view hierarchy is the place to interact with the user the activity's life state is in turn:
OnCreate
OnStart
Onrestart
Onresume
OnPause
OnStop
OnDestroy ================================================================= If you want to save some data in the page, you want to save it in OnPause (), Read in Onresume ().
In Android, the lifecycle of the Activity is delegated to the system for unified management. ================================================================= There are four loading modes for activity:1. Standard
mode startup mode, activity is created each time the activity is activated and placed in the task stack.
2. Singletop
If there is an instance of the activity at the top of the task, reusing the instance will create a new instance and put it on top of the stack, even if
The activity instance already exists, as long as it is not on top of the stack, the instance will be created.
3. Singletask
If an instance of the activity is already in the stack, the instance is reused (Onnewintent () of the instance is invoked). When reused, the instance is returned to the
Stack top, so the instance above it will be removed from the stack. If the instance does not exist in the stack, a new instance is created to be put into the stack.
4. SingleInstance
Create the activity instance in a new stack and have multiple apps share the activity instance in the stack. Once the activity of the model is changed, the actual
Example exists in a stack, and any application that activates the activity will reuse the instance in that stack, with the effect that multiple applications share an application,
Whoever activates the activity will go into the same app. Customizing view display steps in activity: 1, creating a new class, integrating from view Class 2, overriding the drawing method in view OnDraw () 3, creating a new class, integrating from Activity Class 4, overriding OnCreate (), initializing a custom view, Then set the user interface to this view object slot 1: Eat Dad's Baidu video tutorial, the tutorial inside the demo Run all OK, I local tried n this, run with the simulator will always prompt unfortunately,activity has stopped. Google finally found the problem: no new Paint () This example does not use the XML layout, the XML directory Baidu Bar, a lot, basically similar. Subsequent updates to the new MyView class inherit the view class:
Package Com.example.lesson1_helloandroid;import Android.content.context;import Android.graphics.canvas;import  Android.graphics.color;import Android.graphics.paint;import android.view.view;/** * This is a custom myview. * Requires at least overloaded construction methods and OnDraw methods * For custom view without its own unique properties, you can use it directly in the XML file * If you have your own unique attributes, you need to get the name of the custom attribute in the property file Attrs.xml in the constructor *  and set default values as needed, which are not defined in the XML file. * If you use custom attributes, you need to add a new schemas in the application XML file, * For example here is xmlns:my= "http://schemas.android.com/apk/res/demo.view.my" * where xmlns after "My" is a custom attribute prefix, res is our custom View package * @author Administrator * */public class MyView extends view{//set brush paint paint;//structure Create function Public MyView (context context) {super (context);//TODO auto-generated constructor stubpaint = new Paint ();//Set Color font size ET Paint.setcolor (color.white);p aint.settextsize ();p Aint.setantialias (true); protected void OnDraw (canvas canvas) {//custom interface draws a rectangular box, draws a text Super.ondraw (canvas) in a rectangle, or//defines the canvas background color canvas.drawcolor ( Color.Blue);//Draw a rectangle on the canvas canvas.drawrect (10,10,110,110, paint);//The text is drawn by a brush on a rectangular frame canvas.drawtext ("Your SisterThe Cock silk ",", "," 

=================================================== is displayed through the XML layout (not tested, but the idea is roughly the same) =================================== ===

The corresponding properties file:
<?xml version= "1.0" encoding= "Utf-8"? ><resources><declare-styleable name= "MyView" ><attr name= "TextColor" format= "color"/><attr name= "TextSize" format= "Dimension"/></declare-styleable></ Resources>

To use in a layout file:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "   xmlns:my=" http://schemas.android.com/apk/res/demo.view.my "     android:orientation=" vertical "    android:layout_width= "fill_parent"    android:layout_height= "fill_parent"    >    < Demo.view.my.MyView    android:layout_width= "fill_parent"    android:layout_height= "Wrap_content"     my: Textcolor= "#FFFFFFFF"     my:textsize= "22DP"    /></linearlayout>
=============================================================================================================

Activity class:

Package com.example.lesson1_helloandroid;//represents the name of the packet import Android.os.bundle;import android.app.activity;// is an activity pack, each Android activity needs to inherit activity class import Android.view.menu;//public class Lesson1_helloandroid extends activity {@ Override//oncreate is an overloaded function that implements the procedure that is executed by the application creation in this function. protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//Set Current view (view)// The method of setting is to use a file, which therefore determines what is contained in the view. The current setting represents the use of activity_lesson1_hello_android.xml files from the res/layout/directory//setcontentview (R.layout.activity_lesson1__hello _android); MyView MyView = new MyView (this); This.setcontentview (MyView);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (r.menu.lesson1__hello_android, menu); return true;}}

Androidmanifest.xml (Note: It seems to give permission can be run, the default can be, I am because the error has been added permissions .... But the final question is because the video of the father is not new Paint ())

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "    package=" com.example.test "    android:versioncode=" 1 "    android:versionname=" 1.0 ">    < USES-SDK        android:minsdkversion= "android:targetsdkversion="        />    <application        android : allowbackup= "true"        android:icon= "@drawable/ic_launcher"        android:label= "@string/app_name"        Android : theme= "@style/apptheme" >        <activity            android:name= "com.example.test.MainActivity"            android: Label= "@string/app_name" >            <intent-filter>                <action android:name= " Android.intent.action.MAIN "/>                <category android:name=" Android.intent.category.LAUNCHER "/>            </intent-filter>        </activity>    </application></manifest>
Running Results: Service services (Programs with a longer life cycle but no user interface):

To start a new service, or to pass a new instruction to an existing service, you can call the following two methods:

1.context.startservice ()
2.context.bindservice ()

The broadcast receiver service sends a broadcast intent (all registered intenfilter with matching BROADCASTRECEIV will be activated) and can call the following three ways:

1.context.sendbroadcast ()

2.context.sendorderbroadcast ()

3.context.sendstickbroadcast ()

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.

Cond..... http://mobile1.riaos.com/?p=2027820

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.