基于Android系统的多媒体播放器设计与实现外文翻译资料

 2022-09-16 10:39:40

Android Application Fundamentals

Android applications are written in the Java programming language. The Android SDK tools compile the code—along with any data and resource files—into an Android package, an archive file with an .apk suffix. All the code in a single .apk file is considered to be one application and is the file that Android-powered devices use to install the application.

Once installed on a device, each Android application lives in its own security sandbox:

The Android operating system is a multi-user Linux system in which each application is a different user.

By default, the system assigns each application a unique Linux user ID (the ID is used only by the system and is unknown to the application). The system sets permissions for all the files in an application so that only the user ID assigned to that application can access them.

Each process has its own virtual machine (VM), so an applications code runs in isolation from other applications,and is not affected by any other program code.

By default, every application runs in its own Linux process. Android starts the process when any of the applications components need to be executed, then shuts down the process when its no longer needed or when the system must recover memory for other applications.

In this way, the Android system implements the principle of least privilege. That is, each application, by default, has access only to the components that it requires to do its work and no more. This creates a very secure environment in which an application cannot access parts of the system for which it is not given permission.

However, there are ways for an application to share data with other applications and for an application to access system services:

Its possible to arrange for two applications to share the same Linux user ID, in which case they are able to access each others files. To conserve system resources, applications with the same user ID can also arrange to run in the same Linux process and share the same VM (the applications must also be signed with the same certificate).

An application can request permission to access device data such as the users contacts, SMS messages, the mountable storage (SD card), camera, Bluetooth, and more. All application permissions must be granted by the user at install time. That covers the basics regarding how an Android application exists within the system.

The rest of this document introduces you to:

1、The core framework components that define your application.

2、The manifest file in which you declare components and required device features for your application.

3、Resources that are separate from the application code and allow your application to gracefully optimize its behavior for a variety of device configurations.

Application Components

Application components are the essential building blocks of an Android application. Each component is a different point through which the system can enter your application. Not all components are actual entry points for the user and some depend on each other, but each one exists as its own entity and plays a specific role—each one is a unique building block that helps define your applications overall behavior.

A central feature of Android is that one application can make use of elements of other applications (provided those applications permit it). For example, if your application needs to display a scrolling list of images and another application has developed a suitable scroller and made it available to others, you can call upon that scroller to do the work, rather than develop your own. Your application doesnt incorporate the code of the other application or link to it. Rather, it simply starts up that piece of the other application when the need arises.

For this to work, the system must be able to start an application process when any part of it is needed, and instantiate the Java objects for that part. Therefore, unlike applications on most other systems, Android applications dont have a single entry point for everything in the application (no main() function, for example). Rather, they have essentialcomponents that the system can instantiate and run as needed. There are four different types of application components. Each type serves a distinct purpose and has a distinct lifecycle that defines how the component is created and destroyed.

Here are the four types of application components:

Activities

An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others. As such, a different application can start any one of these activities (if the email application allows it). For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture.

An application might consist of just one activity or, like the text messaging application just mentioned, it may contain several. What the activities are, and how many there are depends, of course, on the application and its design. Typically, one of the activities is marked as the first one that should be presented to the user when the application is launched. Moving from one activity to another is accomplished by having the current activity start the next one.

Each activity is given a default window to draw in. Typically, the window fills the screen, but it might be smaller than the screen and float on top of other windows. An activity can also make

剩余内容已隐藏,支付完成后下载完整资料


安卓应用程序基础

Android应用程序是用Java编程语言编写的。编译好的Java代码随同所有应用程序要使用的数据和资源文件一起,使用aapt tool打包成一个Android包,即一个后缀为.apk的归档文件。此文件是发布应用程序和在移动设备上安装应用程序的媒介;它是用户下载到他们设备上的文件。在一个.apk文件中的所有代码被认为是一个应用程序。

一旦安装在设备上,每个Android应用程序都生活在自己的安全沙箱:

Android操作系统是一个多用户的Linux系统,每个应用程序都是一个不同的用户。

默认情况下,系统给每个应用程序分配一个唯一的Linux用户ID(这个ID仅限系统使用,对应用程序是未知的)。系统为所有文件设置的权限在一个应用程序里,因此只有用户ID分配任务给应用程序才可以访问它们。

每一个进程都有自己的Java虚拟机(VM),所以应用程序代码运行时同其他程序代码分开,不受任何其他程序代码的影响。

默认情况下,每个应用程序都运行在自己的Linux进程。Android在应用程序中某段代码需要被执行时启动一个进程,不再使用或系统资源被其他应用程序请求时关闭这个进程。

通过这种方式,Android系统实现了最小特权原则。也就是说,每个应用程序,默认情况下,只对组件的访问它需要做的工作,没有更多的。这将创建一个非常安全的环境,使得一个应用程序不能访问没有被允许的系统的部分。

