Android中應用前后臺切換監(jiān)聽的實現詳解
前言
最近在工作中遇到了這么一個需求:如何實現 Android 應用前后臺切換的監(jiān)聽?下面來一起看看詳細的介紹:
iOS 內邊是可以實現的,AppDelegate 給了一個回調監(jiān)聽:
@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)注釋。一個iOS應用周期,大概的流程是這樣的。
應用從前臺進入到后臺:
applicationWillResignActive() -> applicationDidEnterBackground()
應用從后臺恢復到前臺:
applicationWillEnterForeground() -> applicationDidBecomeActive()
Android 中也存在 Application,但是并沒有提供前后臺切換的監(jiān)聽。
倒不如說,在 Android 中,壓根就沒有應用前后臺的概念。
Android 中基本頁面單位是 Activity。
Activity 有自己的生命周期,但是 Application 卻沒有一個整體的生命周期。
我們可以通過監(jiān)聽 Activity 的生命周期,來模擬實現一個 Application 的生命周期。
Activity 的生命周期不在闡述,寫過 Android 的都應該知道。
我們假設現在有兩個 Activity 分別是 A 和 B,A 是啟動頁面,那么生命周期回調是這樣的:(我們忽略掉一些不關心的回調)
A 被啟動或者 A 進入前臺
A.onStart() A.onResume()
從 A 跳轉到 B:
A.onPause() B.onStart() B.onResume() A.onStop()
從 B 返回 A:
B.onPause() A.onStart() A.onResume() B.onStop()
A 被關閉或者 A 進入后臺
A.onPause() A.onStop()
注意上面兩個頁面回調的啟動順序。
onResume 和 onPause 是一組,兩個頁面之間是順序調用。
onStart 和 onStop 是一組,兩個頁面之間是交叉調用。
也就是說,A 啟動到 B,會先調用 B.onStart() ,然后再調用 A.onStop() ;而 B 返回 A 則是相反的,會先調用 A.onStart() ,然后再調用 B.onStop() 。
利用這個特性,我們可以做一個全局計數器,來記錄前臺頁面的數量,在所有 Activity.onStart() 中計數器 +1,在所有 Activity.onStop() 中計數器 -1。計數器數目大于0,說明應用在前臺;計數器數目等于0,說明應用在后臺。計數器從1變成0,說明應用從前臺進入后臺;計數器從0變成1,說明應用從后臺進入前臺。
有了思路,我們來實現。
Application 提供了一個監(jiān)聽器用于監(jiān)聽整個應用中 Activity 聲明周期:Application.ActivityLifecycleCallbacks 。
這個監(jiān)聽器要求 API >= 14。對應 API < 14 的情況,可以通過編寫一個 BaseActivity,然后讓所有的 Activity 都集成這個類來實現整個應用 Activity 聲明周期的監(jiān)聽,效果是相同的。
API >= 14,實現如下:
public class ApplicationListener implements Application.ActivityLifecycleCallbacks {
private int foregroundCount = 0; // 位于前臺的 Activity 的數目
@Override
public void onActivityStarted(final Activity activity) {
if (foregroundCount <= 0) {
// TODO 這里處理從后臺恢復到前臺的邏輯
}
foregroundCount++;
}
@Override
public void onActivityStopped(Activity activity) {
foregroundCount--;
if (foregroundCount <= 0) {
// TODO 這里處理從前臺進入到后臺的邏輯
}
}
/*
* 下面回調,我們都不需要
*/
@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) {}
}
我們在 Application 中注冊這個監(jiān)聽器來發(fā)揮效果:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ApplicationListener());
}
}
對于 API < 14 的情況,BaseActivity 實現如下:
public class BaseActivity extends AppCompatActivity {
private static int foregroundCount = 0; // 注意是個靜態(tài)變量
@Override
protected void onStart() {
super.onStart();
if (foregroundCount <= 0) {
// TODO 這里處理從后臺恢復到前臺的邏輯
}
foregroundCount++;
}
@Override
protected void onStop() {
foregroundCount--;
if (foregroundCount <= 0) {
// TODO 這里處理從前臺進入到后臺的邏輯
}
super.onStop();
}
}
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
解決VSCode調試react-native android項目錯誤問題
這篇文章主要介紹了VSCode調試react-native android項目錯誤解決辦法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
Android開發(fā)中RecyclerView模仿探探左右滑動布局功能
本文給大家分享android開發(fā)中RecyclerView模仿探探左右滑動布局功能,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01
Android利用ContentProvider獲取聯系人信息
這篇文章主要為大家詳細介紹了Android利用ContentProvider獲取聯系人信息,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
規(guī)避Android開發(fā)中內存泄漏陷阱的解決方案
在Android開發(fā)中,內存泄漏是一個常見但容易被忽視的問題,它會導致應用程序占用過多的內存資源,最終影響應用的性能和用戶體驗,本文將深入探討Android常見的內存泄漏問題,并提供優(yōu)化指南,需要的朋友可以參考下2024-05-05
Android中TextView動態(tài)設置縮進距離的方法
項目需求如果在項目中第一行文字需要添加布局的情況我們應該怎么做呢,經過一番考慮和查找我最終選擇了縮進的方式解決這個問題,這篇文章主要給大家介紹了關于Android中TextView動態(tài)設置縮進距離的相關資料,需要的朋友可以參考下2022-04-04

