Android的:如何彻底杀灭作为后台进程运行的独立服务? [英] Android: How to completely kill a Service running separately as a background process?

查看:723
本文介绍了Android的:如何彻底杀灭作为后台进程运行的独立服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有一个活动,这被用作常规GUI的应用程序,和一个服务。我的活动有两个按钮。一键停止的过程,一个杀死进程。我分别使用这些方法,启动和停止我的过程:

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:

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);
        }
    }
}

此服务没有绑定到actvivty或应用。我在清单配置为这样的服务:

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>

当我启动它自己独立的运行在别的服务。意思是,当我启动服务和的onDestroy()应用程序,服务仍在运行。我可以看到它仍通过 ADB因为运行,我跑了 PS 命令,并说,这是。

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.

问题是,当我称之为 stopService(意向)。当我打电话 stopService(意向)从活动中的一面,它将运行在的onDestroy()服务的,但是当我运行 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.

我希望能够彻底摧毁该服务。我不知道如何服务的工作,所以,请不要犹豫,说话补救。我不知道是否有什么我的服务或没有在做的事情。谢谢!

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!

编辑:
当我启动服务,我用的是 onStartCommand()和运行一些code有哪些它。我也是从 onStartCommand返回 START_STICKY ()。我也尝试返回 START_NOT_STICKY 和服务仍在运行我打电话后, startService(意向)

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).

推荐答案

服务停止stopService被调用后。停止后,他们是无效的,但缓存。通过Android文档他们成为空进程结果
不持有任何活动应用组件的过程。保持这种进程活着的唯一原因是为了缓存目的,下一次组件需要在运行它以提高启动时间。该系统往往为了处理高速缓存和基础内核缓存之间平衡整个系统的资源,杀死这些进程。

Services are stopped after stopService is called. After stop, they are inactive, but cached. By android documentation they become an empty process
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.

服务将保留在缓存中,直至将你的应用程序再次调用或系统正缓存将没有足够的空间为它保留。 RS命令不知道主动和缓存dprocess区别和传真显示所有awailible进程。如果删除此行为将保持:远程(注意,单独的进程不会创建)

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)

这篇关于Android的:如何彻底杀灭作为后台进程运行的独立服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