本文主要是介绍手持ActivityLifecycleCallbacks,监听前后台状态如此舒服,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
之前写过一小文,Android 程序打开即启动指定页面,比如密码检查页。,实现方式麻烦琐碎,后来Alex_Cin留言说可以考虑采用Application.ActivityLifecycleCallbacks来实现 监听程序处于前台还是后台
,阅读学习后,大呼过瘾。很早就想把这个写成博客记录分享给各位朋友,但是因为个人原因迟迟未完成,现在补上。
关于监听程序处于前台还是后台
- 弄一个BaseActivity,onResume等方法加1减1?这样不好,bug还是有的。
- 获得所有程序的列表,判断当前程序是否至于前台?不好,而且新SDK有所限制。
- 广播+服务,牛刀杀鸡。
所以,Application.ActivityLifecycleCallbacks登场,清脆利索,简单大气。
老规矩,先看图:
ges.gif
结构图
Paste_Image.png
菜已上,酒已喝。说说菜怎么做的吧。
一、Application.ActivityLifecycleCallbacks
Application通过ActivityLifecycleCallbacks使用接口提供了一套回调方法,用于让开发者对Activity的生命周期事件进行集中处理。 ActivityLifecycleCallbacks接口回调可以简化监测Activity的生命周期事件,在一个类中作统一处理。 ActivityLifecycleCallbacks使用要求API 14+ (Android 4.0+)。
既然是接口,那么来实现以下看看怎样呗
随便写一个类,比如名为TestAppCb,实现Application.ActivityLifecycleCallbacks接口
public class TestAppCb implements Application.ActivityLifecycleCallbacks {@Overridepublic void onActivityCreated(Activity activity, Bundle savedInstanceState) {}@Overridepublic void onActivityStarted(Activity activity) {}@Overridepublic void onActivityResumed(Activity activity) {}@Overridepublic void onActivityPaused(Activity activity) {}@Overridepublic void onActivityStopped(Activity activity) {}@Overridepublic void onActivitySaveInstanceState(Activity activity, Bundle outState) {}@Overridepublic void onActivityDestroyed(Activity activity) {}
}
我们发现了很多跟Activity很声明周期很相似的方法。作用不赘述,下面会附上一个名为ForegroundCallbacks的实现类,自行参考。
以往若需监测Activity的生命周期事件代码,你可能是这样做的,重写每一个Acivity的onResume(),然后作统计和处理,ActivityLifecycleCallbacks接口回调可以简化这一繁琐过程,在一个类中作统一处理。
二、具体使用
核心封装类,根据下面的代码的封装,结合自定义的Application,就可以轻松地实现监听程序处于前台还是后台的需求。
public class ForegroundCallbacks implements Application.ActivityLifecycleCallbacks {public static final long CHECK_DELAY = 500;public static final String TAG = ForegroundCallbacks.class.getName();public interface Listener {public void onBecameForeground();public void onBecameBackground();}private static ForegroundCallbacks instance;private boolean foreground = false, paused = true;private Handler handler = new Handler();private List<Listener> listeners = new CopyOnWriteArrayList<Listener>();private Runnable check;public static ForegroundCallbacks init(Application application){if (instance == null) {instance = new ForegroundCallbacks();application.registerActivityLifecycleCallbacks(instance);}return instance;}public static ForegroundCallbacks get(Application application){if (instance == null) {init(application);}return instance;}public static ForegroundCallbacks get(Context ctx){if (instance == null) {Context appCtx = ctx.getApplicationContext();if (appCtx instanceof Application) {init((Application)appCtx);}throw new IllegalStateException("Foreground is not initialised and " +"cannot obtain the Application object");}return instance;}public static ForegroundCallbacks get(){if (instance == null) {throw new IllegalStateException("Foreground is not initialised - invoke " +"at least once with parameterised init/get");}return instance;}public boolean isForeground(){return foreground;}public boolean isBackground(){return !foreground;}public void addListener(Listener listener){listeners.add(listener);}public void removeListener(Listener listener){listeners.remove(listener);}@Overridepublic void onActivityResumed(Activity activity) {paused = false;boolean wasBackground = !foreground;foreground = true;if (check != null)handler.removeCallbacks(check);if (wasBackground){L.d ("went foreground");for (Listener l : listeners) {try {l.onBecameForeground();} catch (Exception exc) {L.d ("Listener threw exception!:"+exc.toString());}}} else {L.d ("still foreground");}}@Overridepublic void onActivityPaused(Activity activity) {paused = true;if (check != null)handler.removeCallbacks(check);handler.postDelayed(check = new Runnable(){@Overridepublic void run() {if (foreground && paused) {foreground = false;L.d ("went background");for (Listener l : listeners) {try {l.onBecameBackground();} catch (Exception exc) {L.d ("Listener threw exception!:"+exc.toString());}}} else {L.d ("still foreground");}}}, CHECK_DELAY);}@Overridepublic void onActivityCreated(Activity activity, Bundle savedInstanceState) {}@Overridepublic void onActivityStarted(Activity activity) {}@Overridepublic void onActivityStopped(Activity activity) {}@Overridepublic void onActivitySaveInstanceState(Activity activity, Bundle outState) {}@Overridepublic void onActivityDestroyed(Activity activity) {}
}
.
.
MyApplication 结合Application.ActivityLifecycleCallbacks
监听如此轻松
public class MyApplication extends Application{@Overridepublic void onCreate() {super.onCreate();ForegroundCallbacks.init(this);ForegroundCallbacks.get().addListener(new ForegroundCallbacks.Listener() {@Overridepublic void onBecameForeground() {L.d("当前程序切换到前台");if(CacheUtils.getBoolean(getApplicationContext(), MyConst.GESTRUE_IS_LIVE)){L.d("已经开启手势锁");Intent intent = new Intent(getApplicationContext(), CheckGesPwdActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(intent);}else{}}@Overridepublic void onBecameBackground() {L.d("当前程序切换到后台");}});}
}
如上,即可,手势库可以直接引用做个小demo。
整个过程实现起来都是相当轻松的,亮点是ForegroundCallbacks,里面的代码值得咀嚼和学习。
本文粗糙了点,哈哈哈哈,不过我为什么感觉我还是挺良心的,哈哈哈哈。
附上github链接GestureLockMaster
三、单个Activity单独使用?
就在一个页面监听,当然可以。
public class TestLifeAc extends AppCompatActivity implements ForegroundCallbacks.Listener {@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_city_list);// 注册监听ForegroundCallbacks.get(this).addListener(this);}@Overrideprotected void onDestroy() {super.onDestroy();// 移除监听ForegroundCallbacks.get(this).removeListener(this);}@Overridepublic void onBecameForeground(Activity activity) {// 切换为前台}@Overridepublic void onBecameBackground(Activity activity) {//切换为后台}
}
四,参考学习
Is my Android app currently foreground or background?
原文地址:https://www.jianshu.com/p/e7f64e6bc2cc
这篇关于手持ActivityLifecycleCallbacks,监听前后台状态如此舒服的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!