当前位置:  编程技术>移动开发
本页文章导读:
    ▪Application Fundamentals-Thread-safe methods(线程保险方法)        Application Fundamentals--Thread-safe methods(线程安全方法) Thread-safe methods--线程安全方法In a few contexts, the methods you implement may be called from more than one thread, and therefore must be written to be thread-safe. 翻译.........
    ▪ Application Fundamentals-Launch modes-起步模式        Application Fundamentals--Launch modes-启动模式 Launch modes-启动模式There are four different launch modes that can be assigned to an <activity> element's launchMode attribute:翻译:activity元素的launchMode属性可以设定为.........
    ▪ Application Fundamentals-Application Components(施用组件)       Application Fundamentals--Application Components(应用组件) Application Components-应用组件A central feature of Android is that one application can make use of elements of other applications (provided those applications permit it). For .........

[1]Application Fundamentals-Thread-safe methods(线程保险方法)
    来源: 互联网  发布时间: 2014-02-18
Application Fundamentals--Thread-safe methods(线程安全方法)
Thread-safe methods--线程安全方法

In a few contexts, the methods you implement may be called from more than one thread, and therefore must be written to be thread-safe. 翻译:某些场景下,方法的实现需要考虑线程安全。

This is primarily true for methods that can be called remotely — as in the RPC mechanism discussed in the previous section. When a call on a method implemented in an IBinder object originates in the same process as the IBinder, the method is executed in the caller's thread. However, when the call originates in another process, the method is executed in a thread chosen from a pool of threads that Android maintains in the same process as the IBinder; it's not executed in the main thread of the process. For example, whereas a service's onBind() method would be called from the main thread of the service's process, methods implemented in the object that onBind() returns (for example, a Stub subclass that implements RPC methods) would be called from threads in the pool. Since services can have more than one client, more than one pool thread can engage the same IBinder method at the same time. IBinder methods must, therefore, be implemented to be thread-safe.

翻译:那些需要被远程调用的方法是必须支持线程安全的。当我们调用源于同一个进程中创建的IBinder对象的方法的时候,方法的执行和调用是处于一个线程的。但是,对于远程调用而言,调用是源于另外一个进程的,被调用方法是一个特定线程中执行,这个线程来自IBinder对象所属进程维护的线程池(Android系统负责维护)中的一个线程的时候,被调用方法运行所处的线程不是当前IBinder对象所属进程的主线程。比如说,服务的onBind()方法从这个服务进程下的主线程中被调用的,而服务的onBind()方法所返回对象(实现RPC接口的Stub子类实例)是属于线程池中的某个线程,由于服务是可以有多个客户端的,线程池中多个线程可以提供对一个 IBinder实例的一个方法被多个客户端程序同时调用的需要,所以说IBinder接口中的远程方法的实现都必须支持线程安全的。

Similarly, a content provider can receive data requests that originate in other processes. Although the ContentResolver and ContentProvider classes hide the details of how the interprocess communication is managed, ContentProvider methods that respond to those requests — the methods query(), insert(), delete(), update(), and getType() — are called from a pool of threads in the content provider's process, not the main thread of the process. Since these methods may be called from any number of threads at the same time, they too must be implemented to be thread-safe.

翻译:同样,内容提供组件可以接受来自其他进程的数据请求,尽管ContentResolver 和 ContentProvider类都隐藏了很多底层通讯细节,query(), insert(), delete(), update(), 和 getType() 都是在内容提供组件所属进程下的线程池中的某个线程中执行的,所以它们都必须支持线程安全。

    
[2] Application Fundamentals-Launch modes-起步模式
    来源: 互联网  发布时间: 2014-02-18
Application Fundamentals--Launch modes-启动模式
Launch modes-启动模式

There are four different launch modes that can be assigned to an <activity> element's launchMode attribute:翻译:activity元素的launchMode属性可以设定为以下四种值:

"standard" (the default mode)
"singleTop"
"singleTask"
"singleInstance"

