Android 关闭安卓应用

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

Close Android Application

android

提问by Amit

Please suggest how I may close my whole Android Application with one line code.

请建议我如何用一行代码关闭我的整个 Android 应用程序。

回答by Jesse Gordon

Yes - Why, then how(sort of):

是的 - 为什么,然后如何(有点):

Short answer:

简短的回答:

System.exit(0);

This nicely and cleanly terminates the whole java machine which is dedicated to running the app. However, you should do it from the main activity, otherwise android may restart your app automatically. (Tested this on Android 7.0)

这很好且干净地终止了专用于运行应用程序的整个 Java 机器。但是,您应该从主活动中执行此操作,否则 android 可能会自动重新启动您的应用程序。(在 Android 7.0 上测试过)

Details and explanation of why this is a good question and a programmer may have a very legitimate reason to terminate their app this way:

为什么这是一个好问题的详细信息和解释,程序员可能有非常正当的理由以这种方式终止他们的应用程序:

I really don't see the gain in speaking harshly to someone who's looking for a way to terminate their app.

我真的没有看到对正在寻找终止其应用程序的方法的人严厉说话的好处。

Good is a friendly reminder to beginners that on Android you don't have to worry about closing your app -- but some people actually do want to terminate their app even though they know that they don't have to -- and their question of how to do so is a legitimate question with a valid answer.

Good 是对初学者的友好提醒,在 Android 上您不必担心关闭您的应用程序——但有些人实际上确实想终止他们的应用程序,即使他们知道他们不必这样做——以及他们的问题如何做到这一点是一个具有有效答案的合理问题。

Perhaps many folks live in an ideal world and don't realize that there's a real world where real people are trying to solve real problems.

也许很多人生活在一个理想的世界中,却没有意识到存在一个真实的世界,真实的人正在努力解决真实的问题。

The fact is that even with android, it is still a computer and that computer is still running code, and that it is perfectly understandable why someone may wish to truly exit their "app" (i.e. all of the activities and resources belonging to their app.)

事实是,即使使用 android,它仍然是一台计算机并且该计算机仍在运行代码,并且完全可以理解为什么有人可能希望真正退出他们的“应用程序”(即属于他们应用程序的所有活动和资源) .)

It is true that the developers at Google designed a system where they believed nobody would ever need to exit their app. And maybe 99% of the time they are right!

确实,谷歌的开发人员设计了一个系统,他们相信没有人需要退出他们的应用程序。也许 99% 的时间他们是对的!

But one of the amazing things about allowing millions of programmers to write code for a platform is that some of them will try to push the platform to its limits in order to do amazing things! -- Including things that the Android Developers never even dreamed of!

但是,让数百万程序员为平台编写代码的令人惊奇的事情之一是,他们中的一些人会尝试将平台推向极限,以完成令人惊奇的事情!-- 包括 Android 开发者做梦都想不到的事情!

There is another need for being able to close a program, and that is for troubleshooting purposes. That is what brought me to this thread: I'm just learning how to utilize the audio input feature to do realtime DSP.

还有一个需要能够关闭程序,这是为了故障排除目的。这就是让我进入这个线程的原因:我只是在学习如何利用音频输入功能来进行实时 DSP。

Now don't forget that I said the following: I well know that when I have everything done right, I won't need to kill my app to reset the audio interface.

现在不要忘记我说过以下的话:我很清楚,当我把所有事情都做对了,我不需要终止我的应用程序来重置音频界面。

BUT: Remember, perfect apps don't start out as perfect apps! They start out as just barely working apps and grow to become proper ideal apps.

但是:请记住,完美的应用程序并不是从完美的应用程序开始的!它们刚开始只是勉强工作的应用程序,然后成长为合适的理想应用程序。

So what I found was that my little audio oscilloscope test app worked great until I pressed the android Home button. When I then re-launched my oscilloscope app, there was no audio coming in anymore.

所以我发现我的小型音频示波器测试应用程序运行良好,直到我按下了 android Home 按钮。当我重新启动我的示波器应用程序时,不再有音频输入。

At first I would go into Settings->Applications->Manage Applications->AppName->Force Stop.

起初我会进入 Settings->Applications->Manage Applications->AppName->Force Stop.

(Note that if the actual linux process is not running, the Force Stop button will be disabled. If the button is enabled, then the Linux process is still running!)

(注意,如果实际的 linux 进程没有运行,强制停止按钮将被禁用。如果该按钮被启用,则 Linux 进程仍在运行!)

Then I could re-launch my app and it worked again.

然后我可以重新启动我的应用程序并再次运行。

At first, I was just using divide by zero to crash it - and that worked great. But I decided to look for a better way - which landed me here!

起初,我只是使用除以零来使其崩溃 - 效果很好。但我决定寻找更好的方法——这让我来到了这里!

So here's the ways I tried and what I found out:

所以这是我尝试过的方法和我发现的:

