如何在Android应用程序中实现应用程序空闲超时? [英] How to implement application idle timeout in android application?

查看:78
本文介绍了如何在Android应用程序中实现应用程序空闲超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下情况下是否可以实现超时功能?

Is there a way to implement timeout feature in the following scenarios?

具有html页面和本机屏幕的Web应用程序.

A web application with html pages and native screens.

1.当应用程序在后台运行5分钟时->销毁该应用程序. 2.当应用程序位于前台但5分钟未收到任何用户交互时->销毁该应用程序.

1.When the application is in the background for 5 min -> destroy the application. 2.When the application is in the foreground but not receiving any user interaction for 5 min ->destroy the application.

推荐答案

关于背景状态:

默认情况下,无需手动终止应用程序的进程.如果需要释放其他应用程序的资源,则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.

这篇关于如何在Android应用程序中实现应用程序空闲超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