简体   繁体   中英

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. 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. I can see it is still running because through adb , I run the ps command and it says it is.

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!

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() . I also tried returning START_NOT_STICKY and the service is still running after I call startService(intent) .

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

Process.killProcess(Process.myPid());

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.

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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