Android:如何完全杀死作为后台进程单独运行的服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20613008/
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
Android: How to completely kill a Service running separately as a background process?
提问by Rob Avery IV
I have an app that has an Activity, which is used as the regular GUI, and a Service. My activity has two buttons. One button to stop the process and one to kill the process. I use these to methods, respectively, to start and stop my process:
我有一个应用程序,它有一个 Activity(用作常规 GUI)和一个服务。我的活动有两个按钮。一键停止进程,一键终止进程。我分别使用这些方法来启动和停止我的进程:
Intent i = null;
Button start;
Button stop;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
i = new Intent(this, Service.class);
start = (Button) findViewbyId(R.id.start_button);
stop = (Button) findViewById(R.id.stop_button);
start.setOnClickListener(new OnClickListener(){
public void onClick(){
startService(i);
}
}
stop.setOnClickListener(new OnClickListener(){
public void onClick(){
stopService(i);
}
}
}
This Service is not bound to the actvivty or app. I have the service configured in the Manifest as so:
本服务不受活动或应用程序的约束。我在清单中配置了服务,如下所示:
<service
android:name="com.example.mypackage.Service"
android:process=":remote">
<intent-filter>
<action
android:name="com.example.mypackage.Service" />
</intent-filter>
</service>
When I start the Service it runs on it's own independent on anything else. Meaning, when I start the service and onDestroy()
the app, the service is still running. I can see it is still running because through adb
, I run the ps
command and it says it is.
当我启动服务时,它独立于其他任何东西运行。意思是,当我启动服务和onDestroy()
应用程序时,服务仍在运行。我可以看到它仍在运行,因为通过adb
,我运行了ps
命令,它说它是。
The problem is when I call stopService(intent)
. When I call stopService(intent)
from the activity side, it will run the lines of code set in the onDestroy()
of the service, but when I run ps
through adb
, it says that it's still running.
问题是当我调用stopService(intent)
. 当我stopService(intent)
从活动端调用时,它将运行onDestroy()
服务中设置的代码行,但是当我运行ps
时adb
,它说它仍在运行。
I want to be able to COMPLETELY destroy the service. I'm not sure how services work, so please, don't hesitate to talk remedial. I'm not sure if it has to do with what I'm doing within the service or not. Thanks!
我希望能够完全销毁服务。我不确定服务是如何工作的,所以请不要犹豫,谈谈补救措施。我不确定这是否与我在服务中所做的事情有关。谢谢!
EDIT:When I start the service, I use the onStartCommand()
and run some code there which it does. I also return START_STICKY
from onStartCommand()
. I also tried returning START_NOT_STICKY
and the service is still running after I call startService(intent)
.
编辑:当我启动服务时,我使用onStartCommand()
并在那里运行一些代码。我也START_STICKY
从onStartCommand()
. 我也尝试返回,START_NOT_STICKY
并且在我调用后该服务仍在运行startService(intent)
。
回答by Chari D
If you want kill service process forcefully, you can use following code:
如果你想强行杀死服务进程,你可以使用以下代码:
Process.killProcess(Process.myPid());
回答by Yarh
Services are stopped after stopService is called. After stop, they are inactive, but cached.
调用 stopService 后停止服务。停止后,它们处于非活动状态,但被缓存。
By android documentation they become an empty process:
根据 android 文档,它们变成了一个空进程:
A process that doesn't hold any active application components. The only reason to keep this kind of process alive is for caching purposes, to improve startup time the next time a component needs to run in it. The system often kills these processes in order to balance overall system resources between process caches and the underlying kernel caches.
不包含任何活动应用程序组件的进程。保持这种进程活着的唯一原因是为了缓存目的,以便在下次需要在其中运行组件时缩短启动时间。系统通常会杀死这些进程,以平衡进程缓存和底层内核缓存之间的整体系统资源。
Service will remain in cache until it will be called by you app again or sytem cache will not have room for it to keep. RS command does not know difference between active and cache dprocess and allways shows all awailible processes. This behaviour will maintain if you remove :remote
(note that separate process will not be created)
服务将保留在缓存中,直到您的应用程序再次调用它,否则系统缓存将没有空间保留它。RS 命令不知道 active 和 cache dprocess 之间的区别,并且始终显示所有可用的进程。如果您删除此行为将保持:remote
(请注意,不会创建单独的进程)
回答by Chigozie Kingsley
I think you could set android:exported="false"
in your manifest so that other apps cannot access the service thereby starting the service. I may need to see the full implementation of your code to fully determine the cause of it. I did the same thing and it is working perfectly well.
我认为您可以android:exported="false"
在清单中进行设置,以便其他应用程序无法访问该服务从而启动该服务。我可能需要查看您代码的完整实现才能完全确定其原因。我做了同样的事情,它运行得很好。