android 监听前台,Android 前台与后台监听

1 packagecom.triaged.badge.helpers;2

3 importandroid.app.Activity;4 importandroid.app.Application;5 importandroid.content.Context;6 importandroid.os.Bundle;7 importandroid.os.Handler;8

9 importcom.triaged.badge.app.App;10

11 importjava.util.List;12 importjava.util.concurrent.CopyOnWriteArrayList;13

14 /**

15 * Usage:16 *17 * 1. Get the Foreground Singleton, passing a Context or Application object unless you18 * are sure that the Singleton has definitely already been initialised elsewhere.19 *20 * 2.a) Perform a direct, synchronous check: Foreground.isForeground() / .isBackground()21 *22 * or23 *24 * 2.b) Register to be notified (useful in Service or other non-UI components):25 *26 * Foreground.Listener myListener = new Foreground.Listener(){27 * public void onBecameForeground(){28 * // ... whatever you want to do29 * }30 * public void onBecameBackground(){31 * // ... whatever you want to do32 * }33 * }34 *35 * public void onCreate(){36 * super.onCreate();37 * Foreground.get(this).addListener(listener);38 * }39 *40 * public void onDestroy(){41 * super.onCreate();42 * Foreground.get(this).removeListener(listener);43 * }44 */

45 public class Foreground implementsApplication.ActivityLifecycleCallbacks {46

47 public static final long CHECK_DELAY = 500;48 public static final String TAG = Foreground.class.getName();49

50 public interfaceListener {51

52 public voidonBecameForeground();53

54 public voidonBecameBackground();55

56 }57

58 private staticForeground instance;59

60 private boolean foreground = false, paused = true;61 private Handler handler = newHandler();62 private List listeners = new CopyOnWriteArrayList();63 privateRunnable check;64

65 /**

66 * Its not strictly necessary to use this method - _usually_ invoking67 * get with a Context gives us a path to retrieve the Application and68 * initialise, but sometimes (e.g. in test harness) the ApplicationContext69 * is != the Application, and the docs make no guarantees.70 *71 *@paramapplication72 *@returnan initialised Foreground instance73 */

74 public staticForeground init(Application application){75 if (instance == null) {76 instance = newForeground();77 application.registerActivityLifecycleCallbacks(instance);78 }79 returninstance;80 }81

82 public staticForeground get(Application application){83 if (instance == null) {84 init(application);85 }86 returninstance;87 }88

89 public staticForeground get(Context ctx){90 if (instance == null) {91 Context appCtx =ctx.getApplicationContext();92 if (appCtx instanceofApplication) {93 init((Application)appCtx);94 }95 throw newIllegalStateException(96 "Foreground is not initialised and " +

97 "cannot obtain the Application object");98 }99 returninstance;100 }101

102 public staticForeground get(){103 if (instance == null) {104 throw newIllegalStateException(105 "Foreground is not initialised - invoke " +

106 "at least once with parameterised init/get");107 }108 returninstance;109 }110

111 public booleanisForeground(){112 returnforeground;113 }114

115 public booleanisBackground(){116 return !foreground;117 }118

119 public voidaddListener(Listener listener){120 listeners.add(listener);121 }122

123 public voidremoveListener(Listener listener){124 listeners.remove(listener);125 }126

127 @Override128 public voidonActivityResumed(Activity activity) {129 paused = false;130 boolean wasBackground = !foreground;131 foreground = true;132

133 if (check != null)134 handler.removeCallbacks(check);135

136 if(wasBackground){137 App.gLogger.i( "went foreground");138 for(Listener l : listeners) {139 try{140 l.onBecameForeground();141 } catch(Exception exc) {142 App.gLogger.e( "Listener threw exception!", exc);143 }144 }145 } else{146 App.gLogger.i( "still foreground");147 }148 }149

150 @Override151 public voidonActivityPaused(Activity activity) {152 paused = true;153

154 if (check != null)155 handler.removeCallbacks(check);156

157 handler.postDelayed(check = newRunnable(){158 @Override159 public voidrun() {160 if (foreground &&paused) {161 foreground = false;162 App.gLogger.i( "went background");163 for(Listener l : listeners) {164 try{165 l.onBecameBackground();166 } catch(Exception exc) {167 App.gLogger.e( "Listener threw exception!", exc);168 }169 }170 } else{171 App.gLogger.i( "still foreground");172 }173 }174 }, CHECK_DELAY);175 }176

177 @Override178 public voidonActivityCreated(Activity activity, Bundle savedInstanceState) {}179

180 @Override181 public voidonActivityStarted(Activity activity) {}182

183 @Override184 public voidonActivityStopped(Activity activity) {}185

186 @Override187 public voidonActivitySaveInstanceState(Activity activity, Bundle outState) {}188

189 @Override190 public voidonActivityDestroyed(Activity activity) {}191 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值