Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an IntentService run indefinitely?

Based on my understanding, an IntentService will get stopped when its current request is done.

Consider the below scenario, i will be triggering a request to the IntentSerivce for every 100ms and the processing time will be 90 ms for that request.

So for every my request - startSerivce call, service will get invoked and after 90ms (once the processing is done), onDestroy of the IntentServicee will get invoked.

I would like to have this IntentServicee running till i say stop. Is that possible?

assume that i am going to do the below in my service

  1. Do configure it
  2. Send some request
  3. wait for response
  4. read the response
  5. Broadcast to activity that invoked

Steps 1 is common for all my requests, so i thought i can do them when service is started initially once and then do 3-6 in HandleIntent based on the requests.

like image 503
Pathy Avatar asked Jul 27 '11 08:07

Pathy


People also ask

Does IntentService run on background thread?

The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface's responsiveness.

Is IntentService deprecated?

This class was deprecated in API level 30. IntentService is subject to all the background execution limits imposed with Android 8.0 (API level 26). Consider using WorkManager or JobIntentService , which uses jobs instead of services when running on Android 8.0 or higher.

Can an IntentService execute multiple tasks sequentially?

1) Is it possible to have multiple intentService threads at the same time or not? No, each IntentService only has one HandlerThread that it uses to execute requests in the order that "startService" is called.


1 Answers

The IntentService is actually a pretty small class which wraps a Handler, the problem being that after an Intent has been handled it calls stopSelf().

Removing that single line gives you an IntentService which needs to be explicitly stopped:

public abstract class NonStopIntentService extends Service {

    private String mName;
    private volatile Looper mServiceLooper;
    private volatile ServiceHandler mServiceHandler;

    public NonStopIntentService(String name) {
        super();
        mName = name;
    }

    private final class ServiceHandler extends Handler {
        public ServiceHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            onHandleIntent((Intent)msg.obj);
            // stopSelf(msg.arg1); <-- Removed
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
        thread.start();

        mServiceLooper = thread.getLooper();
        mServiceHandler = new ServiceHandler(mServiceLooper);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.obj = intent;
        mServiceHandler.sendMessage(msg);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        onStart(intent, startId);
        return START_STICKY;
    }    

    @Override
    public void onDestroy() {
        mServiceLooper.quit();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * This method is invoked on the worker thread with a request to process.
     * Only one Intent is processed at a time, but the processing happens on a
     * worker thread that runs independently from other application logic.
     * So, if this code takes a long time, it will hold up other requests to
     * the same IntentService, but it will not hold up anything else.
     *
     * @param intent The value passed to {@link
     *               android.content.Context#startService(Intent)}.
     */
    protected abstract void onHandleIntent(Intent intent);  

}
like image 120
Graeme Avatar answered Sep 19 '22 15:09

Graeme