欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中應(yīng)用前后臺(tái)切換監(jiān)聽(tīng)的實(shí)現(xiàn)詳解

 更新時(shí)間:2017年07月02日 10:30:21   作者:TAKWOLF  
這篇文章主要給大家介紹了關(guān)于Android中應(yīng)用前后臺(tái)切換監(jiān)聽(tīng)實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。

前言

最近在工作中遇到了這么一個(gè)需求:如何實(shí)現(xiàn) Android 應(yīng)用前后臺(tái)切換的監(jiān)聽(tīng)?下面來(lái)一起看看詳細(xì)的介紹:

iOS 內(nèi)邊是可以實(shí)現(xiàn)的,AppDelegate 給了一個(gè)回調(diào)監(jiān)聽(tīng):

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
 func applicationWillResignActive(_ application: UIApplication) {
 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
 }
 func applicationDidEnterBackground(_ application: UIApplication) {
 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
 }
 func applicationWillEnterForeground(_ application: UIApplication) {
 // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
 }
 func applicationDidBecomeActive(_ application: UIApplication) {
 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
 }
}

我保留了系統(tǒng)注釋。一個(gè)iOS應(yīng)用周期,大概的流程是這樣的。

應(yīng)用從前臺(tái)進(jìn)入到后臺(tái):

applicationWillResignActive() -> applicationDidEnterBackground()

應(yīng)用從后臺(tái)恢復(fù)到前臺(tái):

applicationWillEnterForeground() -> applicationDidBecomeActive()

Android 中也存在 Application,但是并沒(méi)有提供前后臺(tái)切換的監(jiān)聽(tīng)。

倒不如說(shuō),在 Android 中,壓根就沒(méi)有應(yīng)用前后臺(tái)的概念。

Android 中基本頁(yè)面單位是 Activity。

Activity 有自己的生命周期,但是 Application 卻沒(méi)有一個(gè)整體的生命周期。

我們可以通過(guò)監(jiān)聽(tīng) Activity 的生命周期,來(lái)模擬實(shí)現(xiàn)一個(gè) Application 的生命周期。

Activity 的生命周期不在闡述,寫(xiě)過(guò) Android 的都應(yīng)該知道。

我們假設(shè)現(xiàn)在有兩個(gè) Activity 分別是 A 和 B,A 是啟動(dòng)頁(yè)面,那么生命周期回調(diào)是這樣的:(我們忽略掉一些不關(guān)心的回調(diào))

A 被啟動(dòng)或者 A 進(jìn)入前臺(tái)

A.onStart()
A.onResume()

從 A 跳轉(zhuǎn)到 B:

A.onPause()
B.onStart()
B.onResume()
A.onStop()

從 B 返回 A:

B.onPause()
A.onStart()
A.onResume()
B.onStop()

A 被關(guān)閉或者 A 進(jìn)入后臺(tái)

A.onPause()
A.onStop()

注意上面兩個(gè)頁(yè)面回調(diào)的啟動(dòng)順序。

onResume 和 onPause 是一組,兩個(gè)頁(yè)面之間是順序調(diào)用。

onStart 和 onStop 是一組,兩個(gè)頁(yè)面之間是交叉調(diào)用。

也就是說(shuō),A 啟動(dòng)到 B,會(huì)先調(diào)用 B.onStart() ,然后再調(diào)用 A.onStop() ;而 B 返回 A 則是相反的,會(huì)先調(diào)用 A.onStart() ,然后再調(diào)用 B.onStop() 。

利用這個(gè)特性,我們可以做一個(gè)全局計(jì)數(shù)器,來(lái)記錄前臺(tái)頁(yè)面的數(shù)量,在所有 Activity.onStart() 中計(jì)數(shù)器 +1,在所有 Activity.onStop() 中計(jì)數(shù)器 -1。計(jì)數(shù)器數(shù)目大于0,說(shuō)明應(yīng)用在前臺(tái);計(jì)數(shù)器數(shù)目等于0,說(shuō)明應(yīng)用在后臺(tái)。計(jì)數(shù)器從1變成0,說(shuō)明應(yīng)用從前臺(tái)進(jìn)入后臺(tái);計(jì)數(shù)器從0變成1,說(shuō)明應(yīng)用從后臺(tái)進(jìn)入前臺(tái)。

有了思路,我們來(lái)實(shí)現(xiàn)。

Application 提供了一個(gè)監(jiān)聽(tīng)器用于監(jiān)聽(tīng)整個(gè)應(yīng)用中 Activity 聲明周期:Application.ActivityLifecycleCallbacks 。

這個(gè)監(jiān)聽(tīng)器要求 API >= 14。對(duì)應(yīng) API < 14 的情況,可以通過(guò)編寫(xiě)一個(gè) BaseActivity,然后讓所有的 Activity 都集成這個(gè)類(lèi)來(lái)實(shí)現(xiàn)整個(gè)應(yīng)用 Activity 聲明周期的監(jiān)聽(tīng),效果是相同的。

API >= 14,實(shí)現(xiàn)如下:

public class ApplicationListener implements Application.ActivityLifecycleCallbacks {
 private int foregroundCount = 0; // 位于前臺(tái)的 Activity 的數(shù)目
 @Override
 public void onActivityStarted(final Activity activity) {
  if (foregroundCount <= 0) {
   // TODO 這里處理從后臺(tái)恢復(fù)到前臺(tái)的邏輯
  }
  foregroundCount++;
 }
 @Override
 public void onActivityStopped(Activity activity) {
  foregroundCount--;
  if (foregroundCount <= 0) {
   // TODO 這里處理從前臺(tái)進(jìn)入到后臺(tái)的邏輯
  }
 }
 /*
  * 下面回調(diào),我們都不需要
  */
 @Override
 public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
 @Override
 public void onActivityResumed(Activity activity) {}
 @Override
 public void onActivityPaused(Activity activity) {}
 @Override
 public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
 @Override
 public void onActivityDestroyed(Activity activity) {}
}

我們?cè)?Application 中注冊(cè)這個(gè)監(jiān)聽(tīng)器來(lái)發(fā)揮效果:

public class MyApplication extends Application {
 @Override
 public void onCreate() {
  super.onCreate();
  registerActivityLifecycleCallbacks(new ApplicationListener());
 }
}

對(duì)于 API < 14 的情況,BaseActivity 實(shí)現(xiàn)如下:

public class BaseActivity extends AppCompatActivity {
 private static int foregroundCount = 0; // 注意是個(gè)靜態(tài)變量
 @Override
 protected void onStart() {
  super.onStart();
  if (foregroundCount <= 0) {
   // TODO 這里處理從后臺(tái)恢復(fù)到前臺(tái)的邏輯
  }
  foregroundCount++;
 }
 @Override
 protected void onStop() {
  foregroundCount--;
  if (foregroundCount <= 0) {
   // TODO 這里處理從前臺(tái)進(jìn)入到后臺(tái)的邏輯
  }
  super.onStop();
 }
}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論