-
[AOS] Check App Status (Background or Foreground)2023. 6. 30. 15:18๋ฐ์ํ๐ฎ๐ปโ๏ธ์ฑ์์ ํ ์ง์ ํ๋ค๊ฐ ๋ค์ ์ง์ ์์ ์ฌ์ฉ์ ์ธ์ฆ์ ์ํ๋ค์? ๋ณด์์ด ๋๋ฌด ์ฝํ๊ฑฐ ์๋๊ฐ์?๋ค...? ๋ค... ๋ณด์ ๊ฐํํ๊ฒ ์ต๋๋ค... ๐ญ๐ถ
์ฑ ์ํ๊ฐ Background์ธ์ง Foreground์ธ์ง ์ํ๋ฅผ ์ฒดํฌํ์ฌ ๊ฐ๊ฐ์ ์ํ์ ๋ฐ๋ฅธ action์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ 2๊ฐ์ง๊ฐ ์๋ค.
Observer๋ฅผ ์์ฑํ๊ณ , Androidx.lifecycle ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ implementationํ์ฌ processLifecycleOwner๋ฅผ ์ด์ฉํ ๋ฐฉ๋ฒ๊ณผ,
๊ฐ๋จํ๊ฒ ActivityManager์ RunningAppProcessInfo ๊ฐ์ ์ด์ฉํ ๋ฐฉ๋ฒ์ด ์๋ค.
ProcessLifecycleOwner
1. implementation for ProcessLifecycleOwner
dependencies { ... implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation "android.arch.lifecycle:extensions:1.1.1" implementation 'android.arch.lifecycle:common:1.1.1' ... }
2. Observer ํด๋์ค ์์ฑํ๊ธฐ
class OnAppObserver:LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_START) fun onStart() { // to do when app is in foreground } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) fun onStop() { // to do when app is in background } }
3. Observer ์ค์
class MainApplication : Application() { override fun onCreate() { super.onCreate() ProcessLifecycleOwner.get().lifecycle.addObserver(OnAppObserver()) ... } }
ActivityManager.RunningAppProcessInfo
override fun onStop() { super.onStop() val processInfo = RunningAppProcessInfo() ActivityManager.getMyMemoryState(processInfo) if (processInfo.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { // to do when app is in background } }
์ ์ฝ๋๋ onCreate()์ ๊ตฌํํ๋ฉด ํญ์ Foreground ์ํ๋ก ์ฒดํฌ๋๊ธฐ ๋๋ฌธ์, ์ฑ์ด background ์ํ๋ก ์งํ๋ ๋ ํธ์ถ๋๋ onStop() ํจ์๋ฅผ overrideํ์ฌ ๊ตฌํํด์ฃผ์๋ค.
๋ฐ์ํ'๐ฐ Android ๐ญ > ๐ ๊ฐ๋ฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[AOS] Android Screen ON / OFF Detect (0) 2023.06.30 [AOS] Kotlin ์ค์ฝํ ํจ์ (Scope Function) (0) 2023.04.28 [AOS] ์ด๋ฏธ์ง ๋ฌธ์๋ฉ์์ง ์ ์กํ๊ธฐ (0) 2023.04.14 [AOS] Android DeepLink ์ค์ (0) 2022.11.23 [AOS] ForegroundService (0) 2022.11.15