最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android生命周期深入分析(二)

 更新時(shí)間:2012年12月06日 09:04:56   作者:  
Android 程序的生命周期是由系統(tǒng)控制而非程序自身直接控制。這和我們編寫(xiě)桌面應(yīng)用程序時(shí)的思維有一些不同,本文將詳細(xì)介紹,需要了解的朋友可以參考下

在 Android 中,多數(shù)情況下每個(gè)程序都是在各自獨(dú)立的 Linux 進(jìn)程中運(yùn)行的。當(dāng)一個(gè)程序或其某些部分被請(qǐng)求時(shí),它的進(jìn)程就“出生”了;當(dāng)這個(gè)程序沒(méi)有必要再運(yùn)行下去且系統(tǒng)需要回收這個(gè)進(jìn)程的內(nèi)存用于其他程序時(shí),這個(gè) 進(jìn)程就“死亡”了??梢钥闯?,Android 程序的生命周期是由系統(tǒng)控制而非程序自身直接控制。這和我們編寫(xiě)桌面應(yīng)用程序時(shí)的思維有一些不同,一個(gè)桌面應(yīng)用程序的進(jìn)程也是在其他進(jìn)程或用戶(hù)請(qǐng)求時(shí)被創(chuàng) 建,但是往往是在程序自身收到關(guān)閉請(qǐng)求后執(zhí)行一個(gè)特定的動(dòng)作(比如從 main 函數(shù)中 return)而導(dǎo)致進(jìn)程結(jié)束的。要想做好某種類(lèi)型的程序或者某種平臺(tái)下的程序的開(kāi)發(fā),最關(guān)鍵的就是要弄清楚這種類(lèi)型的程序或整個(gè)平臺(tái)下的程序的一般工作 模式并熟記在心。在 Android 中,程序的生命周期控制就是屬于這個(gè)范疇——我的個(gè)人理解:)

在 Android 系統(tǒng)中,當(dāng)某個(gè) activity調(diào)用 startActivity(myIntent) 時(shí),系統(tǒng)會(huì)在所有已經(jīng)安裝的程序中尋找其 intent filter 和 myIntent 最匹配的一個(gè) activity,啟動(dòng)這個(gè)進(jìn)程,并把這個(gè) intent 通知給這個(gè) activity。這就是一個(gè)程序的“生”。比如我們?cè)?Home application 中選擇 “Web browser”,系統(tǒng)會(huì)根據(jù)這個(gè) intent 找到并啟動(dòng) Web browser 程序,顯示 Web browser 的一個(gè) activity 供我們?yōu)g覽網(wǎng)頁(yè)(這個(gè)啟動(dòng)過(guò)程有點(diǎn)類(lèi)似我們?cè)谠趥€(gè)人電腦上雙擊桌面上的一個(gè)圖標(biāo),啟動(dòng)某個(gè)應(yīng)用程序)。在 Android 中,所有的應(yīng)用程序“生來(lái)就是平等的”,所以不光 Android 的核心程序甚至第三方程序也可以發(fā)出一個(gè) intent 來(lái)啟動(dòng)另外一個(gè)程序中的一個(gè) activity。Android 的這種設(shè)計(jì)非常有利于“程序部件”的重用。

  一個(gè) Android 程序的進(jìn)程是何時(shí)被系統(tǒng)結(jié)束的呢?通俗地說(shuō),一個(gè)即將被系統(tǒng)關(guān)閉的程序是系統(tǒng)在內(nèi)存不足(low memory)時(shí),根據(jù)“重要性層次”選出來(lái)的“犧牲品”。一個(gè)進(jìn)程的重要性是根據(jù)其中運(yùn)行的部件和部件的狀態(tài)決定的。各種進(jìn)程按照重要性從高到低排列如 下:
  1. 前臺(tái)進(jìn)程。這樣的進(jìn)程擁有一個(gè)在屏幕上顯示并和用戶(hù)交互的 activity 或者它的一個(gè)IntentReciver 正在運(yùn)行。這樣的程序重要性最高,只有在系統(tǒng)內(nèi)存非常低,萬(wàn)不得已時(shí)才會(huì)被結(jié)束。
  2. 可見(jiàn)進(jìn)程。在屏幕上顯示,但是不在前臺(tái)的程序。比如一個(gè)前臺(tái)進(jìn)程以對(duì)話框的形式顯示在該進(jìn)程前面。這樣的進(jìn)程也很重要,它們只有在系統(tǒng)沒(méi)有足夠內(nèi)存運(yùn)行所有前臺(tái)進(jìn)程時(shí),才會(huì)被結(jié)束。
  3. 服務(wù)進(jìn)程。這樣的進(jìn)程在后臺(tái)持續(xù)運(yùn)行,比如后臺(tái)音樂(lè)播放、后臺(tái)數(shù)據(jù)上傳下載等。這樣的進(jìn)程對(duì)用戶(hù)來(lái)說(shuō)一般很有用,所以只有當(dāng)系統(tǒng)沒(méi)有足夠內(nèi)存來(lái)維持所有的前臺(tái)和可見(jiàn)進(jìn)程時(shí),才會(huì)被結(jié)束。
  4. 后臺(tái)進(jìn)程。這樣的程序擁有一個(gè)用戶(hù)不可見(jiàn)的 activity。這樣的程序在系統(tǒng)內(nèi)存不足時(shí),按照 LRU 的順序被結(jié)束。
  5. 空進(jìn)程。這樣的進(jìn)程不包含任何活動(dòng)的程序部件。系統(tǒng)可能隨時(shí)關(guān)閉這類(lèi)進(jìn)程。
 

從某種意義上講,垃圾收集機(jī)制把程序員從“內(nèi)存管理噩夢(mèng)”中解放出來(lái),而 Android 的進(jìn)程生命周期管理機(jī)制把用戶(hù)從“任務(wù)管理噩夢(mèng)”中解放出來(lái)。我見(jiàn)過(guò)一些 Nokia S60 用戶(hù)和 Windows Mobile 用戶(hù)要么因?yàn)殚L(zhǎng)期不關(guān)閉多余的應(yīng)用程序而導(dǎo)致系統(tǒng)變慢,要么因?yàn)椴粫r(shí)查看應(yīng)用程序列表而影響使用體驗(yàn)。Android 使用 Java 作為應(yīng)用程序 API,并且結(jié)合其獨(dú)特的生命周期管理機(jī)制同時(shí)為開(kāi)發(fā)者和使用者提供最大程度的便利。

Activity lifecycle Activity有三種基本狀態(tài)
    Active:處于屏幕前景(當(dāng)前task的棧頂Activity處于Active狀態(tài)),同一時(shí)刻只能有一個(gè)Activity處于Active狀態(tài); Paused狀態(tài):處于背景畫(huà)面畫(huà)面狀態(tài),失去了焦點(diǎn),但依然是活動(dòng)狀態(tài); stopped:不可見(jiàn),但依然保持所有的狀態(tài)和內(nèi)存信息。

可以調(diào)用finish()結(jié)束處理Paused或者stopped狀態(tài)的Activity。

各種狀態(tài)之間通過(guò)下列的函數(shù)調(diào)用轉(zhuǎn)換

void onCreate(Bundle savedInstanceState)
void onStart()
void onRestart()
void onResume()
void onPause()
void onStop()
void onDestroy()

Activity的生命周期可以分為三組: The entire lifetime of an activity happens between the first call toonCreate() through to a single final call to onDestroy().

The visible lifetime of an activity happens between a call toonStart() until a corresponding call to onStop().

The foreground lifetime of an activity happens between a call to onResume() until a corresponding call toonPause().

activity_lifecycle

 

保存Activity狀態(tài)

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.

Unlike onPause() and the other methods discussed earlier, onSaveInstanceState() and onRestoreInstanceState()are not lifecycle methods. They are not always called. 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. 啟動(dòng)另一個(gè)Activity的過(guò)程 The current activity's onPause() method is called. Next, the starting activity's onCreate(), onStart(), and onResume() methods are called in sequence. Then, if the starting activity is no longer visible on screen, its onStop() method is called. service生命周期

A service can be used in two ways: It can be started and allowed to run until someone stops it or it stops itself. In this mode, it's started by callingContext.startService() and stopped by calling Context.stopService(). It can stop itself by callingService.stopSelf() or Service.stopSelfResult(). Only one stopService() call is needed to stop the service, no matter how many times startService() was called.

It can be operated programmatically using an interface that it defines and exports. Clients establish a connection to the Service object and use that connection to call into the service. The connection is established by callingContext.bindService(), and is closed by calling Context.unbindService(). Multiple clients can bind to the same service. If the service has not already been launched, bindService() can optionally launch it.

相關(guān)的方法:

void onCreate()
void onStart(Intent intent)
void onDestroy()

The onCreate() and onDestroy() methods are called for all services, whether they're started byContext.startService() or Context.bindService(). However, onStart() is called only for services started bystartService().

If a service permits others to bind to it, there are additional callback methods for it to implement:

IBinder onBind(Intent intent)
boolean onUnbind(Intent intent)
void onRebind(Intent intent)

service_lifecycle

 

Broadcast receiver lifecycle

只有一個(gè)方法:void onReceive(Context curContext, Intent broadcastMsg)

A process with an active broadcast receiver is protected from being killed. But a process with only inactive components can be killed by the system at any time, when the memory it consumes is needed by other processes.

This presents a problem when the response to a broadcast message is time consuming and, therefore, something that should be done in a separate thread, away from the main thread where other components of the user interface run. IfonReceive() spawns the thread and then returns, the entire process, including the new thread, is judged to be inactive (unless other application components are active in the process), putting it in jeopardy of being killed. The solution to this problem is for onReceive() to start a service and let the service do the job, so the system knows that there is still active work being done in the process. 進(jìn)程的生命周期

Android根據(jù)其重要性在內(nèi)存不足的時(shí)候移去重要性最低的進(jìn)程。重要性由高到低為:

    前臺(tái)進(jìn)程 可見(jiàn)進(jìn)程 服務(wù)進(jìn)程 后臺(tái)進(jìn)程 空進(jìn)程

注意:Because a process running a service is ranked higher than one with background activities, an activity that initiates a long-running operation might do well to start a service for that operation, rather than simply spawn a thread — particularly if the operation will likely outlast the activity. 比如播放MP3的時(shí)候就要啟動(dòng)一個(gè)service。

相關(guān)文章

  • Handler實(shí)現(xiàn)倒計(jì)時(shí)功能

    Handler實(shí)現(xiàn)倒計(jì)時(shí)功能

    這篇文章主要為大家詳細(xì)介紹了Handler實(shí)現(xiàn)倒計(jì)時(shí)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Android入門(mén)之Service的使用詳解

    Android入門(mén)之Service的使用詳解

    我們的Android在啟動(dòng)一些長(zhǎng)事務(wù)時(shí)都會(huì)使用異步,很多初學(xué)者覺(jué)得這個(gè)異步就是一個(gè)異步線程+Handler而己。如果你這么想就錯(cuò)了。這一切其實(shí)靠的正是Android里的Service。本文就來(lái)和大家聊聊Service的生命周期和使用,需要的可以參考一下
    2022-12-12
  • Android使用GestureOverlayView控件實(shí)現(xiàn)手勢(shì)識(shí)別

    Android使用GestureOverlayView控件實(shí)現(xiàn)手勢(shì)識(shí)別

    這篇文章主要為大家詳細(xì)介紹了Android使用GestureOverlayView控件實(shí)現(xiàn)手勢(shì)識(shí)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Android實(shí)現(xiàn)縮放動(dòng)畫(huà)

    Android實(shí)現(xiàn)縮放動(dòng)畫(huà)

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)縮放動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • android現(xiàn)有項(xiàng)目輕微改動(dòng)適配平板心得

    android現(xiàn)有項(xiàng)目輕微改動(dòng)適配平板心得

    這篇文章主要介紹了android現(xiàn)有項(xiàng)目輕微改動(dòng)適配平板心得,對(duì)android適配感興趣的同學(xué)可以參考下
    2021-04-04
  • Android開(kāi)發(fā)技巧之ViewStub控件惰性裝載

    Android開(kāi)發(fā)技巧之ViewStub控件惰性裝載

    布局文件中的控件并不一定在程序啟動(dòng)時(shí)全都用到,有一些控件只在特定的情況下才會(huì)被使用到;我們急需一種機(jī)制來(lái)改變<include>標(biāo)簽的這種行為,只在需要時(shí)裝載控件。這種機(jī)制就是本節(jié)要介紹的ViewStub控件
    2013-01-01
  • 使用adb?or?fastboot命令進(jìn)入高通的9008(edl)模式的兩種方法

    使用adb?or?fastboot命令進(jìn)入高通的9008(edl)模式的兩種方法

    這篇文章主要介紹了使用adb?or?fastboot命令進(jìn)入高通的9008(edl)模式,兩種方式通過(guò)命令給大家寫(xiě)的非常詳細(xì),文中又給大家補(bǔ)充介紹了高通手機(jī)?進(jìn)入?高通9008模式的兩種方法,需要的朋友可以參考下
    2023-01-01
  • Android如何調(diào)用系統(tǒng)相機(jī)拍照

    Android如何調(diào)用系統(tǒng)相機(jī)拍照

    這篇文章主要為大家詳細(xì)介紹了Android如何調(diào)用系統(tǒng)相機(jī)拍照的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • android初學(xué)者必須掌握的Activity狀態(tài)的四大知識(shí)點(diǎn)(必讀)

    android初學(xué)者必須掌握的Activity狀態(tài)的四大知識(shí)點(diǎn)(必讀)

    本篇文章主要介紹了android activity的四種狀態(tài),詳細(xì)的介紹了四種狀態(tài),包括Running狀態(tài)、Paused狀態(tài)、Stopped狀態(tài)、Killed狀態(tài),有興趣的可以了解一下。
    2016-11-11
  • Android開(kāi)發(fā)之獲取單選與復(fù)選框的值操作示例

    Android開(kāi)發(fā)之獲取單選與復(fù)選框的值操作示例

    這篇文章主要介紹了Android開(kāi)發(fā)之獲取單選與復(fù)選框的值操作,結(jié)合實(shí)例形式分析了Android針對(duì)單選按鈕、復(fù)選框的事件響應(yīng)、數(shù)值獲取等相關(guān)操作技巧,需要的朋友可以參考下
    2019-04-04

最新評(píng)論

伊春市| 柳河县| 五华县| 治县。| 乐山市| 西城区| 通海县| 汶川县| 武功县| 杨浦区| 九江县| 新密市| 老河口市| 盐津县| 庆云县| 色达县| 龙州县| 资阳市| 綦江县| 柘城县| 民县| 成武县| 康保县| 叶城县| 石嘴山市| 宁强县| 乡宁县| 开鲁县| 格尔木市| 岳池县| 浏阳市| 海晏县| 宕昌县| 阿拉善左旗| 邵阳县| 江阴市| 昌都县| 茌平县| 黄冈市| 延长县| 和硕县|