Android 开发从入门到精通 - Android 应用程序 — 概述
来源:互联网 发布:网络用语黑洞英语 编辑:程序博客网 时间:2024/06/11 21:12
我们来看看 Android 应用程序的四种主要类型:活动、服务、接收器和 ContentProvider
。我们还要看看显示用户界面(UI)元素的视图。
活动是最常用的 Android 应用程序形式。活动在一个称为视图 的类的帮助下,为应用程序提供 UI。视图类实现各种 UI 元素,比如文本框、标签、按钮和计算平台上常见的其他 UI 元素。
一个应用程序可以包含一个或多个活动。这些活动通常与应用程序中的屏幕形成一对一关系。
应用程序通过调用 startActivity()
或 startSubActivity()
方法从一个活动转移到另一个活动。如果应用程序只需 “切换” 到新的活动,就应该使用前一个方法。如果需要异步的调用/响应模式,就使用后一个方法。在这两种情况下,都需要通过方法的参数传递一个 intent。
由操作系统负责决定哪个活动最适合满足指定的 intent。
回页首
与其他多任务计算环境一样,“在后台” 运行着一些应用程序,它们执行各种任务。Android 把这种应用程序称为 “服务”。服务是没有 UI 的 Android 应用程序。
接收器是一个应用程序组件,它接收请求并处理 intent。与服务一样,接收器在一般情况下也没有 UI 元素。接收器通常在 AndroidManifest.xml 文件中注册。清单 2 是接收器代码的示例。注意,接收器的类属性是负责实现这个接收器的 Java 类。
package com.msi.samplereceiver;import android.content.Context;import android.content.Intent;import android.content.IntentReceiver;public class myreceiver extends IntentReceiver {public void onReceiveIntent(Context arg0, Intent arg1) {// do something when this method is invoked.}}
回页首
ContentProvider
是 Android 的数据存储抽象机制。我们以移动设备上常见的一种数据为例:地址簿或联系人数据库。地址簿包含所有联系人及其电话号码,用户在使用手机时可能需要使用这些数据。ContentProvider
对数据存储的访问方法进行抽象。ContentProvider
在许多方面起到数据库服务器的作用。对数据存储中数据的读写操作应该通过适当的 ContentProvider
传递,而不是直接访问文件或数据库。可能还有 ContentProvider
的 “客户机” 和 “实现”。
下一节介绍 Android 视图,这是 Android 在移动设备屏幕上显示 UI 元素的机制。
回页首
Android 活动通过视图显示 UI 元素。视图采用以下布局设计之一:
- LinearVertical
- 后续的每个元素都排在前一个元素下面,形成一个单一列。
- LinearHorizontal
- 后续的每个元素都排在前一个元素右边,形成一个单一行。
- Relative
- 后续的每个元素相对于前一个元素有一定的偏移量。
- Table
- 与 HTML 表相似的一系列行和列。每个单元格可以包含一个视图元素。
选择一种布局(或布局的组合)之后,就可以用各个视图显示 UI。
视图元素由大家熟悉的 UI 元素组成,包括:
- Button
- ImageButton
- EditText
- TextView(与标签相似)
- CheckBox
- Radio Button
- Gallery 和 ImageSwitcher(用来显示多个图像)
- List
- Grid
- DatePicker
- TimePicker
- Spinner(与组合框相似)
- AutoComplete(具有文本自动补全特性的 EditText)
视图是在一个 XML 文件中定义的。清单 3 给出一个简单的 LinearVertical
布局示例。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Activity 1!" /><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Activity 1, second text view!" /><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Switch To Activity 2"id="@+id/switchto2"/> </LinearLayout>
注意,每个元素有一个或多个属于 Android 名称空间的属性。
下一节讨论如何获取 Android SDK 并在 Eclipse 环境中配置它。
- Android 开发从入门到精通 - Android 应用程序 — 概述
- Android 开发从入门到精通 - 用 Eclipse 开发 Android 应用程序
- Android 开发从入门到精通
- Android开发从入门到精通
- Android开发从入门到精通
- Android开发从入门到精通
- Android 开发从入门到精通
- Android 开发从入门到精通
- Android 开发 NDK从入门到精通
- Android 开发从入门到精通
- android 开发从入门到精通
- Android 开发 NDK从入门到精通
- android BLE从入门到精通开发
- android BLE从入门到精通开发
- Android 开发从入门到精通 - Android 开发简介
- Android 开发从入门到精通-Android开发
- Android 从入门到精通
- Android 开发 之 JNI入门 - NDK从入门到精通
- Linux下Tomcat的安装
- VC之字符串转换_BSTR/LPSTR/LPWSTR/Char (ZZ)
- CSS可以设置圆角的方块
- [转]国内WEB设计资源、教程、素材、站长资讯交流类网站汇集
- CSS教程
- Android 开发从入门到精通 - Android 应用程序 — 概述
- Python Socket
- 3ds max 超过2G内存报错的临时性解决方案
- 功能点分析法 FPA 总结
- 配置开发支持高并发TCP连接的Linux应用程序全攻略
- Android 开发笔记
- (召集)IBM欢迎您的加入!
- 关于数据库操作
- 把应用程序加载到WINDOWS CE内核的方法