Android开发检测App从后台进入前台的解决方法
Posted yongfengnice
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android开发检测App从后台进入前台的解决方法相关的知识,希望对你有一定的参考价值。
public class ActLifecycle implements ActivityLifecycleCallbacks { private int mPreActivityStartedCount = 0; private int mActivityStartedCount = 0; private int mActivityOrientation = Configuration.ORIENTATION_UNDEFINED; @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { } @Override public void onActivityStarted(Activity activity) { mPreActivityStartedCount = mActivityStartedCount; mActivityStartedCount++; int orientation = getOrientation(activity); if (mPreActivityStartedCount == 0 && mActivityStartedCount == 1 && orientation == mActivityOrientation) { handleAppForeground(activity);//app从后台进入前台了,在这个自定义的方法里处理具体的业务 } mActivityOrientation = orientation; } private int getOrientation(Activity activity) { if (activity != null && activity.getResources() != null) { Configuration configuration = activity.getResources().getConfiguration(); if (configuration != null) { return configuration.orientation; } } return Configuration.ORIENTATION_UNDEFINED; } @Override public void onActivityResumed(Activity activity) { } @Override public void onActivityPaused(Activity activity) { } @Override public void onActivityStopped(Activity activity) { mPreActivityStartedCount = mActivityStartedCount; mActivityStartedCount--; } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { } @Override public void onActivityDestroyed(Activity activity) { } }
//注册app生命周期回调函数
application.registerActivityLifecycleCallbacks(new ActLifecycle());
以上是关于Android开发检测App从后台进入前台的解决方法的主要内容,如果未能解决你的问题,请参考以下文章
Android性能优化第(八)篇---App启动速度优化之耗时检测处理