The modes differ from each other on these four points:---launchMode属性设定为以上这四个值会导致在以下在四个方面出现不同的差异:

    * 一、Which task will hold the activity that responds to the intent ---哪个task将持有被intent激活的这个Activity(实例被压入当前task堆栈还是其他堆栈)?. For the "standard" and "singleTop" modes, it's the task that originated the intent (and called startActivity()) — unless the Intent object contains the FLAG_ACTIVITY_NEW_TASK flag. In that case, a different task is chosen as described in the previous section, Affinities and new tasks.

      翻译: "standard" 和 "singleTop"启动模式的Activity组件被激活时候将归属当前task。除非激活这个Activity的intent中的flag值被设定为FLAG_ACTIVITY_NEW_TASK。

      In contrast, the "singleTask" and "singleInstance" modes mark activities that are always at the root of a task. They define a task; they're never launched into another task.

      翻译:"singleTask" 和 "singleInstance" 启动模式的Activity组件被激活时候始终是一个task的根Activity,这样的Activity是不可能进入其他task堆栈(当前task 堆栈)的,因为它始终是扮演根Activity的角色,如果它进入当前task堆栈的话,它就无法扮演根Activty的角色。所以这样的 Activity实例总是作为根Activity的角色归属于一个自身的task堆栈。
    *

      二、Whether there can be multiple instances of the activity---是否该Activity组件可以有多个实例存在?. A "standard" or "singleTop" activity can be instantiated many times. They can belong to multiple tasks, and a given task can have multiple instances of the same activity.

      翻译:"standard" 和 "singleTop"启动模式的Activity组件可以被多次实例化。在不同的task堆栈中可以重复出现,在一个的task堆栈中也可以有多个同类实例,

      In contrast, "singleTask" and "singleInstance" activities are limited to just one instance. Since these activities are at the root of a task, this limitation means that there is never more than a single instance of the task on the device at one time.

      翻译:相反,"singleTask" 和 "singleInstance" 启动模式的Activity组件被限制只能存在一个唯一的实例。这是因为这样的实例在自身独立的task堆栈中是根Activity的角色,在一个task中只能存在一个这样的组件实例。
    *
      三、Whether the instance can have other activities in its task ---该Activity组件实例是否允许它所在的task堆栈中有其他Activity组件实例?. A "singleInstance" activity stands alone as the only activity in its task. If it starts another activity, that activity will be launched into a different task regardless of its launch mode — as if FLAG_ACTIVITY_NEW_TASK was in the intent. In all other respects, the "singleInstance" mode is identical to "singleTask".

      翻译:"singleInstance" 启动模式的activity是不允许所处的task中出现其他activity实例的。这就意味着如果说这个activity激活其他activity的时候,被激活的activity是不可能与它同处于一个task堆栈的,被激活的activity只能是归属另外一个新的task堆栈的。这就如同这个 activity所发送的intent对象中的flag属性被设定为FLAG_ACTIVITY_NEW_TASK一样。除了这一点以外,singleInstance" 启动模式 和"singleTask"启动模式是一样的。

      The other three modes permit multiple activities to belong to the task. A "singleTask" activity will always be the root activity of the task, but it can start other activities that will be assigned to its task. Instances of "standard" and "singleTop" activities can appear anywhere in a stack.

      翻译:其他三个启动属性值的Activity组件实例是允许自己所处的task有多个Activity实例存在的。"singleTask" 启动模式的activity实例在堆栈中是根元素的角色,它可以激活其他 activity并压入到自己所处的task堆栈中。"standard" and "singleTop"启动模式的activity实例可以出现在一个task堆栈中的任意位置。

    * 四、Whether a new instance of the class will be launched to handle a new intent ----是否可以再创建一个这样的activity实例来响应intent对象?. For the default "standard" mode, a new instance is created to respond to every new intent. Each instance handles just one intent. For the "singleTop" mode, an existing instance of the class is re-used to handle a new intent if it resides at the top of the activity stack of the target task. If it does not reside at the top, it is not re-used. Instead, a new instance is created for the new intent and pushed on the stack.

      翻译:每当接收到一个intent对象的时候,启动模式是默认值"standard"的activity都是新创建一个实例来响应的,启动模式是"singleTop"的activity需要先判断当前task堆栈中的栈顶activity实例的类型是否和它一样,如果一样的话,直接使用栈顶实例来响应,如果不一样的话就创建新创建一个实例来响应,并且把这个新实例压入当前task堆栈。

      For example, suppose a task's activity stack consists of root activity A with activities B, C, and D on top in that order, so the stack is A-B-C-D. An intent arrives for an activity of type D. If D has the default "standard" launch mode, a new instance of the class is launched and the stack becomes A-B-C-D-D. However, if D's launch mode is "singleTop", the existing instance is expected to handle the new intent (since it's at the top of the stack) and the stack remains A-B-C-D.

      翻译:例如。一个task堆栈中 A是根activity实例,还有B、C、D 三个activity实例,这时候activity D处于栈顶,那么这个堆栈的实例顺序是A-B-C-D。如果这时堆栈顶activity D需要激活一个activity D类型的activity实例,如果activity D的启动模式是"standard"的话,那么Android系统将新建一个这样的实例并且压入当前task堆栈,这时堆栈中的实例顺序将变为A-B-C-D-D。如果activity D的启动模式是"singleTop"的话,那么Android系统直接使用堆顶的这个activity D来应答intent,这时堆栈中的实例顺序依然保持为A-B-C-D。

      If, on the other hand, the arriving intent is for an activity of type B, a new instance of B would be launched no matter whether B's mode is "standard" or "singleTop" (since B is not at the top of the stack), so the resulting stack would be A-B-C-D-B.

      翻译:如果说intent要激活的是activity B类型的实例,那么Android系统将马上新建一个activity B实例,这时堆栈中的实例顺序将变为A-B-C-D-B。这是由于当前堆栈顶实例不是B,此时根本不会考虑activity B的启动模式是"standard" 还是 "singleTop" 。

      As noted above, there's never more than one instance of a "singleTask" or "singleInstance" activity, so that instance is expected to handle all new intents. A "singleInstance" activity is always at the top of the stack (since it is the only activity in the task), so it is always in position to handle the intent. However, a "singleTask" activity may or may not have other activities above it in the stack. If it does, it is not in position to handle the intent, and the intent is dropped. (Even though the intent is dropped, its arrival would have caused the task to come to the foreground, where it would remain.)

      翻译:前面我们已经说过:"singleTask" 或 "singleInstance" 启动模式的activity在一个task堆栈中是绝对不可能有多个同类实例的,任何对这样的Activity发出请求的相应都是由这个唯一实例来响应的,"singleInstance" 启动模式的activity总是在task堆栈的顶端,因为它是堆栈中唯一的实例,所以它同时也是根Activity的角色。但是,"singleTask" 启动模式的activity在堆栈中是可以有其他非同类的实例的,如果说这时候有一个intent试图再次激活一个"singleTask" 启动模式的activity,假如这时候恰好堆栈顶实例就是所需要的实例那么由该实例直接响应,如果不是,那么该intent请求失效,虽然失效但是会导致该task来到系统前台。

When an existing activity is asked to handle a new intent, the Intent object is passed to the activity in an onNewIntent() call. (The intent object that originally started the activity can be retrieved by calling getIntent().)

Note that when a new instance of an Activity is created to handle a new intent, the user can always press the BACK key to return to the previous state (to the previous activity). But when an existing instance of an Activity handles a new intent, the user cannot press the BACK key to return to what that instance was doing before the new intent arrived.

需要注意的是:如果是一个新的Activity实例来响应intent请求,用户按下BACK键的时候,界面回到前一个Activity界面,如果Android系统式用当前task堆栈中的现有实例来响应的话,用户即使按下BACK键,也不能回到前一个界面。

For more on launch modes, see the description of the <activity> element.

    
[3] Application Fundamentals-Application Components(施用组件)
    来源: 互联网  发布时间: 2014-02-18
Application Fundamentals--Application Components(应用组件)
Application Components-应用组件

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 doesn't 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.【翻译:Android系统的一个重要特点是一个应用可以使用其他应用的元素,当然前提是被许可。例如,如果你的应用需要使用一个可滚动显示的图像List组件,而其他一个应用已经有这样一个组件,只要这个应用允许其他应用可以使用该组件,那么你的应用程序中就可以调用该组件,这时候你的应用程序不是通过包含或是链接的方式来使用该组件,而是直接启动其他应用中的某个组件。】

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 don't have a single entry point for everything in the application (no main() function, for example). Rather, they have essential components that the system can instantiate and run as needed. There are four types of components: 【翻译:要达到以上被其他应用程序借用自身元素的目的,Android系统必须在发生被借用需要的时候能够启动被借用应用的Linux进程,然后在进程中创建被借用元素所对应的Java实例。所以,与其他大多数操作系统的应用所不同的是:Android应用程序并没有一个唯一的启动入口如main这样的函数,而是根据应用程序的需要,创建组件实例,然后根据需要执行组件的相应方法。】

一、Activities组件
    An activity presents a visual user interface for one focused endeavor the user can undertake. For example, an activity might present a list of menu items users can choose from or it might display photographs along with their captions. A text messaging application might have one activity that shows a list of contacts to send messages to, a second activity to write the message to the chosen contact, and other activities to review old messages or change settings. Though they work together to form a cohesive user interface, each activity is independent of the others. Each one is implemented as a subclass of the Activity base class. 【翻译:activity组件是直接面向用户交互的组件,比如一个文本消息应用程序,可以有一个专门显示联系者的activity组件,还有一个针对指定的联系者发送消息的activity组件,当然还可以有其他Activity组件用做阅读接收到的消息或是消息修改的,这一系列activities组成了该应用的图形用户界面,其中每个Activity都是独立的,每个Activity都是 Activity的子类实现。】

    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.【翻译:一个应用程序可以只有一个或是多个Activity,具体数量取决于具体应用的需求和设计,但是总有一个activity是需要在该应用程序启动的时候第一个被呈现给客户(第一个被启动),之后第一个被启动的Activity可以根据用户实际操作可以继续启动其他的activity组件。】

    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 use of additional windows — for example, a pop-up dialog that calls for a user response in the midst of the activity, or a window that presents users with vital information when they select a particular item on-screen.【翻译:每个activity都给定在一个默认窗口中呈现,最为典型的是activity的窗口铺满在当前屏幕上,但是实际上需要比当前屏幕小些,浮动显示在其他窗口之上。这些都是可控的。activity本身还可以使用其他窗口,比如弹出式对话框,或是当用户选中当前窗口中的某个条目时候出现另外一个窗口为用户显示重要提示信息。】

    The visual content of the window is provided by a hierarchy of views — objects derived from the base View class. Each view controls a particular rectangular space within the window. Parent views contain and organize the layout of their children. Leaf views (those at the bottom of the hierarchy) draw in the rectangles they control and respond to user actions directed at that space. Thus, views are where the activity's interaction with the user takes place. For example, a view might display a small image and initiate an action when the user taps that image. Android has a number of ready-made views that you can use — including buttons, text fields, scroll bars, menu items, check boxes, and more.【翻译:activity的窗口中的内容通常是由view控件来呈现的,view控件有两类,一是容器类view控件,二是叶节点类view控件,view控件不仅显示内容,也可以根据用户的操作动作做出相应的动作即执行相关的代码。】

    A view hierarchy is placed within an activity's window by the Activity.setContentView() method. The content view is the View object at the root of the hierarchy. (See the separate User Interface document for more information on views and the hierarchy.)【翻译:view控件呈现在一个Activity的窗口中需要通过Activity.setContentView()方法才能实现。】

二、Services组件
    A service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time. For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it. Each service extends the Service base class. 【翻译:Services组件是没有用户界面但可以在一个指定时间段中在后台运行的程序,比如播放背景音乐或者是在后台执行网络数据获取操作或是执行某些计算为某些activities提供计算结果。】

    A prime example is a media player playing songs from a play list. The player application would probably have one or more activities that allow the user to choose songs and start playing them. However, the music playback itself would not be handled by an activity because users will expect the music to keep playing even after they leave the player and begin something different. To keep the music going, the media player activity could start a service to run in the background. The system would then keep the music playback service running even after the activity that started it leaves the screen. 【翻译:例如多媒体播放应用程序,是由多个activities组成的,一旦用户选定并播放了某个音乐后,该应用立即启动一个后台服务来播放音乐,这时候用户很可能闲置该应用或是开始另外一个应用,但是这时候后台服务依然是在播发音乐中。】

    It's possible to connect to (bind to) an ongoing service (and start the service if it's not already running). While connected, you can communicate with the service through an interface that the service exposes. For the music service, this interface might allow users to pause, rewind, stop, and restart the playback. 【翻译:一旦我们连接或是绑定的服务是已启动状态,比如我们刚说的音乐服务组件,通过该服务的接口我们就可以执行暂停、回退、终止或是重新播放操作。】

    Like activities and the other components, services run in the main thread of the application process. So that they won't block other components or the user interface, they often spawn another thread for time-consuming tasks (like music playback). See Processes and Threads, later. 【翻译:如果服务组件方法和应用中的其他组件activities方法一样,都在当前应用进程的主线程中运行,那么服务组件方法是不能够执行阻塞性操作的,如果这样将会干扰用户与activities界面的交互操作。所以服务组件被执行的方法经常是在主线程以外的另一个线程中执行,详细信息请参考Processes and Threads】

