当前位置:  编程技术>移动开发
本页文章导读:
    ▪Application Fundamentals-Processes and Threads(过程与线程)        Application Fundamentals--Processes and Threads(进程与线程) Processes and Threads--进程与线程When the first of an application's components needs to be run, Android starts a Linux process for it with a single thread of execution. By defau.........
    ▪ Application Fundamentals-Saving activity state(保留activity实例的状态信息)        Application Fundamentals--Saving activity state(保存activity实例的状态信息) Saving activity state--保存activity实例的状态信息When the system, rather than the user, shuts down an activity to conserve memory, the user may expect to.........
    ▪ 照相机保存数据到ContentResolver       相机保存数据到ContentResolver public class CameraTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setConte.........

[1]Application Fundamentals-Processes and Threads(过程与线程)
    来源: 互联网  发布时间: 2014-02-18
Application Fundamentals--Processes and Threads(进程与线程)
Processes and Threads--进程与线程

When the first of an application's components needs to be run, Android starts a Linux process for it with a single thread of execution. By default, all components of the application run in that process and thread.

翻译:一旦应用中的某个组件需要执行,那么Android系统将启动一个Linux进程,在该进程中为该组件的运行分配一个线程。默认场合下,一个应用的所有组件都在该进程下的一个主线程中运行。

However, you can arrange for components to run in other processes, and you can spawn additional threads for any process.

翻译:然而作为开发者,是可以修订系统默认的进程、线程启动与管理策略的,达到多线程方式管理应用的目的。

Processes--进程

The process where a component runs is controlled by the manifest file. The component elements — <activity>, <service>, <receiver>, and <provider> — each have a process attribute that can specify a process where that component should run. These attributes can be set so that each component runs in its own process, or so that some components share a process while others do not. They can also be set so that components of different applications run in the same process — provided that the applications share the same Linux user ID and are signed by the same authorities. The <application> element also has a process attribute, for setting a default value that applies to all components.

翻译:每个组件运行的进程是由manifest文件控制的,Activity、service、provider、receiver组件元素都有一个process属性可以用来设定该组件运行的进程。如果设定了不同组件的不同进程,那么各个组件分别在自身的进程中运行。也可以为一些组件设定同样的进程属性值,这样一来,这些组件将运行在一个进程中。而且我们还可以通过这个属性的设置使得不同应用中的组件运行在同一个进程中,一旦不同应用中的组件运行在同一个进程中,那么不同应用将共享同一个Linux user ID,而且不同应用享有同样的执行权限。 <application> 元素本身也有一个process属性,如果我们设定了该元素的process属性,那么就意味着当前应用涉及的所有组件元素将使用该属性值作为自身的process属性值。

All components are instantiated in the main thread of the specified process, and system calls to the component are dispatched from that thread. Separate threads are not created for each instance. Consequently, methods that respond to those calls — methods like View.onKeyDown() that report user actions and the lifecycle notifications discussed later in the Component Lifecycles section — always run in the main thread of the process. This means that no component should perform long or blocking operations (such as networking operations or computation loops) when called by the system, since this will block any other components also in the process. You can spawn separate threads for long operations, as discussed under Threads, next.

翻译:所有的组件都是在指定(manifest文件所指定的进程或默认的)的主线程中被实例化的,Android系统对某个组件方法的调用是基于这个主线程派发的。这时候,Android系统是不会再为这些组件实例们分别创建各自独立的线程的。因此,所有基于某个组件的方法如View.onKeyDown()或是后面将讲述到的组件生命周期方法的调用与执行都是在这个主线程中运行的。这就意味着组件方法被系统调用执行过程中是不能出现长时间等待或是阻塞现象(网络操作或循环等待),一旦出现这些现象,将导致主线程中的所有其他组件方法执行出现阻塞现象。实际开发过程中,总有需要执行长等待或可能导致阻塞现象操作的可能,针对这种需要,开发者可以使用独立线程的方式达到自己的目的,为一些特殊组件提供独立线程支持,以避免阻塞主线程的现象的发生。后面我们将会在 Threads章节中讲述。

Android may decide to shut down a process at some point, when memory is low and required by other processes that are more immediately serving the user. Application components running in the process are consequently destroyed. A process is restarted for those components when there's again work for them to do.

翻译:Android系统可以在某个时间点上关闭进程,比如内存不足或其他更紧急的进程需要更多内存的时候。进程被关闭的同时,运行在被关闭进程上的组件也将被销毁。

When deciding which processes to terminate, Android weighs their relative importance to the user. For example, it more readily shuts down a process with activities that are no longer visible on screen than a process with visible activities. The decision whether to terminate a process, therefore, depends on the state of the components running in that process. Those states are the subject of a later section, Component Lifecycles.

     在决定哪个进程被终止的时候,Android系统需要衡量进程对用户的重要性,比如,系统更倾向终止当前屏幕上没有可见的Activity的进程。所以是否关闭一个进程和这个进程中运行的Activity状态有关系的,这里说的状态和后面我们将要讲到的Component Lifecycles有密切关系。

Threads--线程

