禁止应用程序在后台运行
有什么方法可以禁止我的应用程序在后台运行吗? 我不希望我的应用程序在后台运行,我需要在用户完成后完全关闭它
Is there any way to disable my application from running on the background?
I don't want my app to run in the background, i need to completely close it when the user a done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您关闭所有活动时,正确调用
finish()
。如果您启动了任何需要结果值的活动,请务必在完成之前在这些活动中调用setResult()
。如果您有任何线程正在运行循环,请保留一个全局变量来指示您的应用程序是否正在运行。当您的活动恢复(调用
onResume()
)时,将此变量设置为 true;当您的活动暂停(onPause()
)时,将该变量设置为 false。然后让线程的循环在循环之前检查此全局变量,以确保它们在活动完成后正常终止。除此之外,当用户按下堆栈中最后一个活动的后退按钮时,您的活动应该自动尝试完成,从而终止您的应用程序。
Properly call
finish()
on all your activities when you close them. If you started any activities expecting a result value, be sure to callsetResult()
in those activities before finishing.If you have any threads running loops, keep a global variable indicating if your app is running. Set this variable to true when your activity resumes (invokes
onResume()
), and set the variable to false when your activity pauses (onPause()
). Then just have your threads' loops check this global variable before looping to make sure they terminate gracefully once your activity finishes.Other than that, your activities should automatically try to finish when the user presses the back button on the last activity in the stack, terminating your application.