三、Broadcast receivers-广播接收组件
    A broadcast receiver is a component that does nothing but receive and react to broadcast announcements. Many broadcasts originate in system code — for example, announcements that the timezone has changed, that the battery is low, that a picture has been taken, or that the user changed a language preference. Applications can also initiate broadcasts — for example, to let other applications know that some data has been downloaded to the device and is available for them to use. 【翻译:广播接收组件只是用来接收广播信息或是当接收到广播信息时做出必要的提示动作,很多广播信息是来自操作系统的,例如电力不足时候发出警告。自己开发的应用程序也可以发布广播信息。】

    An application can have any number of broadcast receivers to respond to any announcements it considers important. All receivers extend the BroadcastReceiver base class. 【翻译:一个应用可以有多个广播接收组件来应答各类广播信息】

    Broadcast receivers do not display a user interface. However, they may start an activity in response to the information they receive, or they may use the NotificationManager to alert the user. Notifications can get the user's attention in various ways — flashing the backlight, vibrating the device, playing a sound, and so on. They typically place a persistent icon in the status bar, which users can open to get the message. 【翻译:广播接收组件同样没有用户图形界面,但是当接收到指定的广播信息时候,它可以启动一个activity组件来做应答或是NotificationManager 以把特定图像显示在状态栏或是发出声音信息的方式来告知用户。】

