Android Simple development tutorial four: Android Application basics

Source: Internet
Author: User

One notable feature of the Android platform is "low coupling". Activity is one of the most basic user UI modules for Android apps. If you use Windows Form as a reference, the activity is equivalent to WinForm in Windows. Unlike Windows applications, the interaction between running an activity or an activity is achieved through a message. That is, if you want to initiate an activity or start another activity in an activity, it is triggered by sending a intent message instead of a show or load method that needs to be tuned to the form example, unlike the Windows WinForm application. The interaction between the activity is realized by intent message, and the coupling between modules is reduced to a great extent. This mechanism is similar to subscriber/publisher mechanism.

Another important feature of the Android platform is "reuse." An Android application can have multiple activity components. For example, Android uses the equivalent of a poker box, and each card in the box is a relatively independent activity. The Android app wants to be a stack of cards from poker, the first card to be pulled is the main activity of the Android app, and the main activity can invoke other activity (by sending intent messages), The triggered activity is like playing poker on the main activity. This creates an "activity" stack. Press back on the device to return to the previous activity as a browser. Every application on the Android phone is the same structure. "Reuse" means that when an Android application is running, it can trigger activity defined in other applications. For example, in Gtalk, you want to show a friend's position on the map. and GOOGLEMAP applications can display maps. Gtalk does not need to repeat the same code or for a similar activity. You can start the mapviewactivity in GoogleMap directly by intent messages.

The following figure shows the basic components of Android applications.

In addition to activity, Android can also implement service,service similar Windows Service, typically running in the background without a user interface. Brodcast receiver can be used to respond to some system messages. The basic functionality is somewhat similar to the pushregistry in Java me. For example, if you want to trigger your application when you receive a message, you can define a broadcast receiver in the manifest file of the Android application to trigger an activity.

As shown in the illustration above, the Application object is like a container in an Android application that can contain multiple activity, multiple service, or multiple broadcast Receiver. These activity,service,broadcast receiver are relatively independent, interacting with each other only through intent messages. Like the MIDlet jad file for Java me, each Android application has a manifest file with a file name fixed to Androidmanifest.xml. The Activity,service, broadcast receiver, defined in the Android application, need to be defined in this mainifest file to be invoked by this application or other application. Here it is to use the concept of Publisher/subscriber to illustrate. A activity,service, etc. if it can be called, you need to subscriber some kind of message in mainifest.

 <activity android:name=. HelloWorld "android:label=" @string/app_name "> <intent-filter> &L T;action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.categor" Y.launcher "/> </intent-filter> </activity> 

Above is the definition of the main activity in the HelloWorld in the Androidmanifest.xml, defines the corresponding class of the activity, and the intent-filter that can trigger the activity, (equivalent to subscriber some kind of message), but when the user clicks on the application icon, the Android operating system sends a intent message, and the Android system checks subscribe the activity of the intent message, Service or broadcast Receiver, if found, move the Activity,service or broadcast Receiver. For HelloWorld, the "Hello World" is displayed on the screen. In addition to the system can send Intent, the Android reference can also be initiated by StartActivity (Intent), StartService (Intent) to start other activity or service. Intent can carry incoming data (parameters). Even in the same application, it is necessary to transmit information through intent, which greatly reduces the coupling between the various modules in the application, thus allowing seamless replacement of one module in the application without affecting other parts. You may feel less WinForm (new Form1 ()) When you first touch Android. Show () comes directly to the convenience. However, the low coupling design of Android is very advantageous in terms of serviceability and scalability of applications. In addition, if you need to share some data between multiple activity, you can extend the application class implementation, and the variables defined in the application class can be accessed by all the activity in the application.

See a full set of tutorials: http://www.bianceng.cn/OS/extra/201301/35252.htm

Related Article

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.