然而,有很多方法为一个应用程序与其他应用程序之间共享数据,和让应用程序访问系统服务:

可以安排两个应用程序共享相同的Linux用户ID,在这种情况下,他们可以访问对方的文件。为了节省系统资源,具有相同用户ID的应用程序也能运行在相同的Linux进程并且共享相同的VM(应用程序必须使用相同的证书)。

应用程序可以申请获取设备数据,如用户的联系人、短信、挂载存储(SD卡相机,蓝牙,或者更多。所有应用程序在安装时必须被用户授予权限。这涵盖了Android应用程序在系统中如何存在的基础。

本文的其余部分介绍:

1、定义应用程序的核心框架组件。

2、为应用程序申明组件元素和设备特性的清单文件。

3、为了不同的设备配置,独立于应用程序代码和允许应用程序优雅的优化其行为的资源。

应用程序组件(Application Components)

应用程序组件是Android应用程序的基本构建块。每个组件是一个不同的可以输入您的应用程序的入口系统。并不是所有的组件都是用户实际的入口点,一些组件是相互依赖的,但每一个都是作为实体存在并且扮演一个特定角色——每一个都是一个独特的帮助定义应用程序整体行为的构建块。

Android的一个中心功能就是应用程序可以使用其他应用程序的元素(在提供元素的应用程序允许的情况下)。例如,如果您的应用程序想要显示一系列带有滑屏效果功能的图片,然后某个应用程序刚好开发出了合适的滑屏的模块,并且同意共享,您就可以调用那个滑屏模块处理这些图片并显示出来,而不是您自己再去开发一个。您的应用程序并没有包含或链接到了其他的应用程序的代码。但是当请求发出后,您的应用程序确实可以简单的使用其他程序的部分功能。

为了达成这个过程,系统必须可以在应用程序的某个部分被请求时启动这个程序的进程,然后为那个部分的Java对象创建实例。因此不像其他操作系统上的应用程序那样,Android程序没有一个单一的应用程序入口(例如没有main()函数),然而他们含有当系统需要时创建实例的实质组件。有四种不同类型的应用程序组件。每种类型都有不同的目的,不同的生命周期,它定义了如何创建和销毁组件。

四种类型的应用程序组件:

Activities(活动)

一个activity 代表用户界面的一个独立屏幕。例如,一个邮件应用程序应该有一个 activity 用于显示新邮件列表,另一个 activity 用于撰写一封邮件,还有一个 activity 用于读取邮件。尽管所有 activity协同工作以构成邮件应用程序的用户体验,但彼此之间相对独立。因此,不同的应用程序能够从任何一个activity 启动 (只要邮件应用程序允许)。例如,用户需要分享一张照片,一个拍照应用程序能够启动邮件应用程序的activity 。

一个应用程序可能只含有一个activity,或者像上边提到的邮件应用程序一样含有几个。有几个activity,这些activity是什么样的,取决于您的应用程序是如何设计的。最典型的是将一个activity设计为第一个activity,应用程序被加载时呈现给用户。从一个activity转到另一个是通过在当前activity开始另一个来实现的。

每一个activity提供了一个用以绘制的默认窗口。典型的,窗口填满整个屏幕,但是它覆盖在其他窗口上时,会变得略小一点。一个activity可以使用多个窗口,例如,在窗口中央显示一个需要用户回应的弹出窗口,或者在用户选择屏幕上一个特定项目适时为他显示重要信息的窗口。

窗口的可见内容由一组view层提供——从View基类衍生出来的对象。每一个view控制窗口中的一块矩形区域。父view包含并组织子view的布局。页view(底层的view)绘制它们管理的矩形,并响应在“空白”的地方的动作。就是说,view就是活动和用户互动的地方。例如,一个view显示一个小图片,然后初始化用户点击这个小图片后的动作。Android有很多已做好的view供您选择——包括按钮,文本输入框,滚动条,菜单项,多选列表等。

通过使用Activity.setContentView()方法将一组view层放置在一个活动的窗口中。content view是在层的最底部的View对象。

Services(服务)

service 是在后台运行,执行长时间操作或者执行远程操作。 service 不提供用户界面。例如,当用户在另一个应用程序时,一个 service 可在后台播放音乐,或者是从网络上获取数据,而不阻断用户与当前activity的交互。其他组件,比如一个activity,为了与该service互动,可以启动或者绑定它。

一个主要的例子就是一个从列表中播放音乐的媒体播放器。播放器程序可能会有一个或几个允许用户选择希望播放的音乐然后显示播放的activity。但是音乐的回放过程本身不会使用一个activity。因为用户希望在切出播放器界面做别的事时音乐也能一直放下去。为了保持播放继续,activity可以启动一个服务在后台运行。然后即使启动这个服务的activity退出,音乐播放服务也能继续运行。

您可以连接到(绑定到)一个正在运行的服务(如果服务没在运行,就会启动这个服务)。连接后,您就可以通过服务暴露出来的接口与其进行通信。对音乐播放服务,这个接口可能允许用户对播放进行暂停、回退、停止、重播等操作。

像活动和其他组件一样,服务运行在程序进程的主线程中。这样它们就不会阻碍其他组件或是用户界面,它们通常是运行一个子线程来进行实时的任务(例如音乐回放)。

Content providers(内容提供者)

一个内容提供者管理一组共享的应用程序数据。您可以将数据存储在文件系统中,一个SQLite数据库,网络上,或任何其他应用程序可以访问的持久性存储位置。通过内容提供者,其他应用程序可以查询或者修改数据(如果内容提供者允许)。例如,Android系统提供了一个内容提供者来管理用户的联系信息。因此,任何有适当权限的应用程序可以查询内容提供者的一部分(如ContactsContract.Data)来读写一个特定的人的信息。

对于向您的应用程序读写不能共享的数据,内容提供者也很有用,。例如,记事本示例应用程序使用一个内容提供者保存笔记。

Broadcast receivers(广播接收器)

广播接收器是一个专注于接收广播通知信息,并做出对应处理的组件。很多广播是源自于系统代码的,比如,通知时区改变、电池电量低、拍摄了一张照片或者用户改变了语言选项。应用程序也可以进行广播,比如说,通知其它应用程序一些数据下载完成并处于可用状态。

应用程序可以拥有任意数量的广播接收器以对所有它感兴趣的通知信息予以响应。所有的接收器均继承自BroadcastReceiver基类。

广播接收器没有用户界面。然而,它们可以启动一个activity来响应它们收到的信息,或者用NotificationManager来通知用户。通知可以用很多种方式来吸引用户的注意力──闪动背灯、震动、播放声音等等。一般来说是在状态栏上放一个持久的图标,用户可以打开它并获取消息。

Android系统设计的一个独特方面是任何的一个程序都可以启动另一程序的组件。比如,你想让你的程序可以使用照相机拍照,如果已经有了实现这种功能的程序并且你你的程序能使用它(有权限),那么你就没有再要再写一个新的Activity来实现这个功能。你的程序不需要包含或者链接这个拍照程序。相反,你只需要在你的程序中打开这个拍照程序中的实现拍照功能的Activity。当拍完之后,拍好的照片甚至会自动返回给你的程序。者对于用户来说,就好像是想拍照功能的程序就是你的这个程序的一部分一样。

当系统启动一个组件之后,如果这个组件所在的程序之前没有运行的话,系统会自动开始这个程序的进程,并初始化这个组件所需要的相关类。比如,你的程序开启了一个拍照功能程序的Activity,这时系统会启动这个Activity所在的程序,所以这个Activity运行在拍照功能的程序当中,而不是在你的程序中。所以,不像其他操作系统的中的程序一样,Android程序没有一个单独的入口点(比如没有我们常见的main()函数)。

因为系统中的程序运行在自己的独立进程中,并且程序中的文件都有自己的限制其他程序访问的权限,所以,你的程序不能直接激活其他程序中的组件。但是Android系统就可以。具体是这样的实现的,为了激活(activate)其他程序中的组件,你必须向系统发送一个消息来详细说明你要启动其他组件的意图,这样系统才会为你激活这个组件。

Activating Components(激活组件)

四大组件中的三个组件——activities、services和broadcast receiver——是由一种叫intent的异步消息来激活的。这些intents在运行时(runtime)将这些属于你的程序或不同程序的单独的组件绑定在一起(bind),你可以把这些intents看作是需要其他组件的action的messengers。

一个intent就是一个Intent对象,这个intent定义了一种可以激活(activate)某个特定组件或者某种特定类型的组件,这两种情况分别对应两种intent的定义方式或者显示的或者隐式的。

对于activities和services,一个intent定义了要执行的操作(action)(比如,要“view”或者“send”什么)和要操作的数据的URI。比如,一个intent可能会为一个activity传递一个请求来展示一张图片或者打开一个网页。有时,你可以启动一个activity来得到返回的结果,在这个例子中这个activity的返回的结果也是一个Intent(比如,你可以发送一个intent让用户选择一个personal contact并返回给你——这个返回的intent就包含了一个指向用户选择的联系人的URI)。(关于activity和service的启动方式,下面将介绍。)

对于广播接收者来说,intent只是简单的定义了要广播的内容(比如,一个用以表明电池电量很低的广播仅包含了一个表明电池电量很低的字符串)。

剩余内容已隐藏,支付完成后下载完整资料


资料编号:[148604],资料为PDF文档或Word文档,PDF文档可免费转换为Word

您需要先支付 30元 才能查看全部内容!立即支付

发小红书推广免费获取该资料资格。点击链接进入获取推广文案即可: Ai一键组稿 | 降AI率 | 降重复率 | 论文一键排版