四、Content providers-内容提供组件
    A content provider makes a specific set of the application's data available to other applications. The data can be stored in the file system, in an SQLite database, or in any other manner that makes sense. The content provider extends the ContentProvider base class to implement a standard set of methods that enable other applications to retrieve and store data of the type it controls. However, applications do not call these methods directly. Rather they use a ContentResolver object and call its methods instead. A ContentResolver can talk to any content provider; it cooperates with the provider to manage any interprocess communication that's involved. 【翻译:内容提供组件专门为应用提供数据,数据通常是以一个文件的形式或是在系统自带的SQLite数据库中或是其他方式存放的,内容提供组件实际是由 ContentProvider扩展而来,ContentProvider提供了一套标准方法来读取和存储数据,然而在应用程序中并非直接调用这些方法的,而是使用ContentResolver对象间接调用的,ContentResolver和不同类型的内容提供组件交互的同时承担了进程间通讯管理。】

    See the separate Content Providers document for more information on using content providers.

Whenever there's a request that should be handled by a particular component, Android makes sure that the application process of the component is running, starting it if necessary, and that an appropriate instance of the component is available, creating the instance if necessary. 【翻译:上面我们说了Android应用是由以上四类组件构成的,这些组件相对独立存在,事实上任何一个组件都会关联到一个请求对象,请求对象可以触发组件,被触发的组件可根据请求对象中封转的请求信息做出相应的处理操作响应。Android系统负责:组件所属的应用进程的启动和运行以及进程所关联的组件实例的创建等等。】

    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3