Android 如何杀死我自己的活动 - 艰难的方式

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

How to kill my own Activity - the hard way

androidprocessandroid-activitykill

提问by Nils Pipenbrinck

So I have my Activity, and on pressing a "Quit" button I call Activity.finish(). This effectively closes my application.

所以我有我的活动,并在按下“退出”按钮时调用 Activity.finish()。这有效地关闭了我的应用程序。

The problem: The Dalvik-process of my application is still hanging around as a zombie in background. It seems like this is normal as other applications do the same. Even The hello-world example hangs around in memory..

问题:我的应用程序的 Dalvik 进程仍然像僵尸一样在后台徘徊。这似乎是正常的,因为其他应用程序也是如此。甚至 hello-world 示例都在内存中。

I could live with this, but unfortunatley this behaviour makes the development of my application a pain. I have a remote service connected to my Activity, and this service won't unload until my Activity unloads (which as said it never does).

我可以忍受这一点,但不幸的是,这种行为使我的应用程序的开发变得痛苦。我有一个连接到我的 Activity 的远程服务,并且在我的 Activity 卸载之前,该服务不会卸载(正如所说的那样,它永远不会卸载)。

Everything is somehow kept alive for no good reason.

一切都以某种方式无缘无故地保持活力。

How can I reallyremove my Activity from memory?

我怎样才能真正从内存中删除我的活动?

I'm looking for something like Activity.finish_and_kill_my_process_please() call or something similar.

我正在寻找类似 Activity.finish_and_kill_my_process_please() 调用或类似的东西。

采纳答案by CommonsWare

So I have my Activity, and on pressing a "Quit" button I call Activity.finish(). This effectively closes my application.

所以我有我的活动,并在按下“退出”按钮时调用 Activity.finish()。这有效地关闭了我的应用程序。

Please don't do this.

请不要这样做

The Dalvik-process of my application is still hanging around as a zombie in background.

我的应用程序的 Dalvik 进程仍然像僵尸一样在后台徘徊。

The process is kept in a cache, for potential reuse by Android. You can safely ignore it.

该进程保存在缓存中,以供 Android 潜在重用。您可以放心地忽略它。

I have a remote service connected to my Activity, and this service won't unload until my Activity unloads (which as said it never does).

我有一个连接到我的 Activity 的远程服务,并且在我的 Activity 卸载之前,该服务不会卸载(正如所说的那样,它永远不会卸载)。

You probably have a bug in your application, such as calling startService()and failing to call stopService(), or calling bindService()and failing to call unbindService().

您的应用程序中可能存在错误,例如调用startService()并且无法调用stopService(),或者调用bindService()并且无法调用unbindService()

回答by bdhac

  1. Call finish();on button click
  2. Add this line to onDestroy method:
  1. 调用完成(); 点击按钮
  2. 将此行添加到 onDestroy 方法:

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

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

    public void onDestroy() {
        super.onDestroy();
        android.os.Process.killProcess(android.os.Process.myPid());
    }       

回答by Cristian

Try using the killBackgroundProcessesmethod:

尝试使用以下killBackgroundProcesses方法:

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("name.of.your.package");

You will need to add the KILL_BACKGROUND_PROCESSESpermission to your AndroidManifest.xmlfile.

您需要将KILL_BACKGROUND_PROCESSES权限添加到您的AndroidManifest.xml文件中。

回答by Midhun VP

By killing the process you can stop the application completely.from the last screen on back button press you can kill the process by :

通过终止进程,您可以完全停止应用程序。从按下后退按钮的最后一个屏幕,您可以通过以下方式终止进程:

public void onBackPressed() {   
        super.onBackPressed();   
        int pid = android.os.Process.myPid();
        android.os.Process.killProcess(pid);    }

回答by Ahmet Ipkin

Well, if not only activity, but also the application you want to terminate, you can use

好吧,如果不仅是活动,还有你想终止的应用程序,你可以使用

System.exit(0)

System.exit(0)

People say that it's deprecated or bad example, but, well it's pure Java and does work...

人们说它已被弃用或不好的例子,但是,它是纯 Java 并且确实有效......

回答by PolyMesh

Similar to the System.exit(0) on the Java side, you can also call a native method during onDestroy which calls exit(0) on the C/C++ side. This seems to force the lib to exit completely. It's working for me!

与 Java 端的 System.exit(0) 类似,您也可以在 onDestroy 期间调用本地方法,该方法在 C/C++ 端调用 exit(0)。这似乎迫使 lib 完全退出。它对我有用!

回答by Krishna Prasad

By using the ActivityManager we can close the another applications also.

通过使用 ActivityManager,我们也可以关闭其他应用程序。

private ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
am.restartPackage("com.jimmy.appToBeClosed");

to use the above code should have the android.permission.RESTART_PACKAGEpermissions in manifest file.

使用上面的代码应该android.permission.RESTART_PACKAGE在清单文件中具有权限。

回答by Sergey Galin

System.exit() instantly unloads whole virtual machine with all native libraries, services and etc.

System.exit() 立即卸载带有所有本地库、服务等的整个虚拟机。