JobIntentService not remove thread after finish like IntentService

310 Views Asked by At

I have JobIntentService from official doc:

public class SimpleJobIntentService extends JobIntentService {

    static final int JOB_ID = 1000;

    static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, SimpleJobIntentService.class, JOB_ID, work);
    }

    @Override
    protected void onHandleWork(Intent intent) {
        // We have received work to do.  The system or framework is already
        // holding a wake lock for us at this point, so we can just go.
        Log.i("SimpleJobIntentService", "Executing work: " + intent);
        String label = intent.getStringExtra("label");
        if (label == null) {
            label = intent.toString();
        }
        toast("Executing: " + label);
        for (int i = 0; i < 5; i++) {
            Log.i("SimpleJobIntentService", "Running service " + (i + 1)
                    + "/5 @ " + SystemClock.elapsedRealtime());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        Log.i("SimpleJobIntentService", "Completed service @ " + SystemClock.elapsedRealtime());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        toast("All work complete");
    }

    final Handler mHandler = new Handler();

    // Helper for showing tests
    void toast(final CharSequence text) {
        mHandler.post(new Runnable() {
            @Override public void run() {
                Toast.makeText(SimpleJobIntentService.this, text, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Before run this service, after call Thread.activeCount() I got number 33. After ending first running service, I got 34, next 35 and so on.
Why ? Someone could explain me ? How to kill this thread after ending ? It is normal behavior of JobIntentService ? IntentService works perfectlym always kill a thread after ending.

I start this service from fragment using:

Intent intent = new Intent(getActivity(), SimpleJobIntentService.class);
SimpleJobIntentService.enqueueWork(getActivity(), intent);

anyway, if I startService this way:

getActivity().startService(new Intent(getActivity(), SimpleJobIntentService.class));

JobIntentService also not remove thread

0

There are 0 best solutions below