`
lukew
  • 浏览: 51607 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android应用程序剖析

阅读更多
Anatomy of an Android Application


There are four building blocks to an Android application:
  • Activity
  • Intent Receiver
  • Service
  • Content Provider

Not every application needs to have all four, but your application will be written with some combination of these.

Once you have decided what components you need for your application, you should list them in a file called AndroidManifest.xml. This is an XML file where you declare the components of your application and what their capabilities and requirements are. See the Android manifest file documentation for complete details.

Activity
Activities are the most common of the four Android building blocks. An activity is usually a single screen in your application. Each activity is implemented as a single class that extends the Activity base class. Your class will display a user interface composed of Views and respond to events. Most applications consist of multiple screens. For example, a text messaging application might have one screen that shows a list of contacts to send messages to, a second screen to write the message to the chosen contact, and other screens to review old messages or change settings. Each of these screens would be implemented as an activity. Moving to another screen is accomplished by a starting a new activity. In some cases an activity may return a value to the previous activity -- for example an activity that lets the user pick a photo would return the chosen photo to the caller.

When a new screen opens, the previous screen is paused and put onto a history stack. The user can navigate backward through previously opened screens in the history. Screens can also choose to be removed from the history stack when it would be inappropriate for them to remain. Android retains history stacks for each application launched from the home screen.

Intent and Intent Filters
Android uses a special class called an Intent to move from screen to screen. An intent describes what an application wants done. The two most important parts of the intent data structure are the action and the data to act upon. Typical values for action are MAIN (the front door of the activity), VIEW, PICK, EDIT, etc. The data is expressed as a URI. For example, to view contact information for a person, you would create an intent with the VIEW action and the data set to a URI representing that person.

There is a related class called an IntentFilter. While an intent is effectively a request to do something, an intent filter is a description of what intents an activity (or intent receiver, see below) is capable of handling. An activity that is able to display contact information for a person would publish an IntentFilter that said that it knows how to handle the action VIEW when applied to data representing a person. Activities publish their IntentFilters in the AndroidManifest.xml file.

Navigating from screen to screen is accomplished by resolving intents. To navigate forward, an activity calls startActivity(myIntent). The system then looks at the intent filters for all installed applications and picks the activity whose intent filters best matches myIntent. The new activity is informed of the intent, which causes it to be launched. The process of resolving intents happens at run time when startActivity is called, which offers two key benefits:

* Activities can reuse functionality from other components simply by making a request in the form of an Intent
* Activities can be replaced at any time by a new Activity with an equivalent IntentFilter


Intent Receiver
You can use an IntentReceiver when you want code in your application to execute in reaction to an external event, for example, when the phone rings, or when the data network is available, or when it's midnight. Intent receivers do not display a UI, although they may use the NotificationManager to alert the user if something interesting has happened. Intent receivers are registered in AndroidManifest.xml, but you can also register them from code using Context.registerReceiver(). Your application does not have to be running for its intent receivers to be called; the system will start your application, if necessary, when an intent receiver is triggered. Applications can also send their own intent broadcasts to others with Context.broadcastIntent().
Service

A Service is code that is long-lived and runs without a UI. A good example of this is a media player playing songs from a play list. In a media player application, there would probably be one or more activities that allow the user to choose songs and start playing them. However, the music playback itself should not be handled by an activity because the user will expect the music to keep playing even after navigating to a new screen. In this case, the media player activity could start a service using Context.startService() to to run in the background to keep the music going. The system will then keep the music playback service running until it has finished. (You can learn more about the priority given to services in the system by reading Lifecycle of an Android Application.) Note that you can connect to a service (and start it if it's not already running) with the Context.bindService() method. When connected to a service, you can communicate with it through an interface exposed by the service. For the music service, this might allow you to pause, rewind, etc.

Content Provider
Applications can store their data in files, an SQLite database, or any other mechanism that makes sense. A content provider, however, is useful if you want your application's data to be shared with other applications. A content provider is a class that implements a standard set of methods to let other applications store and retrieve the type of data that is handled by that content provider.

To get more details on content providers, see Accessing Content Providers.
分享到:
评论

相关推荐

    Android应用程序开发

    ### Android应用程序开发知识点详解 #### 一、Android概述 Android是一种基于Linux内核的开源移动设备操作系统,由Google公司和开放手机联盟领导及开发。它主要用于触摸操作的手持设备上,如智能手机和平板电脑等...

    Android应用程序开发以及背后的设计思想深度剖析

    这篇文章将逐步揭示Android应用程序背后的设计思想,从基本的 APK 文件结构到更复杂的系统组件和编程模型。 首先,让我们深入了解 APK 文件。APK 是 Android 应用程序的基本打包格式,实质上是一个被压缩的 ZIP ...

    Android应用程序安全分析技术研究.pdf

    本文中提出的 Android 应用程序安全分析技术思路结合了静态分析和动态分析技术的优缺点,提出了一种静态分析和动态分析相结合的 Android 应用程序安全分析技术思路。该方法首先在静态分析阶段,采用基于 D-S 证据...

    Android 的应用程序结构分析:HelloActivity

    本文将以“HelloActivity”为例,详细剖析Android应用程序的组成及其内部结构,帮助读者深入掌握Android应用的构建原理。 #### 二、HelloActivity概述 **HelloActivity** 是一个经典的入门级示例程序,旨在展示...

    android桌面应用程序

    在 Android 桌面应用程序中,Home 分析目录是非常重要的组件之一,负责管理桌面上的图标、快捷方式、widget 等元素。Home 分析目录的主要功能包括: 1. 桌面图标管理:Home 分析目录负责管理桌面上的图标,包括添加...

    《Android应用程序开发》源代码

    在Android应用程序开发中,源代码是开发者实现功能、构建用户界面和管理应用程序逻辑的核心部分。这份"《Android应用程序开发》源代码"包含了官方未修改的示例,为初学者和经验丰富的开发者提供了一个深入理解...

    Android应用程序开发以及背后的设计思想深度剖析PDF

    在Android应用程序开发中,开发者需要理解并掌握一系列关键知识点,这些知识点构成了应用开发的基础,并且与设计思想密切相关。本文将深入探讨这些核心概念,旨在帮助开发者不仅能够编写代码,更能理解其背后的逻辑...

    android应用程序开发源码40例

    【标题】"Android应用程序开发源码40例"是一份包含多个实际项目源代码的集合,旨在帮助开发者深入了解Android应用的构建过程。这些源码涵盖了各种功能和应用场景,是学习和提升Android编程技能的理想资源。 【描述...

    android应用程序小例子

    在Android应用程序开发中,我们经常会遇到各种各样的小例子,这些例子可以帮助开发者更好地理解和掌握Android SDK中的各种功能和API。本篇文章将详细探讨"android应用程序小例子"这一主题,涵盖从基础概念到实际应用...

    88个经典Android应用程序打包下载(源代码).zip

    这个压缩包"88个经典Android应用程序打包下载(源代码).zip"包含了88款Android应用的源代码,为开发者提供了宝贵的参考资料。这些应用可能涵盖各种类型,如社交、游戏、工具、音乐、视频等,通过研究它们,开发者...

    Android应用程序开发源代码

    在Android应用程序开发中,源代码是开发者的心血结晶,它揭示了应用的内部工作机制和逻辑。这份"Android应用程序开发源代码"的压缩包文件显然包含了完整的Android应用开发流程中的所有关键部分,对于初学者和有经验...

    《Android应用程序安全》PDF

    《Android应用程序安全》这本书深入探讨了Android应用开发中的安全问题,涵盖了从代码层面到系统级别的各种防护措施。作为一本专业的IT资源,它旨在帮助开发者、安全工程师和移动应用研究人员理解并解决Android应用...

    android应用程序基础

    ### Android应用程序基础 #### 1....通过以上分析,我们可以看出Android应用程序的基础结构以及如何利用这些组件来构建完整的应用。开发者需要熟悉这些基础知识,以便更好地掌握Android开发的核心技能。

    Android应用程序源代码

    Android应用程序源代码由一系列的文件组成,这些文件包含了程序的逻辑、界面设计、资源管理等核心部分。以下是对Android应用程序源代码相关知识点的详细说明: 1. **项目结构**:一个典型的Android应用源代码结构...

    Android应用程序开发宝典-基于TE&OK6410;.pdf

    此文档不仅覆盖了基本的Android应用程序开发流程,而且还提供了具体的案例分析和技术指导,使读者能够快速理解和应用这些知识。 #### 二、Android系统应用环境搭建 **2-1 UBUNTU10.10上编译ANDROID源码** - **2-1...

    Android应用程序“计算器”

    【Android应用程序“计算器”】 Android应用程序“计算器”是基于谷歌Android操作系统开发的一款实用工具,它为用户提供基础到高级的计算功能,以便在移动设备上进行数学运算。Android平台以其开放性和丰富的开发...

    AndroidStudio实战快速高效地构建Android应用 AndroidStudio 高清完整带目录书签 PDF AndroidStudio实战

    Android Studio作为Google官方推荐的Android应用程序开发集成开发环境(IDE),其功能强大且不断更新,是每一个Android开发者必备的工具。 本书涵盖了从Android Studio的基础操作到高级特性的全面内容,包括但不...

    88个经典Android应用程序打包下载

    这个压缩包包含了88个经典Android应用程序的源代码,是学习和研究Android开发不可多得的资源。 首先,我们可以从这些源码中学习到基础的Android组件使用,如Activity、Service、BroadcastReceiver和ContentProvider...

Global site tag (gtag.js) - Google Analytics