以编程方式关闭 android 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11386394/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:13:58  来源:igfitidea点击:

Close android application programmatically

android

提问by Mahmoud Emam

How can i close my application programmatically?

如何以编程方式关闭我的应用程序?

I used

我用了

   finish();

Or

或者

android.os.Process.killProcess(android.os.Process.myPid());

Or

或者

System.exit(0);

Or

或者

moveTaskToBack(true);

but it closed the current running activity, but i need to close the whole application??

但它关闭了当前正在运行的活动,但我需要关闭整个应用程序??

I need to close app to retrieve some memory and then restart application again.

我需要关闭应用程序以检索一些内存,然后再次重新启动应用程序。

Or

或者

does there is a way to clear all memory of the application?

有没有办法清除应用程序的所有内存?

回答by David

Using these lines in your activity:-

在您的活动中使用这些行:-

this.finish();
Process.killProcess( Process.myPid() ); 

will kill your whole application (assuming its running in a single process) it will also free any associated memory.

将终止您的整个应用程序(假设它在单个进程中运行)它还将释放任何相关的内存。

WARNING: On some phones doing this may leave bluetooth sockets dangling if you are using them.

警告:在某些手机上,如果您正在使用它们,这样做可能会使蓝牙插座悬空。