亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > Android > 正文

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

2020-04-11 12:39:19
字體:
來源:轉載
供稿:網友

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

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

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

從某種意義上講,垃圾收集機制把程序員從“內存管理噩夢”中解放出來,而 Android 的進程生命周期管理機制把用戶從“任務管理噩夢”中解放出來。我見過一些 Nokia S60 用戶和 Windows Mobile 用戶要么因為長期不關閉多余的應用程序而導致系統變慢,要么因為不時查看應用程序列表而影響使用體驗。Android 使用 Java 作為應用程序 API,并且結合其獨特的生命周期管理機制同時為開發者和使用者提供最大程度的便利。

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

可以調用finish()結束處理Paused或者stopped狀態的Activity。

各種狀態之間通過下列的函數調用轉換

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狀態

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.

相關的方法:

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. 進程的生命周期

Android根據其重要性在內存不足的時候移去重要性最低的進程。重要性由高到低為:

    前臺進程 可見進程 服務進程 后臺進程 空進程

注意: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。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲午夜国产成人av电影男同| 日韩免费av一区二区| 国产一区二区三区四区福利| 国产精品美乳一区二区免费| 2018日韩中文字幕| 久久久久国产精品免费网站| 欧美日韩一区二区精品| 国产精品91免费在线| 欧美久久精品一级黑人c片| 91精品国产亚洲| 国产精品嫩草影院一区二区| 91色在线观看| 亚洲综合自拍一区| 成人做爽爽免费视频| 亚洲成年人影院在线| 国产成人久久久精品一区| 黄色一区二区在线观看| 亚洲肉体裸体xxxx137| 茄子视频成人在线| 日韩av在线免费| 欧美中文字幕第一页| 国产色视频一区| 日本不卡免费高清视频| 北条麻妃99精品青青久久| 日韩在线免费观看视频| 久久国产精品久久精品| 国产国语videosex另类| 久久亚洲综合国产精品99麻豆精品福利| 日韩精品在线影院| 91欧美视频网站| 狠狠躁夜夜躁久久躁别揉| 成人精品视频99在线观看免费| 欧美精品成人91久久久久久久| 欧美激情免费观看| 日韩av电影在线免费播放| 青青青国产精品一区二区| 亚洲欧美成人一区二区在线电影| 国产日韩av在线| 草民午夜欧美限制a级福利片| 91视频国产精品| 亚洲二区在线播放视频| 日本国产精品视频| 久久激情视频免费观看| 亚洲成人黄色网址| 海角国产乱辈乱精品视频| 亚洲精品国产拍免费91在线| 亚洲国产美女精品久久久久∴| 国产69精品99久久久久久宅男| 91亚洲午夜在线| 日韩av在线免费看| 亚洲偷熟乱区亚洲香蕉av| 亚洲男人天堂网| 欧美性色19p| 欧美性资源免费| 亚洲在线免费观看| 97超碰国产精品女人人人爽| 欧美精品中文字幕一区| 国产成人精品网站| 成人国产亚洲精品a区天堂华泰| 亚洲国产另类 国产精品国产免费| 国产精品久久久久久久久免费| 亚洲精品久久久久中文字幕二区| 久久久免费高清电视剧观看| 国产精品高潮视频| 亚洲精品动漫100p| 亚洲一区二区中文字幕| 欧美最猛性xxxxx免费| 91精品国产电影| 国外成人性视频| 精品动漫一区二区三区| 国产精品com| 欧洲精品在线视频| 姬川优奈aav一区二区| 国产精品一区二区三区免费视频| 午夜精品一区二区三区在线播放| 日韩精品高清视频| 久久亚洲影音av资源网| 青青久久aⅴ北条麻妃| 亚洲人成电影网站色| 亚洲影院高清在线| 国产精品精品久久久| 国产91久久婷婷一区二区| 日韩久久精品成人| 一本色道久久88综合亚洲精品ⅰ| 国内精品久久久久久久久| 日韩精品丝袜在线| 日本精品在线视频| 欧美一区在线直播| 国模叶桐国产精品一区| 国产精品爽黄69天堂a| 欧洲日本亚洲国产区| 欧美日本高清视频| 日韩精品中文字幕有码专区| 日韩欧美高清在线视频| 欧美国产在线电影| 国产专区精品视频| 91高清免费视频| 亚洲丁香久久久| 亚洲男人第一av网站| 中文字幕精品影院| 国产欧美久久久久久| 久久综合色88| 亚洲第一福利网| 欧美黑人性视频| 欧美成人剧情片在线观看| 亚洲国产欧美在线成人app| 久久天天躁狠狠躁夜夜爽蜜月| 精品色蜜蜜精品视频在线观看| 综合国产在线视频| 亚洲wwwav| 日产日韩在线亚洲欧美| 国产精品吹潮在线观看| 色综合久久88| 亚洲成人a**站| 日韩欧美亚洲范冰冰与中字| 夜夜嗨av一区二区三区免费区| 精品亚洲精品福利线在观看| 久热国产精品视频| 2019中文字幕免费视频| 91社区国产高清| 亚洲综合日韩中文字幕v在线| 国产精品国产自产拍高清av水多| 亚洲人成五月天| 97在线免费视频| 欧洲精品毛片网站| 色综合久久中文字幕综合网小说| 精品欧美一区二区三区| 欧美裸体男粗大视频在线观看| www.久久草.com| 亚洲国产成人一区| 这里精品视频免费| 亚洲国产精品一区二区久| 日韩精品视频免费专区在线播放| 国模gogo一区二区大胆私拍| 欧美乱大交xxxxx另类电影| 不卡av电影在线观看| 久久久久久久久网站| 亚洲国产天堂网精品网站| 亚洲欧美日韩精品久久亚洲区| 国产精品福利片| 亚洲人成电影网| 久久久亚洲精选| 97精品欧美一区二区三区| 国产精品678| 国产一区二区三区在线播放免费观看| 日韩亚洲欧美成人| 国产精品夫妻激情| 国产午夜精品美女视频明星a级| 热草久综合在线| 欧美精品成人91久久久久久久| 91欧美日韩一区| 亚洲韩国青草视频| 欧美亚洲日本黄色| 91经典在线视频| 538国产精品一区二区免费视频| 亚洲自拍偷拍色片视频| 亚洲精品二三区| 欧美激情欧美激情| 日本最新高清不卡中文字幕| 98午夜经典影视| 日韩性xxxx爱| 北条麻妃一区二区在线观看| 在线观看成人黄色| 日韩精品中文字幕久久臀|