Created
November 4, 2016 09:26
-
-
Save HassakuTb/1648e625b0127a46aeec60c678185a05 to your computer and use it in GitHub Desktop.
Checking if the Android application was in foreground.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package jp.tangerinebox.check_is_foreground; | |
| import android.app.Activity; | |
| public abstract class BaseActivity extends Activity{ | |
| @Override | |
| protected void onStart(){ | |
| super.onStart(); | |
| HogeApplication.getInstance().onActivityStart(); | |
| } | |
| @Override | |
| protected void onStop(){ | |
| super.onStop(); | |
| HogeApplication.getInstance().onActivityStop(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package jp.tangerinebox.check_is_foreground; | |
| import android.app.Application; | |
| public class HogeApplication extends Application{ | |
| // ==== Singleton用 ==== | |
| private static HogeApplication sInstance = null; | |
| public static HogeApplication getInstance(){ | |
| return sInstance; | |
| } | |
| @Override | |
| public void onCreate(){ | |
| super.onCreate(); | |
| sInstance = this; | |
| } | |
| // ==== フォアグラウンドチェック ==== | |
| private int mStartCount = 0; | |
| private int mStopCount = 0; | |
| void onActivityStart(){ | |
| ++ mStartCount; | |
| } | |
| void onActivityStop(){ | |
| ++ mStopCount; | |
| } | |
| public boolean isForeground(){ | |
| return mStartCount > mStopCount; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment