Android中应用前后台切换监听的实现详解

所属分类: 软件编程 / Android 阅读数: 27
收藏 0 赞 0 分享

前言

最近在工作中遇到了这么一个需求:如何实现 Android 应用前后台切换的监听?下面来一起看看详细的介绍:

iOS 内边是可以实现的,AppDelegate 给了一个回调监听:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
 func applicationWillResignActive(_ application: UIApplication) {
 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
 }
 func applicationDidEnterBackground(_ application: UIApplication) {
 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
 }
 func applicationWillEnterForeground(_ application: UIApplication) {
 // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
 }
 func applicationDidBecomeActive(_ application: UIApplication) {
 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
 }
}

我保留了系统注释。一个iOS应用周期,大概的流程是这样的。

应用从前台进入到后台:

applicationWillResignActive() -> applicationDidEnterBackground()

应用从后台恢复到前台:

applicationWillEnterForeground() -> applicationDidBecomeActive()

Android 中也存在 Application,但是并没有提供前后台切换的监听。

倒不如说,在 Android 中,压根就没有应用前后台的概念。

Android 中基本页面单位是 Activity。

Activity 有自己的生命周期,但是 Application 却没有一个整体的生命周期。

我们可以通过监听 Activity 的生命周期,来模拟实现一个 Application 的生命周期。

Activity 的生命周期不在阐述,写过 Android 的都应该知道。

我们假设现在有两个 Activity 分别是 A 和 B,A 是启动页面,那么生命周期回调是这样的:(我们忽略掉一些不关心的回调)

A 被启动或者 A 进入前台

A.onStart()
A.onResume()

从 A 跳转到 B:

A.onPause()
B.onStart()
B.onResume()
A.onStop()

从 B 返回 A:

B.onPause()
A.onStart()
A.onResume()
B.onStop()

A 被关闭或者 A 进入后台

A.onPause()
A.onStop()

注意上面两个页面回调的启动顺序。

onResume 和 onPause 是一组,两个页面之间是顺序调用。

onStart 和 onStop 是一组,两个页面之间是交叉调用。

也就是说,A 启动到 B,会先调用 B.onStart() ,然后再调用 A.onStop() ;而 B 返回 A 则是相反的,会先调用 A.onStart() ,然后再调用 B.onStop()

利用这个特性,我们可以做一个全局计数器,来记录前台页面的数量,在所有 Activity.onStart() 中计数器 +1,在所有 Activity.onStop() 中计数器 -1。计数器数目大于0,说明应用在前台;计数器数目等于0,说明应用在后台。计数器从1变成0,说明应用从前台进入后台;计数器从0变成1,说明应用从后台进入前台。

有了思路,我们来实现。

Application 提供了一个监听器用于监听整个应用中 Activity 声明周期:Application.ActivityLifecycleCallbacks

这个监听器要求 API >= 14。对应 API < 14 的情况,可以通过编写一个 BaseActivity,然后让所有的 Activity 都集成这个类来实现整个应用 Activity 声明周期的监听,效果是相同的。

API >= 14,实现如下:

public class ApplicationListener implements Application.ActivityLifecycleCallbacks {
 private int foregroundCount = 0; // 位于前台的 Activity 的数目
 @Override
 public void onActivityStarted(final Activity activity) {
  if (foregroundCount <= 0) {
   // TODO 这里处理从后台恢复到前台的逻辑
  }
  foregroundCount++;
 }
 @Override
 public void onActivityStopped(Activity activity) {
  foregroundCount--;
  if (foregroundCount <= 0) {
   // TODO 这里处理从前台进入到后台的逻辑
  }
 }
 /*
  * 下面回调,我们都不需要
  */
 @Override
 public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
 @Override
 public void onActivityResumed(Activity activity) {}
 @Override
 public void onActivityPaused(Activity activity) {}
 @Override
 public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
 @Override
 public void onActivityDestroyed(Activity activity) {}
}

我们在 Application 中注册这个监听器来发挥效果:

public class MyApplication extends Application {
 @Override
 public void onCreate() {
  super.onCreate();
  registerActivityLifecycleCallbacks(new ApplicationListener());
 }
}

对于 API < 14 的情况,BaseActivity 实现如下:

public class BaseActivity extends AppCompatActivity {
 private static int foregroundCount = 0; // 注意是个静态变量
 @Override
 protected void onStart() {
  super.onStart();
  if (foregroundCount <= 0) {
   // TODO 这里处理从后台恢复到前台的逻辑
  }
  foregroundCount++;
 }
 @Override
 protected void onStop() {
  foregroundCount--;
  if (foregroundCount <= 0) {
   // TODO 这里处理从前台进入到后台的逻辑
  }
  super.onStop();
 }
}

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多