woong's
홈 버튼 오버라이드 본문
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | public class MyApplication extends Application { // ... @Override public void onCreate() { super.onCreate(); registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() { // ... @Override public void onActivityResumed(Activity activity) { if (isBackground) { isBackground = false; notifyForeground(); } } // ... }); } // ... } Tying It All Together Alright, here is a more complete sample to be notified when the app enters foreground and is sent to background. public class MyApplication extends Application { // Starts as true in order to be notified on first launch private boolean isBackground = true; @Override public void onCreate() { super.onCreate(); listenForForeground(); listenForScreenTurningOff(); } private void listenForForeground() { registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { //... @Override public void onActivityResumed(Activity activity) { if (isBackground) { isBackground = false; notifyForeground(); } } //... }); } private void listenForScreenTurningOff() { IntentFilter screenStateFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF); registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { isBackground = true; notifyBackground(); } }, screenStateFilter); } @Override public void onTrimMemory(int level) { super.onTrimMemory(level); if (level == TRIM_MEMORY_UI_HIDDEN) { isBackground = true; notifyBackground(); } } private void notifyForeground() { // This is where you can notify listeners, handle session tracking, etc } private void notifyBackground() { // This is where you can notify listeners, handle session tracking, etc } public boolean isBackground() { return isBackground; } } | cs |
'Develop > Android' 카테고리의 다른 글
Android Trasitions API 사용하기 (0) | 2016.10.12 |
---|---|
Android 국가별 폴더 분기 (0) | 2016.10.10 |
Android Zxing 사용하기 (1) | 2016.05.10 |
Android CoordinatorLayout Behavior 사용하기 (0) | 2016.03.30 |
Android Templates 사용하기 (0) | 2016.03.30 |
Comments