Background: Android runs on Linux. Linux has processes and process IDs (PIDs) just like Windows does, only better. To see what processes are running on your android (with it connected into the USB and everything) run adb shell topand you will get an updating list of all the processes running in the linux under the android.

背景:Android 在 Linux 上运行。Linux 有进程和进程 ID (PID),就像 Windows 一样,只是更好。要查看您的 android 上正在运行哪些进程(将它连接到 USB 和所有东西)运行adb shell top,您将获得在 android 下运行在 linux 中的所有进程的更新列表。

If you have linux on your PC as well, you can type

如果您的 PC 上也有 linux,则可以键入

adb shell top | egrep -i '(User|PID|MyFirstApp)' --line-buffered

adb shell top | egrep -i '(User|PID|MyFirstApp)' --line-buffered

to get just the results for your app named MyFirstApp. You can see how many Linux Processes are running under that name and how much of the cpu power they are consuming.

只获取名为 MyFirstApp 的应用程序的结果。您可以看到有多少 Linux 进程在该名称下运行,以及它们消耗了多少 CPU 功率。

(Like the task manager / process list in Windows)

(就像 Windows 中的任务管理器/进程列表)

Or if you want to see just the running apps:

或者,如果您只想查看正在运行的应用程序:

adb shell top | egrep -i '(User|PID|app_)' --line-buffered

adb shell top | egrep -i '(User|PID|app_)' --line-buffered

You can also kill any app on your device by running adb shell kill 12345where 12345 is it's PID number.

您还可以通过adb shell kill 12345在 12345 是其 PID 编号的位置运行来终止设备上的任何应用程序 。

From what I can tell, each single-threaded app just uses a single Linux process.

据我所知,每个单线程应用程序只使用一个 Linux 进程。

So what I found out was that (of course) if I just activate the android Home option, my app continues to run. And if I use the Activity.finish(), it still leaves the process running. Divide by zero definitely terminates the linux process that is running. Killing the PID from within the app seems the nicest so far that I've found, at least for debugging purposes.

所以我发现(当然)如果我只是激活 android Home 选项,我的应用程序会继续运行。如果我使用Activity.finish(),它仍然会保持进程运行。除以零肯定会终止正在运行的 linux 进程。到目前为止,我发现从应用程序中删除 PID 似乎是最好的,至少出于调试目的。

I initially solved my need to kill my app by adding a button that would cause a divide by zero, like this in my MainActivity.java:

我最初通过添加一个按钮来解决我杀死我的应用程序的需要,该按钮会导致除以零,就像我的MainActivity.java

public void exit(View view)
{
    int x;
    x=1/0;
}

Then in my layout XML file section for that button I just set the android:onClick="exit".

然后在该按钮的布局 XML 文件部分中,我只设置了 android:onClick="exit"。

Of course divide by zero is messy because it always displays the "This application stopped..." or whatever.

当然,除以零很麻烦,因为它总是显示“此应用程序已停止...”或其他任何内容。

So then I tried the finish, like this:

然后我尝试了完成,如下所示:

public void exit(View view)
{
    finish();
}

And that made the app disappear from the screen but it was still running in the background.

这使得应用程序从屏幕上消失,但它仍在后台运行。

Then I tried:

然后我尝试:

public void exit(View view)
{
      android.os.Process.killProcess(android.os.Process.myPid());
}

So far, this is the best solution I've tried.

到目前为止,这是我尝试过的最佳解决方案。

UPDATE: This is the same as above in that it instantly terminates the Linux process and all threads for the app:

更新:这与上面的相同,因为它会立即终止应用程序的 Linux 进程和所有线程:

public void exit(View view)
{
      System.exit(0);
}

It instantly does a nice full exit of the thread in question without telling the user that the app crashed.

它立即完全退出相关线程,而不会告诉用户应用程序崩溃了。

All memory used by the app will be freed. (Note: Actually, you can set parameters in your manifest file to cause different threads to run in different Linux processes, so it gets more complicated then.)

应用程序使用的所有内存都将被释放。(注意:实际上,您可以在清单文件中设置参数以导致不同的线程在不同的 Linux 进程中运行,因此它变得更加复杂。)

At least for quick and dirty testing, if you absolutely need to know that the thread is actually fully exited, the kill process does it nicely. However, if you are running multiple threads you may have to kill each of those, probably from within each thread.

至少对于快速和肮脏的测试,如果您绝对需要知道线程实际上已完全退出,kill 进程可以很好地完成它。但是,如果您正在运行多个线程,则可能必须从每个线程中杀死每个线程。

EDIT: Here is a great link to read on the topic: http://developer.android.com/guide/components/fundamentals.htmlIt explains how each app runs in its own virtual machine, and each virtual machine runs under its own user ID.

编辑:这是一个很好的阅读主题的链接: http://developer.android.com/guide/components/fundamentals.html它解释了每个应用程序如何在自己的虚拟机中运行,每个虚拟机在自己的用户 ID 下运行。

