简体   繁体   English

Android:如何以编程方式杀死在后台运行的相机活动?

[英]Android : How to kill programmatically the camera activity running in background?

What happens here is I call the crop image activity and then the camera activity runs in background. 这里发生的是我调用裁剪图像活动,然后相机活动在后台运行。 When I finish this activity, the camera is still alive at the background. 当我完成此活动时,相机仍然在后台活着。 So how can I kill programmatically the camera activity running in background? 那么如何以编程方式杀死在后台运行的相机活动?

Intent newIntent = new Intent(); newIntent.setAction("com.android.camera.action.CROP");
newIntent.setClassName("com.android.gallery", "com.android.camera.CropImage");
newIntent.setData(selectedImage); startActivityForResult(newIntent, IMAGE_CROP

You should let Android handle process lifecycle on its own. 您应该让Android自己处理流程生命周期。 When it'd need more memory, it'll kill unused processes. 当它需要更多内存时,它会杀死未使用的进程。 If you kill it yourself, you also risk killing it while the user is using it leading to a bad user experience. 如果你自己杀了它,你也有可能在用户使用它时将其杀死,从而导致糟糕的用户体验。

For the same reason users should not use a Task Killer to kill their apps every x minutes, you should not try and kill the camera process either. 出于同样的原因,用户不应该每隔x分钟使用一个任务杀手来杀死他们的应用程序,你也不应该尝试杀死摄像头进程。

Why? 为什么? The operating system can handle memory management just fine itself. 操作系统可以很好地处理内存管理。 Let it do its thing, it knows what it is doing. 让它做它的事情,它知道它在做什么。 Also if you killed a service, it is highly likely it'll re-start itself when killed, causing a drain on power resources. 此外,如果你杀了一个服务,很可能它会在被杀时重新启动,导致电源资源耗尽。 You could also potentially kill a process that is writing to disk and cause corruption. 您还可能会杀死写入磁盘并导致损坏的进程。

Also: As CommonsWare pointed out, you're using unreliable private APIs (by hackishly calling the crop intent). 另外:正如CommonsWare所指出的,你正在使用不可靠的私有API(通过骇人地调用作物意图)。

You can get the list of all running processes on the phone via ActivityManager.getRunningAppProcesses and then search for the one you want to kill and then use the method ActivityManager.killBackgroundProcesses to kill the process you want. 您可以通过ActivityManager.getRunningAppProcesses获取手机上所有正在运行的进程的列表,然后搜索要杀死的进程,然后使用方法ActivityManager.killBackgroundProcesses来终止所需的进程。

Note: You need permission KILL_BACKGROUND_PROCESSES 注意:您需要获得KILL_BACKGROUND_PROCESSES的许可

Note: This method is only supported since android 2.2 (froyo) . 注意:此方法仅支持android 2.2(froyo)。 On older versions of android you could get yourself a copy of the android source code and search for the right aidl and after that with the help of some magic you can kill processes as well :) 在旧版本的Android上你可以得到自己的Android源代码的副本,并搜索正确的援助,然后在一些魔法的帮助下你也可以杀死进程:)

Final note: you should not kill processes unless you really have to as it was mentioned already. 最后的注意事项:你不应该杀死进程,除非你真的必须如已经提到的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM