国产激情自拍_国产9色视频_丁香花在线电影小说观看 _久久久久国产精品嫩草影院

首頁 > 系統(tǒng) > Android > 正文

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

2020-04-11 12:39:19
字體:
供稿:網(wǎng)友

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

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

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

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

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

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

各種狀態(tài)之間通過下列的函數(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. 啟動另一個Activity的過程 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

只有一個方法: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)存不足的時候移去重要性最低的進(jìn)程。重要性由高到低為:

    前臺進(jìn)程 可見進(jìn)程 服務(wù)進(jìn)程 后臺進(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的時候就要啟動一個service。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
国产激情自拍_国产9色视频_丁香花在线电影小说观看 _久久久久国产精品嫩草影院
国产在线观看色| 国产精品免费91| 国产www.大片在线| 国产馆av播放| 国产不卡在线| 亚洲日本久久久午夜精品| a级片国产精品自在拍在线播放| 免费看ww视频网站入口| 男女午夜视频在线观看| 福利在线视频导航| 国产福利在线| 免费国产阿v视频在线观看| 成人av小说网| 精品推荐国产麻豆剧传媒| 日本一本久久| 国产精品久久久久白浆| 午夜在线不卡| 97视频免费| 国产在线观看18| 亚洲天堂久久久| 国产鲁鲁视频在线观看特色| 在线中文av| www.xxx黄| 精品国产一区二区三区久久久狼牙 | 国产对白在线| 欧洲一区av| av男人的天堂网| 91欧洲在线视精品在亚洲| 天堂在线中文| 国产免费a∨片在线观看不卡| 国产夫妻视频| 国产美女在线播放| 亚洲精品手机在线| 国产对白在线| 天堂在线中文资源| 四虎网站在线观看| 在线色视频观看| 国产精品久久久久白浆| 99热在线免费播放| 五月婷婷视频在线观看| 久草.com| 最近中文av字幕在线中文| 开心激情五月婷婷| 国产农村av| 最近中文av字幕在线中文| 天堂中文在线视频| 成在在线免费视频| 高清欧美精品xxxxx在线看 | 精品福利视频导航大全| 国产一卡2卡3卡免费网站| 欧美96在线| 日本中文字幕视频| 国产区视频在线观看| 国产成人天天5g影院| 96久久久久久| 快射av在线播放一区| 国产免费高清| 国产黄色免费网站| 一本大道久久a久久精品| 亚洲精品视频在线免费| 久久久久久五月天久久久久久久久| www免费在线观看| 天堂在线免费观看| 国产精品久久久久永久免费看| 国产一级激情| av在线免费观看网| 国产精品一区二三区| 国产福利免费在线观看| a视频免费看| 国产丝袜在线观看视频| 伊人电影在线观看| 91av资源在线| 亚洲精品视频区| 国产一级二级三级在线观看| 99在线播放| 国产三级在线看| 好看的中文字幕在线播放| 99在线播放| 2020国产在线视频| 亚洲欧美精品日韩欧美| 丁香视频五月| 欧美日韩国产亚洲沙发| 国产无遮挡在线视频免费观看| 亚洲第一成人在线视频| 国产深夜视频在线观看| 日本在线免费中文字幕| 国产福利小视频在线观看| 资源视频在线播放免费| 久热国产在线| www在线视频| 在线午夜视频| 国产成人午夜精品| 国产极品视频| 国产超碰在线观看| 午夜av在线免费观看| 国产蜜臀在线| 国产精品一区在线看| 黄色国产网站在线观看| www.色婷婷| 国产国产国产国产国产国产| 国产一级大片| 丁香花在线电影| 久久久久久久久免费视频| 日本片在线看| 超碰国产在线观看| 老司机精品视频一区二区| 免费看黄视频网站| www狠狠操| 久久香蕉av| 国产美女自拍视频| 最近中文字幕在线中文视频 | 2019中文字幕视频| 国产叼嘿网站免费观看不用充会员| 懂色av一区| 天堂在线视频| 99re在线视频播放| gogo在线观看| 国产精品作爱| 精品一区二区三区高清免费不卡| 91最新在线| jlzzjlzz欧美大全| 国产免费人人看| 国产成人综合亚洲欧美在| 99综合精品久久| 欧美性xxxx交| 日本中文字幕在线播放| 国产欧美一区二区三区小说| 91久久麻豆| 久久香蕉一区| 精灵使的剑舞无删减版在线观看| 亚洲精品久久久成人| 九九热视频在线| 精品街拍一区二区| 狠狠干婷婷色| 精品女厕厕露p撒尿| 亚洲sss视频| 最近中文字幕mv免费高清电影| √8天堂资源地址中文在线| 黄色激情网址| 日本视频二区| 在线天堂视频| 国产视频三级在线观看播放| wwww亚洲| 在线观看av网站| 91在线中文| 中文在线视频| 精品入口蜜桃| 国产精品视频二区三区| 99视频在线观看地址| 开心丁香婷婷深爱五月| 精品成人一区二区三区免费视频| 国产精品人人| 国产丝袜自拍| 国产欧美日韩第一页| 最近中文字幕av免费高清| 国产伦精品一区二区三区高清版禁 | 国产美女性感在线观看懂色av| av大片在线播放| 在线午夜视频| 伊人伊人av电影| 国产精品伦一区二区三区视频| 成人免费一区二区三区视频网站| 超碰免费97在线观看| 欧美日韩性视频一区二区三区| 亚洲欧美日韩综合精品网| 在线āv视频| 精品久久久久一区二区三区| 夜夜操天天干| 黄色电影网站在线观看| 中文字幕免费中文| 国产三级在线观看| 91视频久色| 欧美精品se| 国产一区二区三区美女秒播| 国产精品久久一区二区三区不卡| 天堂资源中文在线| av二区三区| 免费看成年人视频在线观看| 天天av天天爱| 亚洲最新永久在线观看| 香蕉视频网站在线观看| 欧美性xxxx交| 国产福利在线看| 阿v免费在线观看| 最近免费中文字幕大全免费第三页 | 在线免费黄色毛片| 久热精品视频在线播放| 国产亚洲精品拍拍拍拍拍| 91涩漫在线观看c| 女同一区二区免费aⅴ| 国产精品爱久久久久久久小说 | 丁香视频五月| av片在线观看永久免费| 国产一级在线观看www色| av手机天堂| 国产网站免费观看| 久久精品免视着国产成人| 国产精品理人伦一区二区三区| h网站免费在线观看| 国产激情二区|