如何终止我自己的 Activity - 困难的方法
所以我有了我的 Activity,按下“退出”按钮后,我调用 Activity.finish()。这有效地关闭了我的应用程序。
问题:我的应用程序的 Dalvik 进程仍然作为僵尸在后台徘徊。这似乎是正常的,因为其他应用程序也这样做。甚至 hello-world 示例都留在内存中。
我可以忍受这种情况,但不幸的是,这种行为使我的应用程序的开发变得痛苦。我有一个远程服务连接到我的活动,并且该服务在我的活动卸载之前不会卸载(正如所说的那样,它永远不会卸载)。
一切都以某种方式毫无理由地保持活力。
我怎样才能真正从内存中删除我的 Activity?
我正在寻找类似 Activity.finish_and_kill_my_process_please() 调用或类似的东西。
So I have my Activity, and on pressing a "Quit" button I call Activity.finish(). This effectively closes my application.
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..
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).
Everything is somehow kept alive for no good reason.
How can I really remove my Activity from memory?
I'm looking for something like Activity.finish_and_kill_my_process_please() call or something similar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
android.os.Process.killProcess(android.os.Process.myPid ());
android.os.Process.killProcess(android.os.Process.myPid());
请不要这样做。
该进程保存在缓存中,供 Android 重用。您可以安全地忽略它。
您的应用程序中可能存在错误,例如调用
startService()
并未能调用stopService()
,或者调用bindService()
并且无法调用unbindService()
。Please don't do this.
The process is kept in a cache, for potential reuse by Android. You can safely ignore it.
You probably have a bug in your application, such as calling
startService()
and failing to callstopService()
, or callingbindService()
and failing to callunbindService()
.尝试使用
killBackgroundProcesses
方法:
您需要添加
对您的
权限。AndroidManifest.xml
文件的 KILL_BACKGROUND_PROCESSESTry using the
killBackgroundProcesses
method:You will need to add the
KILL_BACKGROUND_PROCESSES
permission to yourAndroidManifest.xml
file.通过终止进程,您可以完全停止应用程序。从按后退按钮的最后一个屏幕中,您可以通过以下方式终止进程:
By killing the process you can stop the application completely.from the last screen on back button press you can kill the process by :
好吧,如果不仅是 Activity,还想终止应用程序,则可以使用
System.exit(0)
人们说它已被弃用或不好的示例,但是,它是纯 Java 并且确实有效...
Well, if not only activity, but also the application you want to terminate, you can use
System.exit(0)
People say that it's deprecated or bad example, but, well it's pure Java and does work...
通过使用 ActivityManager,我们还可以关闭其他应用程序。
要使用上述代码,应在清单文件中拥有
android.permission.RESTART_PACKAGE
权限。By using the ActivityManager we can close the another applications also.
to use the above code should have the
android.permission.RESTART_PACKAGE
permissions in manifest file.与 Java 端的 System.exit(0) 类似,您也可以在 onDestroy 期间调用本机方法,该方法在 C/C++ 端调用 exit(0)。这似乎迫使库完全退出。它对我有用!
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!
System.exit() 立即卸载整个虚拟机以及所有本机库、服务等。
System.exit() instantly unloads whole virtual machine with all native libraries, services and etc.