简体   繁体   English

Android:如何完全杀死作为后台进程单独运行的服务?

[英]Android: How to completely kill a Service running separately as a background process?

I have an app that has an Activity, which is used as the regular GUI, and a Service.我有一个应用程序,它有一个 Activity(用作常规 GUI)和一个服务。 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:我分别使用这些方法来启动和停止我的进程:

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.意思是,当我启动服务和onDestroy()应用程序时,该服务仍在运行。 I can see it is still running because through adb , I run the ps command and it says it is.我可以看到它仍在运行,因为通过adb ,我运行了ps命令,它说它是。

The problem is when I call stopService(intent) .问题是当我调用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) ,它将运行服务的onDestroy()中设置的代码行,但是当我通过adb运行ps时,它说它仍在运行。

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.编辑:当我启动服务时,我使用onStartCommand()并在那里运行一些代码。 I also return START_STICKY from onStartCommand() .我也从onStartCommand()返回START_STICKY I also tried returning START_NOT_STICKY and the service is still running after I call startService(intent) .我还尝试返回START_NOT_STICKY并且在我调用startService(intent)后该服务仍在运行。

如果你想强行杀死服务进程,你可以使用以下代码:

Process.killProcess(Process.myPid());

Services are stopped after stopService is called.调用 stopService 后停止服务。 After stop, they are inactive, but cached.停止后,它们处于非活动状态,但被缓存。

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. RS 命令不知道 active 和 cache dprocess 之间的区别,并且始终显示所有可用的进程。 This behaviour will maintain if you remove :remote (note that separate process will not be created)如果您删除:remote此行为将保持不变(请注意,不会创建单独的进程)

I think you could set android:exported="false" in your manifest so that other apps cannot access the service thereby starting the service.我认为您可以在清单中设置android:exported="false"以便其他应用程序无法访问该服务从而启动该服务。 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.我做了同样的事情,它运行得很好。

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

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