stopService() not for service

Asked

Viewed 81 times

0

I’m studying Services on Android, I can perform the same in a simple test just for understanding, but I can’t stop it in main Activity at all. I believe I’ve tried everything. I tried to put stopSelf() in onDestroy, I changed the call of stopService() in the main acitivity anyway, but it doesn’t work, the service keeps running. It shows in onDestroy() that it was destroyed, but it is still working. I would like to better understand this situation.

Another question would be on the basis of... Will I be able to stop the service, but as it is linked to a thread to the main part, it will only actually stop when what is inside the thread ends?

Setting the field.

private Intent serviceIntent;

Performing startService() and stopService() at the click of their respective buttons.

serviceIntent = new Intent(this, MyIntentService.class);
startService(serviceIntent);
ou
stopService(serviceIntent);

Intentservice Extended Class. Only thing it does is a counter from 0 to 20 in the Log.

@Override
protected void onHandleIntent(@Nullable Intent intent) {

    // Recebendo o valor passado pela intent.
    int numero = intent.getIntExtra("numero", 50);

    for(int i = 1; i < numero; i++) {
        Log.d("onHandleIntent", ""+i);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
  • That seems strange to me. I made a small example that works well for me, you can make some modifications and see what happens: https://github.com/calheiros/my-app.git

  • The worst that’s pretty much the same thing that you did, the difference is that I put a down count until a certain amount.

1 answer

3


Will I be able to stop the service, but as it is linked to a thread to the main part, it will only actually stop when what is inside the thread ends?

Yes, that’s exactly what happens.
It is one thing for the service to be available/running another is to be running a task (the onStartCommand()).
That is, one thing is the code that keeps the service active another thing is the task it performs.

The method startService() has two functions:

  • create the service
  • call the method onStartCommand().

On the first call the service is created, its methods being onCreate() and onStartCommand() called.
From that moment the service is available and in future calls to startService() is just called the method onStartCommand().

Perhaps it is this dual function that creates confusion.
Perhaps it would be better to have two methods, one for creating the service: startService() and another to execute the command: startCommand().

A Service is an application component (such as an Activity), its creation involves resource allocation.
The purpose of methods stopService() and stopSelf() is to indicate, when the service is no longer needed, that those resources may be released.
If the task is still running it will continue until finished, either in the method onStartCommand() or in any other thread created by the service.

An Intentservice is not designed to be stopped externally. It is not prepared to handle a stopService().
Your intention is to perform a task and, when finished, auto-stop yourself by using stopSelf().

The task, code in the method onHandleIntent(), runs into another Thread that, not being stopped by the implementation of Intentservice in response to a stopService(), It will only end after the execution of the entire code. This can be verified in implementation of the Intentservice where stopSelf() is called after onHandleIntent().

You will need to use a mechanism to stop thread when the method onDestroy() is called.
A possibility, between others, is "setar" a flag, test it within the method onHandleIntent() and make him return immediately.

Note that who decides when the method onDestroy() is called is Android, which may not be immediately in relation to the call stopService().

  • So just to confirm my understanding of what you said. In the case of a service, regardless of whether it is a super-class Service or a sub-class Intentservice, it can only be stopped upon completion of the service, is that right? Service needs to set stopSelf() at the end of the service and Intentservice does this automatic processing, correct? But in relation to stopService() that can be called where the service instance was made, what is the actual functionality of it? Since from what I understood it for the service but the activity generated in the thread continues, what I did not understand.

  • I rephrased the answer. I hope it’s clear.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.