登录

  • 登录
  • 忘记密码?点击找回

注册

  • 获取手机验证码 60
  • 注册

找回密码

  • 获取手机验证码60
  • 找回
毕业论文网 > 外文翻译 > 计算机类 > 计算机科学与技术 > 正文

使用活动和信使外文翻译资料

 2022-10-26 10:04:53  

Leveraging Activities and Intents

In many ways Android application management appears to be inspired by

JavaScript and the web browser and rightly so! The web browser model has

proved itself as a mechanism that users find easy to work with. Android as

a system, Android has many things in common with a web browser, some of

which are obvious, and others that you will need to look a little deeper for.

The Activity Stack is much like a single-directional web browser history. When

you launch an Activity using the startActivity method, you effectively

hand control back to the Android system. When the user pushes the hardware

'Back' button on their phone, the default action is to pop the top Activity off

the stack, and display the one underneath (not always the one that started it).

In this chapter well explore a little of how Android runs an application and manages

Activity instances. While not strictly necessary for user interface design, its important to

know how this works. Properly leveraging these concepts will help you ensure a consistent user interface experience. As you will also see, it will help you improve the performance of your application, and allow you to reuse more of your application components.

Its also important to understand how an Activity is created (and when it is created),

as well as how Android decides what Activity to create. Well also look at some good

practices to follow when building an Activity class, and how to behave nicely within the

confines of an Android application.

Weve already encountered the 'Activity Stack' in Chapter 1, Developing a Simple Activity and Chapter 2, Presenting Data for Views where we constructed Intent objects to

launch specific Activity classes. When you used the hardware 'Back' button, you were

automatically taken to the previous Activity instance, no code needed (much like a

web-browser). For this chapter well be looking at:

  • The life cycle of an Activity object
  • Using the Bundle class to maintain application state
  • Exploring the relationship between an Intent and an Activity
  • Passing data into an Activity through an Intent

Exploring the Activity class

The life cycle of an Activity object is much more like a Java Applet than a normal

application. It may be started, paused, resumed, paused again, killed, and then brought

back to life in a seemingly random order. Most Android devices have very good performance specifications. However, most of them appear underpowered when compared to the top-of-the-range devices. For those devices that do have good specifications, users tend to demand a lot more from them than the cheaper devices. On a phone, youre never going to get away from the fact that you have many applications and services sharing a very limited device.

An Activity may be garbage-collected any time it is not visible to the user. This means

it may be your application that is running, but because the user is looking at a different

Activity, any non-visible or background Activity objects may be shut down or garbagecollected in order to save memory. By default, the Android APIs will handle these shut down/ start up cycles elegantly by storing their state before a shut down, and restoring it when they are re-created. A very simple diagram of the life cycle of an application with two Activity instances is shown. When the 'Main Activity' is paused, it becomes eligible for garbage-collection by the system. If this happens, it will first store its state in a temporary location, restoring the state when it is brought back to the foreground.

Storage of user interface state

If an Activity is stopped, all View objects that have an ID assigned

will attempt to store their state before they are made available for

garbage-collection. However, this state is only stored for the lifetime of

the application. When the application is shut-down, this state is lost.

While its possible to use the setContentView method over and over again to change

the content on the screen (much the way you might build a wizard interface with an AWT

CardLayout object), its considered a very bad idea. You are effectively trying to take the

control away from Android, which will always create problems for you. If for example, you developed an application with only one Activity class, and used multiple layout resources or your own custom ViewGroup objects to represent different screens, you would also have to take control of the hardware 'Back' button on the device in order to allow the user to go backwards. Your application is released in the Android market, and a few months later a handset manufacturer decides to put a 'Forward' button onto their new phone (in the same style as the 'Forward' button on a web-browser). The Android system would be patched to handle this change to the device, but your application would not be. As a result, your users get frustrated with your application because 'it doesnt work properly'.

Using Bundle objects

In the onCreate method of the Activity class, weve been accepting a Bundle parameter

named saveInstanceState, as you may have guessed. Its where state information is

stored between stops and starts of an Activity. Despite what it looks like, a Bundle object

is not a form of persistent storage. When the configuration of a device context changes (for example when the user selects a new language, or changes from 'portrait' to 'landscape' mode), the current Activity is 'restarted'. For this to happen, Android requests the Activity save its state in a Bundle object. It then shuts down and destroys the

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


使用活动和信使

Android应用程序管理在许多方面似乎是灵感来自

JavaScript和web浏览器,web浏览器模式证明了自己

作为一种机制,用户很容易处理。作为一个系统,Android

在很多方面与web浏览器有着相似之处,其中一些是明

显的,另外一些则需要你深入一点去探索。

活动堆栈就像单方向性的 web浏览器的历史,当你

