关闭Android应用程序 [英] Close Android Application

查看:102
本文介绍了关闭Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请建议我怎么可能会关闭我与一行code整个Android应用程序。

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

推荐答案

是 - 为什么,那么如何(排序):

Yes - Why, then how(sort of):

简短的回答:

System.exit(0);

这很好,彻底终止了整个渣机是专门用于运行应用程序。

This nicely and cleanly terminates the whole java machine which is dedicated to running the app.

细节和为什么这是一个很好的问题,程序员可以有一个非常正当的理由来终止他们的应用程序这样的解释:

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.

好是一个善意的提醒到Android上,你不必担心关闭您的应用程序初学者 - 但有些人其实要终止他们的应用程序,即使他们知道,他们没有 - 和如何做到这一点他们的问题是一个有效的答案一个合理的问题。

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.

也许很多人生活在一个理想的世界,并没有意识到,有一个真实的世界里,真实的人正在试图解决实际问题。

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.

事实是,即使是机器人,它仍然是一台电脑,而电脑仍在运行code,而且它是完全可以理解为什么有人会想真正离开他们的应用程序(即所有活动和资源属于他们的应用程序。)

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.)

这是事实,开发商在谷歌设计了一个系统,他们认为没有人会需要退出他们的应用程序。也许99%的他们是正确的时候了!

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!

但令人惊奇的事情有关允许数以百万计的程序员写code的平台之一是,有些人会尝试将平台推到了极限,为了做到令人惊奇的事情! - 包括东西的Andr​​oid开发者甚至从来没有梦想

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!

还有一个需要能够关闭程序,这是为了排除故障。这就是把我带到这个主题:我刚开始学习如何利用音频输入功能,做到实时DSP。

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.

现在不要忘记,我下面说:我清楚地知道,当我把一切都做对了,我就不用再杀死我的应用程序重置音频接口。

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.

所以,我发现的是,我的小音响示波器测试的应用程序伟大的工作,直到我pressed Android的主页按钮。当我再重新启动我的示波器的应用程序,没有任何声音进来了。

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.

起初,我会进 设置 - >应用程序 - >管理应用程序 - > AppName->强制停止

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

(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!)

然后,我可以重新启动我的应用程序,它再次工作。

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:

背景: Android的运行在Linux上。 Linux有过程和进程ID(PID),就像Windows一样,只有更好。 要查看哪些进程正在在Android上运行(与它连接到USB和一切)执行亚行外壳顶部,你会得到所有进程的更新列表中运行Linux下的机器人下。

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 top and you will get an updating list of all the processes running in the linux under the android.

如果您的PC上的Linux,以及,你可以键入

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

ADB顶壳| egrep的-i(用户| PID | MyFirstApp)--line缓冲

要得到公正的结果为您的应用程序名为MyFirstApp。你可以看到很多Linux进程是如何下这个名字运行以及如何将CPU电源,他们消耗的多。

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.

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

(Like the task manager / process list in Windows)

或者,如果你想看到的只是运行的应用程序:

Or if you want to see just the running apps:

ADB顶壳| egrep的-i(用户| PID | APP_)--line缓冲

您还可以通过运行杀死你的设备上的任何应用程序 亚行外壳杀死12345 其中,12345是它的PID号。

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

这是我可以告诉,每一个单线程的应用程序只使用一个单一的Linux进程。

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

所以,我发现了什么是(当然),如果我只是激活的Andr​​oid主页选项,我的应用程序继续运行。 如果我使用 Activity.finish(),它仍然让进程在运行。 除以零断然终止正在运行的Linux进程。 从内部应用程序似乎是最好的,到目前为止,我已经发现,至少在调​​试目的杀死的PID。

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.

我初步解决了我的需要添加一个按钮来杀我的应用程序,将导致被零除,像这样在我的 MainActivity.java

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:

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

然后在该按钮我只是设置了android我的布局XML文件部分:的onClick =退出

Then in my layout XML file section for that button I just set the 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.

然后我尝试:

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

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

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

更新:这是与上述相同的,因为它立即终止Linux进程和应用程序的所有主题:

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

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.

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

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.)

至少对于快速和肮脏的测试,如果你绝对需要知道该线程实际上是完全退出,杀进程确实好听。但是,如果你正在运行多个线程,你可能需要从每个线程中自相残杀的,大概。

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.

编辑:这是一个伟大的链接来阅读的话题: http://developer.android.com/guide/components/fundamentals.html 它解释了每个应用程序在其自己的虚拟机上运行,​​每个虚拟机在自己的用户ID运行。

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

下面是说明如何(除非清单中另有规定)的应用程序及其所有线程的一个Linux进程运行的另一个伟大的链接: http://developer.android.com/guide/components /processes-and-threads.html

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

所以,作为一般规则,一个应用程序真的是在计算机上运行的程序和应用程序真的可以完全杀死,从内存中瞬间消除所有的资源。

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.

(通过即时我的意思是尽快 - 而不是后来每当RAM是必要的)

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

PS:有没有想过为什么你去回答你的Andr​​oid手机或启动您最喜爱的应用程序,它冻结一秒钟?自从重新启动,因为你厌倦了吗?这可能是因为所有的应用程序,你跑了,上周还以为你戒烟,但仍然挂在使用内存。电话杀死他们,当它需要更多的内存,导致你想做的事之前,任何行动的延迟!

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!

更新的Andr​​oid 4.0 /姜饼:同上道理也适用,但即使在应用程序退出或崩溃,其整个java虚拟机的过程中死亡,它仍然显示在应用程序管理器中运行,而你仍然有强制关闭选项或不管它是什么。 4.0必须有它认为正在运行,而不是实际检查,看看是否一个应用程序是真正即使在运行的应用程序的一个独立的列表。

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应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