Is it possible to detect exit of an application?

点点圈 提交于 2019-12-19 05:34:08

问题


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.

But the exiting of these blocked application did not work perfectly.

  1. Is it possible to get close/exit event of an application?

  2. How can i finish an application as whole(By finishing all its applications).

  3. How to launch an application without having any history of previous launch?

Thanks in Advance


回答1:


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.

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.

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



来源:https://stackoverflow.com/questions/16298795/is-it-possible-to-detect-exit-of-an-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!