用startActivity方法创建一个活动时,你就有效地控

制了Android系统,当用户在手机上按“后退”按钮时,

默认动作是流行活动从堆栈顶部,并显示下面的一个(并

不总是刚开始的那一个)。

在这一章里,我们将探索一些Android如何运行应用程序和管理活动的实例。虽然对用户界面设计来说不是必需的,但重要的是要了解它是如何工作的。正确地利用这些概念将帮助你确保一致的用户界面体验。您还将看到,它将帮助你改善应用程序的性能,并允许您重用您的许多应用程序组件。

理解一个活动是如何创建的(什么时候创建的)以及Android如何决定创建什么活动也是非常重要的。当我们创建一个活动类以及思索如何在一个Android应用程序范围内设计出好的东西时,我们也将找一些好的实践的例子。

我们在第一章“开发一个简单的活动”以及第二章显示视图中数据,在视图中,我们建造信使对象来创建特殊的活动类时已经提到了“活动堆栈”。当你使用“返回”按钮时,系统自动采取之前的活动实例,不需要代码(就像一个浏览器)。在这一章我们将学习:

  • 一个活动对象的生命周期
  • 使用包类来维护应用程序状态
  • 探索活动和信使之间的关系
  • 通过信使传递数据到活动中

探索活动类

一个活动对象的生命周期更像一个java Applet而不是一个普通的应用程序,它可能经过开始、暂停、恢复、再暂停、结束等阶段,然后在一个随机的阶段再次运作。大多数Android设备有很好的性能规范,然而,他们中的大多数相比高端设备出现动力不足的现象。对于那些有良好规范的设备, 与便宜的设备相比,用户需求更多。在手机上,你永远不会离开你有许多应用程序和共享一个非常有限服务的设备。

一个活动可能是在任何时间对用户都是不可见的。这意味着可能是你的应用程序正在运行,但由于用户查看不同的活动, 为了节省内存,任何不可见或背景活动对象都被关闭或被回收。默认情况下,Android api在一个活动关闭之前将通过存储他们的状态,并当他们被重新创建时恢复它来处理这些关闭/启动周期。一个非常简单的应用程序的生命周期图有两个活动实例。当“主要活动”暂停时,就有资格获得垃圾收集系统。如果发生这种情况,它首先存储在一个临时位置状态, 当回到前台时恢复状态。

用户界面状态的存储

如果一个活动停止,所有被分配一个ID的视图对

象将尝试在他们被垃圾回收之前存储他们的状态。然

而,这种状态只是存储在应用程序的生命周期。当应用

程序关闭时,这种状态就随之消失了。

虽然可以使用setContentView方法反复改变屏幕上的内容(你可以用AWTCardLayout对象建立一个向导的界面),但是这是一个非常糟糕的主意。你实际上是试图夺走Android的控制,这将给你带来麻烦。例如,如果您开发了一个只有一个Activity类的应用程序,并使用多个布局资源或您自己的自定义ViewGroup对象来显示不同的屏 幕,你还必须控制硬件设备上的“返回”按钮,以允许用户倒退。发布您的应用程序在Android市场,几个月后手机制造商决定把“前进”按钮放到他们的新电话(像浏览器上的“前进”按钮一样),Android系统将修补处理这种变化到设备上,但您的应用程序不会这样做。因此,用户将对您的应用程序感到失望,因为它不能正常工作。

使用包对象

在onCreate方法活动的类中,我们一直接受的包参数名叫save-

InstanceState,正如你所想的那样。它处在状态信息存储在哪里和活动在哪里停止和开始之间。尽管它看起来像什么,一个包对象不是持久性存储的一种形式。当关联设备的配置更改时(例如,当用户选择一个新的语言,或从“肖像”改变到“景观”模式时),当前的活动会“重新启动”。要实现这一目标,Android请求包对象的活动保存其状态。然后关闭并破坏现有的实例,然后创建一个新的活动的实例 (与新配置参数),包状态信息保存在实例中。

包类实际上是一个包含任意数量的值的Map lt;字符串,?gt;。因为包对象用于存储短期状态(也就是说,博客用户正忙着打字),它们多是用于存储视图对象的状态。他们在标准 Java序列化方面有两个主要优势:

  • 你被迫手动实现对象的存储。这需要一些对如何将存储

的对象,和哪些部分需要存储进行思考。例如,大多数的

时间在一个用户界面,你不需要存储布局信息,因为这可

以在布局文件中重新创建。

  • 一个包作为一个键值结构比一个序列化的对象更加不会

过时和更加灵活。你可以省去设置为默认值的值,减少

包的大小。

一个包对象也是一个类型安全的结构。如果你使用putString方法,只有getString或getCharSequence将检索对象。我强烈建议使用包的get方法时,您应该提供一个默认值。

