java 是否可以检测应用程序的退出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16298795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to detect exit of an application?
提问by Devu Soman
My android application allows to launch other installed applications from this.This shows some allowed apps. If the user try to launch a disallowed application then show a message and go back to my activity (from where each application launch) using running tasks. My application act as a home launcher.So intent to this activity if the is a blocked application.For eg: It is possible to launch Camera from Gallery in Samsung device.If the camera is not an allowed one shows blocked message and exited to my acivity.But when relaunching Gallery the blocked message shows again beause the top activity(Camera activity) lied in the stack.
我的 android 应用程序允许从此启动其他已安装的应用程序。这显示了一些允许的应用程序。如果用户尝试启动不允许的应用程序,则显示一条消息并使用正在运行的任务返回我的活动(从每个应用程序启动的位置)。我的应用程序充当家庭启动器。所以如果它是一个被阻止的应用程序,那么打算进行此活动。例如:可以从三星设备中的图库启动相机。如果不允许使用相机,则显示被阻止的消息并退出到我的活动。但是当重新启动图库时,被阻止的消息再次显示,因为顶部活动(相机活动)位于堆栈中。
But the exiting of these blocked application did not work perfectly.
但是这些被阻止的应用程序的退出并不完美。
Is it possible to get close/exit event of an application?
How can i finish an application as whole(By finishing all its applications).
How to launch an application without having any history of previous launch?
是否有可能获得应用程序的关闭/退出事件?
我如何才能完成整个应用程序(通过完成其所有应用程序)。
如何在没有任何先前启动历史的情况下启动应用程序?
Thanks in Advance
提前致谢
回答by Triode
Is it possible to get close/exit event of an application?
是否有可能获得应用程序的关闭/退出事件?
Yes it is possible inside your LauncherActivity
You can override
onDestroy
this method will be called on application exit.
是的,在您的内部是可能的,LauncherActivity
您可以override
onDestroy
在应用程序退出时调用此方法。
How can i finish an application as whole(By finishing all its applications)?
I believe you want to stop your all running activities here. This can be achieved in multiple ways.
我相信你想在这里停止你所有的跑步活动。这可以通过多种方式实现。
android.os.Process.killProcess(android.os.Process.myPid());
or
或者
Intent intent = new Intent(getApplicationContext(), YourHomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will clear all the activities and will brings the user to the HomeActivity
. While calling this you can add a flag in intent and using that value you can finish the HomeActivity
also. Use finish()
method to finish the activity.
这将清除所有活动并将用户带到HomeActivity
. 在调用它时,您可以在意图中添加一个标志,并使用该值也可以完成HomeActivity
。使用finish()
方法完成活动。
How to launch an application without having any history of previous launch?
如何在没有任何先前启动历史的情况下启动应用程序?
You can use the same above Solution to achieve this. The second one.
您可以使用上述相同的解决方案来实现这一点。第二个。
Hope this will help.
希望这会有所帮助。
There is an onTerminate
method in application class, but this cannot be used in production environment. See more about this here
onTerminate
应用程序类中有一个方法,但是在生产环境中不能使用。在此处查看更多相关信息