Even though you may confine your application to a single process, there will likely be times when you will need to spawn a thread to do some background work. Since the user interface must always be quick to respond to user actions, the thread that hosts an activity should not also host time-consuming operations like network downloads. Anything that may not be completed quickly should be assigned to a different thread.

翻译:尽管你可以把你的应用约束在一个进程中运行(默认情况下该进程中只有一个主线程),实际上仍然有很多时候是需要多个线程以支持一些后台操作,由于用户界面总是需要能够快速响应客户交互动作的,所以,一个基本原则是:作为某个Activity的宿主线程不应该包含耗时操作,比如下载这样的操作,这样的操作应该在另外一个线程中执行。否则主线程将无法对用户基于界面的操作做出快速响应。

Threads are created in code using standard Java Thread objects. Android provides a number of convenience classes for managing threads — Looper for running a message loop within a thread, Handler for processing messages, and HandlerThread for setting up a thread with a message loop.

翻译:线程的创建可以使用标准的Java类Thread,android提供几个类可以很便捷管理线程, Looper 可以在一个线程中作消息循环, Handler 可以用来处理消息, HandlerThread 可以创建一个可以做消息循环的线程。

    
[2] Application Fundamentals-Saving activity state(保留activity实例的状态信息)
    来源: 互联网  发布时间: 2014-02-18
Application Fundamentals--Saving activity state(保存activity实例的状态信息)
Saving activity state--保存activity实例的状态信息

When the system, rather than the user, shuts down an activity to conserve memory, the user may expect to return to the activity and find it in its previous state.【翻译:系统需要更多内存资源的时候会销毁activity实例,用户希望系统能够在实例重新回到前台的时候能恢复之前的状态信息。】

To capture that state before the activity is killed, you can implement an onSaveInstanceState() method for the activity. Android calls this method before making the activity vulnerable to being destroyed — that is, before onPause() is called. It passes the method a Bundle object where you can record the dynamic state of the activity as name-value pairs. When the activity is again started, the Bundle is passed both to onCreate() and to a method that's called after onStart(), onRestoreInstanceState(), so that either or both of them can recreate the captured state.

翻译:在activity被销毁之前要想俘获实例的状态信息,开发者需要在activity中实现onSaveInstanceState()方法,Android系统会在被销毁之前调用该方法,准确说是在 onPause()方法被调用之前,系统会调用onSaveInstanceState()方法。这个方法的参数是一个Bundle对象,这个参数中以name-value的形式持有这个activity实例的属性状态信息。当这个activity类的另外一个实例被创建的时候,这个Bundle对象会作为参数传递给onCreate() 、onStart() 和onRestoreInstanceState(), 方法。所以在这三个方法中,都可以恢复activity实例之前的状态信息。

Unlike onPause() and the other methods discussed earlier, onSaveInstanceState() and onRestoreInstanceState() are not lifecycle methods. They are not always called. For example, Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action (such as pressing the BACK key). In that case, the user won't expect to return to the activity, so there's no reason to save its state.

翻译:onSaveInstanceState() 和 onRestoreInstanceState()方法与我们之前讨论过的其他生命周期回调方法不同,这两个方法不是生命周期方法,这两个方法不总是被系统调用,例如:Android系统会在activity实例被销毁前调用onSaveInstanceState()方法,系统不会在按下BACK键的时候(实例弹出当前task堆栈,这时候实例也被销毁)多余地去调用onSaveInstanceState()方法。因为这时候用户其实并不期望再次返回到该实例的界面,所以不需要保存该实例的状态信息。

Because onSaveInstanceState() is not always called, you should use it only to record the transient state of the activity, not to store persistent data. Use onPause() for that purpose instead.

正是因为onSaveInstanceState() 不是经常被系统调用,所以,开发者实现该方法通常是为了保留activity实例的临时状态信息,如果是需要保存activity实例的持久化信息,应该是在onPause()方法中实现。

    
[3] 照相机保存数据到ContentResolver
    来源: 互联网  发布时间: 2014-02-18
相机保存数据到ContentResolver

public class CameraTest extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Button cameraButton = (Button) findViewById(R.id.cameraButton); 
    cameraButton.setOnClickListener( new OnClickListener(){ 
        public void onClick(View v ){ 
 
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
 
                startActivityForResult(intent,0); 
        } 
    }); 
 

 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if (requestCode== 0 && resultCode == Activity.RESULT_OK){ 
                Bitmap x = (Bitmap) data.getExtras().get("data"); 
                ((ImageView)findViewById(R.id.pictureView)).setImageBitmap(x); 
                ContentValues values = new ContentValues(); 
        values.put(Images.Media.TITLE, "title"); 
        values.put(Images.Media.BUCKET_ID, "test"); 
        values.put(Images.Media.DESCRIPTION, "test Image taken"); 
        values.put(Images.Media.MIME_TYPE, "image/jpeg"); 
        Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); 
        OutputStream outstream; 
                try { 
                        outstream = getContentResolver().openOutputStream(uri); 
 
        x.compress(Bitmap.CompressFormat.JPEG, 70, outstream); 
        outstream.close(); 
                } catch (FileNotFoundException e) { 
                        // 
                }catch (IOException e){ 
                        // 
                } 
        } 

 
 


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
c/c++开源软件 iis7站长之家
▪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