在一个活动由Android系统暂停时,系统将请求保存包对象的所有状态信息。要做到这一点,onSaveInstanceState方法将被调用。这发生在onPause方法之前。为了恢复活动的状态,系统将调用保存的状态包的onCreate方法。

视图类也有一个onSaveInstanceState方法,以及onRestoreIn-

stanceState方法。如前所述,Activity类的默认功能将尝试通过包里的每个ID保存每个视图对象。这是另一个坚持XML布局而不是构建你自己的布局的好理由。有一个引用视图对象对于保存和恢复来说是不够的,虽然您可以在Java代码中指定id,但是它的用户界面需要的代码更多。

action-building游戏的一个例子:“猜我的号码”

我们想要构建一个从包对象中保存和恢复它的状态的简单例子。对于这个示例,我们有一个很简单的“猜我的号码”的游戏。活动对象选择一个1到10之间的数字并且让用户猜这个数字。

这个例子的基本用户界面布局需要一个标签告诉用户要做什么,一个输入区域输入他们的猜测,和一个按钮告诉应用程序猜测他们希望输入的值。

如果用户在玩这个游戏时获得了信息,我们将很大可能上失去他想要猜测的数字。因为这个原因当系统要求我们保存状态时我们将存储用户试图想在包对象中猜测的数字。我们还需要找到系统启动时存储的数字。

1.从一个命令提示符中创建一个名为GuessMyNumber的新项目 。

android create project -n GuessMyNumber -p GuessMyNumber -k com.

packtpub.guessmynumber -a GuessActivity -t 3

2.在编辑器或者IDE中打开缺省的res/layout/main.xml 文件。

3.删除默认的linearLayout元素中的内容。

4.添加一个新的TextView作为一个标签,告诉用户要做什么:

lt;TextView android:text=

'Im thinking of a number between 1 and 10. Can you guess what

it is?'

android:layout_width='fill_parent'

android:layout_height='wrap_content'/gt;

5.创建一个新的EditText,用户在里面输入他们的猜测。使用android:数值属性的TextView只执行整数输入:

lt;EditText

android:id='@ id/number'

android:numeric='integer'

android:layout_width='fill_parent'

android:layout_height='wrap_content'/gt;

6.添加一个用户点击按钮使用户能提交他们的才猜测:

lt;Button android:id='@ id/guess'

android:text='Guess!'

android:layout_width='fill_parent'

android:layout_height='wrap_content'/gt;

7.现在在你的编辑器或IDE中打开GuessActivity.java文件。

8.使OnClickListener GuessActivity类实现:

public class GuessActivity

extends Activity implements OnClickListener {}

9.创建一个字段变量来存储用户猜测的数字:

private int number;

10.创建一个实用程序方法来生成一个一至十中的随机数:

private static int random() {

return (int)(Math.random() * 9) 1;

}

11.在onCreate方法中,在调用super.onCreate后,检查以确保传入的包不为空:

if(savedInstanceState != null) {

12.如果包不为空,那么尝试从中获取存储数量:

number = savedInstanceState.getInt('Number', random());

13.如果包是空的,活动作为一种新的实例运行产生一个随机数:

else {

number = random();

}

14.然后在main.xml使用setContentView布局资源:

setContentView(R.layout.main);

15.找到在main.xml布局资源中你使用的按钮对象:

Button button = (Button)findViewById(R.id.guess);

16.对于GuessActivity对象,设置按钮对象的OnClickListener

button.setOnClickListener(this);

17.现在重写onSaveInstanceState方法:

protected void onSaveInstanceState(Bundle outState) {

18.首先允许默认的Activity行为:

super.onSaveInstanceState(outState);

19.然后在包中存储有效的数字:

outState.putInt('Number', number);

20.我们需要重写onClick方法来处理用户的猜测:

public void onClick(View clicked) {

21.找到用户输入这个猜测号码的编辑框:

EditText input = (EditText)findViewById(R.id.number);

22.使EditText解析的当前值变为整数:

int value = Integer.parseInt(input.getText().toString());

23.如果他们猜的数太小,就用Toast方法告诉他们:

if(value lt; number) {

Toast.makeText(this, 'Too low', Toast.LENGTH_SHORT).show();

}

24.如果他们猜的数太大,也用Toast方法告诉他们:

else if(value gt; number) {

Toast.makeText(this, 'Too high', Toast.LENGTH_SHORT).show();

}

25.如果他们成功地猜对了数字,就祝贺他们:

else {

Toast.makeText(

this,

'You got it! Try guess another one!',

Toast.LENGTH_SHORT).show();}}

26

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


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

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

企业微信

Copyright © 2010-2022 毕业论文网 站点地图