Ankur
Ankur

Reputation: 759

Same Intent Service running multiple background tasks parallely (ISSUE)

public class DataManager extends IntentService {
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public DataManager() {
        super("DataManager");
        setIntentRedelivery(true);
    }

    @Override
    protected void onHandleIntent(final Intent intent) {
        // download and parsing task done here
    }
}

This is my intent service which i am using to download file and parse it. Now if i get a new request for a file download, i have to clear the ongoing task and start the download for new request cancelling the older one. so i use the below code for doing it :.

private void refreshSync() {
    context.stopService(new Intent(context, DataManager.class));
    final Intent mServiceIntent = new Intent(context, DataManager.class);
    mServiceIntent.putExtras(bundle);
    context.startService(mServiceIntent);
}

So the service gets killed and the next request to start service is intented. But the previous tasks starts again running two parallel tasks performing download. Basically the previous task doesnt get killed which i intended to.

Is there any work around to kill the ongoing task of the service and start another fresh task ?

Upvotes: 1

Views: 1005

Answers (2)

David Wasser
David Wasser

Reputation: 95578

Don't use IntentService. This doesn't match your requirements. IntentService is a simple Service that accepts a queue of work and processes the queue and then shuts itself down when the queue is empty.

You need more intelligence, and you are better off implementing that yourself. Just extend Service instead of IntentService. In onStartCommand() start a background Thread that downloads the data. Keep track of that background Thread in a member variable in the Service. If startService() gets called again, check if you already have a download in progress. If so, stop it and start a new background Thread to download the new file. To stop a background thread, you should provide a boolean variable in the Thread that gets examined every now and then inside the download loop. If that variable's state changes, it means the Thread should stop. This is a standard mechanism for stopping background threads in Java.

Upvotes: 2

Aleksandr Medvedev
Aleksandr Medvedev

Reputation: 8978

You are setting setIntentRedelivery(true);, that force the intents to survive calls of the service if they are not handled completely (if onHandleIntent doesn't manage to return). Taking into account the fact that IntentService has only one working thread (can execute only one task at a time) the behavior of the service completely depends on the onHandleIntent implementation. So you need either analyze implementation and change it according to you goals, or set setIntentRedelivery(false);

Upvotes: 1

Related Questions