且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android应用程序中实现应用程序空闲超时?

更新时间:2023-02-07 08:44:20

关于背景状态:

默认情况下,无需手动终止应用程序的进程.如果需要释放其他应用程序的资源,则Android操作系统会自行执行此操作.

There is no need to kill the app's process manually by default. The Android OS does this by itself if there is a need to free up resources for the other applications.

请参见本指南以作为参考.

尽管如果您需要在这段空闲时间"内执行一些后台工作,则可以启动

Though if you need to perform some background work during this "idle time", you may start a Service to perform those operations and then stop it from code.

关于前景状态:

我认为在此使用的***方法是将消息发送到 Handler 进入应用程序主线程,因为您不知道用户离开后是否会再次与UI进行交互.当用户返回UI时,您可以使用Handler的

I think the best approach to use here is to send Messages to a Handler of the Main thread of your application, since you do not know if the user will interact with the UI again after they leave. When the user comes back to the UI, you may clear the message queue, using Handler's removeMessages method.

我不建议您使用Android中的System.exit(0)完成该过程.

I do not recommend you to finish the process with System.exit(0) in Android.