简体   繁体   English

当我的整个应用程序都在后台时,我如何在服务中知道

[英]How can i know in my service when my entire app is in the background

Mainly all I want to do is that when the user uses my app and presses the home button or just exits the app (doesn't matter which way he exits), the next time he enters the app I want to display the first page of my app (instead of the last screen he was in). 我主要要做的是,当用户使用我的应用程序并按下主屏幕按钮或只是退出该应用程序(无论退出方式如何)时,下次他进入该应用程序时,我要显示的第一页我的应用程序(而不是他所在的最后一个屏幕)。

I want to do this: 我想做这个:

  • when the user exits the app I will delete it from the memory so the app service will keep on going 当用户退出应用程序时,我将从内存中将其删除,因此应用程序服务将继续进行
  • but the next time the user enters the app it will start from the first page 但是下次用户进入应用程序时,它将从首页开始

Thanks. 谢谢。

Basically the problem is that you need to know if your app was sent to the background, or not. 基本上,问题在于您需要知道您的应用是否发送到后台。

If it was sent to background, then when any Activity is started, it loads your "first page" activity in onCreate() before exiting. 如果将其发送到后台,则在启动任何Activity时,它将在退出之前将其“首页”活动加载到onCreate()


Check out some of the solutions in this question I asked some time ago - a lot of the suggestions are really good: 看看我前段时间提出的这个问题中的一些解决方案-很多建议都很好:

Some of the answers may suit you better than others. 有些答案可能比其他答案更适合您。

There are 2 basic ideas: 有2个基本思路:

  1. in onPause() , check if your app is one of the ones that is running (see link below) onPause() ,检查您的应用程序是否是正在运行的应用程序之一(请参阅下面的链接)
  2. in onPause() and in onResume() , get a timestamp. onPause()onResume() ,获取一个时间戳。 If the times differ by more than a few millis, then you can assume the app was sent to background. 如果时间相差几毫秒,则可以假定该应用已发送到后台。 This can easily be done by creating a base Activity subclass from which all your other activities inherit. 这可以通过创建基本Activity子类轻松完成,所有其他活动都将从该子类继承。

I went with an answer similar to (1) above, on this question: 在这个问题上,我得到了与上面的(1)类似的答案:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM