Anatomy of an Android Application

Publisher:CrystalSparkleLatest update time:2011-02-13 Source: 互联网 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

An Android application usually consists of the following four components:

  • Activity
  • Intent Receiver
  • Service
  • Content Provider

Not every app has all four of these, and your app may only use some of them.
Once you decide which components your app should include, you need to list them in the AndroidManifest.xml file. This is an XML file that contains the components defined by your app, their functions, and their prerequisites. You can visit
the Android manifest online documentation for a detailed description of this file.

Activity

Note: Proper nouns are not translated.

Activities are the most commonly used of the four components. In a program, an Activity is usually presented as a separate interface (screen). Each Aactivity is a separate class that extends and implements the Activity base class. This class displays a user interface composed of Views and responds to events. Most programs have multiple Activities. For example, a text messaging program has several interfaces: a contact list interface, a message writing interface, a message viewing interface, or a settings interface. Each interface is an Activity. Switching to another interface is loading a new Activity. In some cases, an Activity may return a value to the previous Activity - for example, an Activity that allows the user to select a photo will return the selected photo to its caller.

When a new screen is opened, the previous screen is paused and put into the history stack (screen switching history stack). Users can go back to the previously opened screens stored in the history stack. Screens with no screen value can also be deleted from the history stack. Android keeps all screens generated by the program running in the history stack: from the first screen to the last one.

Intents and Intent Filters

Android uses a special Intent class to switch interfaces. Intent describes what the program wants to do (intent, n. intention, purpose, intention). The two most important parts of the data structure are the action and the data processed according to established rules. Typical actions are MAIN (the entrance to the Activity), VIEW, PICK, EDIT, and so on. Data is represented by a URI. For example, to view someone's contact information, you need to create an Intent, use the VIEW action, and the data is a URI pointing to the person.

There is a related class called IntentFilter . An Intent is a request to do something; an IntentFilter describes what intents an Activity (or IntentReceiver below) can handle. An Activity that displays a person's contact information uses an IntentFilter, which means it knows how to handle VIEW operations that apply to that person's data. Activities use IntentFilters in the AndroidManifest.xml file.

Activity switching is done by parsing Intents. Use startActivity(myIntent) to start a new Activity. The system examines all installed programs' IntentFilters and finds the Activity that best matches myIntent. The new Activity receives the message from the Intent and is therefore started. The process of parsing Intents occurs when startActivity is called in real time, which has two benefits:

  • Activities can reuse the functionality of other components by issuing only one Intent request
  • Activities can be replaced at any time with a new Activity with an equivalent IntentFilter

Intent Receiver

When you want to execute some code related to external events, such as when a call rings, when the data network is available, or do something in the middle of the night, you may need to use an IntentReceiver . IntentReceivers have no UI, although they use NotificationManager to notify the user that something interesting has happened. IntentReceivers are declared in the AndroidManifest.xml file, but you can declare them using Context.registerReceiver() . Your program does not have to be running to wait for IntentReceivers to be called. When an IntentReceiver is triggered, the system will naturally start your program if necessary. Programs can also send their own Intent broadcasts to other programs through Context.broadcastIntent() .

Service

A service is a piece of code that has no UI and is always present in the system. The best example is a media player that plays songs from a playlist. In a media player application, there might be one or more activities that let the user select songs to play. However, playing songs in the background doesn't require any intervention from the activity, because the user wants to be able to switch to other interfaces while the music is playing. In this case, the media player activity needs to start a service with Context.startService() , which runs in the background to keep the music playing. The system keeps the background music playing service running until the media player is closed. ( Life Cycle of an Android Application provides more information about services.) Note that you can connect to a service (and start it if it's not already running) with the Context.bindService() method. Once connected, you can talk to the service through the interface provided by the service. For the music service, this provides functions such as pause and replay.

Content Provider

The application stores data in a SQLite database format file or in other valid devices. If you want other programs to be able to use your own program's data, Content Provider is very useful. Content Provider is a class that implements a series of standard methods. This class enables other programs to store and read data that can be processed by a certain Content Provider. For more information about Content Provider, please see Accessing Content Providers .

Reference address:Anatomy of an Android Application

Previous article:The application of GSM Modem measurement and control under WinCE system
Next article:Design of Embedded Network Server

Latest Test Measurement Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号