Here's another great link that explains how (unless specified otherwise in manifest) an app and all of its threads runs in a single Linux process: http://developer.android.com/guide/components/processes-and-threads.html

这是另一个很好的链接,它解释了(除非清单中另有说明)应用程序及其所有线程如何在单个 Linux 进程中运行: http://developer.android.com/guide/components/processes-and-threads.html

So as a general rule, an app really is a program running on the computer and the app really can be fully killed, removing all resources from memory instantly.

因此,作为一般规则,应用程序实际上是在计算机上运行的程序,并且该应用程序确实可以被完全杀死,立即从内存中删除所有资源。

(By instantly I mean ASAP -- not later whenever the ram is needed.)

(即时我的意思是尽快——而不是在需要 ram 时。)

PS: Ever wonder why you go to answer your android phone or launch your favorite app and it freezes for a second? Ever reboot because you get tired of it? That's probably because of all the apps you ran in the last week and thought you quit but are still hanging around using memory. Phone kills them when it needs more memory, causing a delay before whatever action you wanted to do!

PS:有没有想过为什么你去接听你的安卓手机或启动你最喜欢的应用程序,它会死机一秒钟?是否曾经因为厌倦而重新启动?这可能是因为您在上周运行的所有应用程序都认为您已退出,但仍在使用内存。手机在需要更多内存时会杀死它们,从而在您想要执行的任何操作之前造成延迟!

Update for Android 4/Gingerbread: Same thing as above applies, except even when an app exits or crashes and its whole java virtual machine process dies, it still shows up as running in the app manager, and you still have the "force close" option or whatever it is. 4.0 must have an independent list of apps it thinks is running rather than actually checking to see if an app is really even running.

Android 4/Gingerbread 更新:与上述相同,除了即使应用程序退出或崩溃并且其整个 Java 虚拟机进程终止,它仍然显示为在应用程序管理器中运行,并且您仍然可以“强制关闭”选项或任何它是。4.0 必须有一个它认为正在运行的应用程序的独立列表,而不是实际检查应用程序是否真的在运行。

回答by RoflcoptrException

You could finish your Activity by calling Activity.finish(). However take care of the Android Activity life-cycle.

您可以通过调用Activity.finish()来完成您的活动。但是请注意Android Activity 生命周期

回答by Ganesh karthik

You could close your Android Application by calling System.exit(0).

您可以通过调用 System.exit(0) 来关闭您的 Android 应用程序。

回答by S.D.

The answer depends on what you mean by "close app". In android's terms its either about an "activity", or a group of activities in a temporal/ancestral order called "task".

答案取决于您所说的“关闭应用程序”是什么意思。在 android 的术语中,它要么是关于一个“活动”,要么是一组按时间/祖先顺序排列的活动,称为“任务”。

End activity: just call finish()

结束活动:只需调用 finish()

End task:

结束任务:

  1. clear activity task-stack
  2. navigate to root activity
  3. then call finish on it.
  1. 清除活动任务堆栈
  2. 导航到根活动
  3. 然后调用完成它。

This will end the entire "task"

这将结束整个“任务”

回答by Shouvik

just call the finish() in the method you would like to end the activity in, for example when you use the onCreate() method, in the end of the method, just add finish() and you will see the activity ends as soon as it is created!

只需在您想要结束活动的方法中调用finish(),例如当您使用onCreate() 方法时,在方法的末尾,只需添加finish(),您就会看到活动尽快结束因为它是创建的!

回答by Paresh Mayani

As per my knowledge, finish function close the current displayed screen only.

据我所知,完成功能仅关闭当前显示的屏幕。

Refer this example (where see the answer given by 'plusminus'), it will sure help you to close your application.

参考这个例子(在那里看到'plusminus'给出的答案),它肯定会帮助你关闭你的应用程序。

回答by Pentium10

Before doing so please read this other questiontoo:

在这样做之前,请阅读另一个问题

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

回答by Sujay

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

This code kill the process from OS Using this code disturb the OS. So I would recommend you to use the below code

此代码从操作系统中杀死进程 使用此代码会干扰操作系统。所以我建议你使用下面的代码

this.finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

回答by Craig Zheng

not a one-line-code solution, but pretty easy:

不是单行代码解决方案,但非常简单:

sub-class Activity, then override and add finish() to onPause() method

子类 Activity,然后覆盖并添加 finish() 到 onPause() 方法

this way, activity will be gone once it enters background, therefore the app won't keep a stack of activities, you can finish() the current activity and the app is gone!

这样,一旦进入后台,活动就会消失,因此应用程序不会保留一堆活动,您可以完成()当前活动,应用程序就消失了!

回答by 3rgo

If you close the main Activity using Activity.finish(), I think it will close all the activities. MAybe you can override the default function, and implement it in a static way, I'm not sure

如果您使用 关闭主活动Activity.finish(),我认为它将关闭所有活动。也许您可以覆盖默认功能,并以静态方式实现它,我不确定