如何检测当一个Android应用程序转到后台,回来到前台转到、前台、应用程序、后台

2023-09-12 00:04:03 作者:月淺思念深

我试图写一个应用程序,并具体的东西时,它一定的时间后,带回了前台。有没有一种方法来检测,当一个应用程序发送到后台或提请前景?

I am trying to write an app that does something specific when it is brought back to the foreground after some amount of time. Is there a way to detect when an app is sent to the background or brought to the foreground?

推荐答案

的onPause() onResume()方法被调用时,应用程序被带到背景并进入前景再次。然而,它们也被称为当应用程序被启动的第一时间,它被杀死之前。你可以阅读更多的 活动 的。

The onPause() and onResume() methods are called when the application is brought to the background and into the foreground again. However, they are also called when the application is started for the first time and before it is killed. You can read more in Activity.

没有任何直接的方法来获得在背景或前景,而应用程序的状态,但即使我面临这个问题,并发现了 onWindowFocusChanged 解决方案和的onStop

There isn't any direct approach to get the application status while in the background or foreground, but even I have faced this issue and found the solution with onWindowFocusChanged and onStop.

有关详细信息检查这里的 Android:解决方法来检测当一个Android应用程序转到后台,回来到前台没有getRunningTasks或getRunningAppProcesses 的。

For more details check here Android: Solution to detect when an Android app goes to the background and come back to the foreground without getRunningTasks or getRunningAppProcesses.