2013年6月20日 星期四

Detect Android App Not In Foreground

在 iOS 只要寫在 UIApplicationDelegate 的 applicationdidenterbackground: 就可以。
但是在 Android 要做到相同功能就沒這麼簡單了。

其中用 getRunningTasks 又只限定在 debug 時可以用,真正上到 Google Play 是不能用的。因此就必須要在 onStop() 和 onWindowFocusChange(boolean) 做一些狀態判斷,看看是不是有任何一個 Activity 在前景。

參考:
http://vardhan-justlikethat.blogspot.in/2013/05/android-solution-to-detect-when-android.html