0

I am making an application that needs to be running a thread that takes a frame using the camerakit package, and processes that frame to give a result.

This processing is expensive, so I it is not recommended to use UI thread, so the problem I had when using the HandleThread is that it is not connected to the Activity lifecycle, so the variable that store the camerakit is null within that thread.

Another point is that I need to update UI components with the result of this processing, taking that into consideration, what kind of thread should I use? Or should I use several types of Thread and connect them using some kind of message.

Alex
  • 3,301
  • 4
  • 29
  • 43
  • i dont understand what problems do you have with `HandleThread`... post your code – pskink Oct 03 '18 at 18:24
  • @pskink, I cannot touch anything in the UI. – Alex Oct 03 '18 at 18:27
  • 1
    see the second code snippet: https://stackoverflow.com/a/25096981/2252830 (with `Handler mHtHandler;` and `Handler mUiHandler;`) – pskink Oct 03 '18 at 18:28
  • HandlerThread if its message based, or just THread if it isn't. Actual changes to the UI thread will need to be posted back to a UI thread handler. – Gabe Sechan Oct 03 '18 at 18:34
  • @GabeSechan, but, how can I access an external variable? camera variable is constructed in Create method, but inside thread it is null. – Alex Oct 03 '18 at 18:38
  • 1
    pass it with `Message` for example - use `Handler#obtainMessage(int what, Object obj)` – pskink Oct 03 '18 at 18:39

1 Answers1

0

Look at AsyncTask. It has been developed specially for such tasks where you have to process the result on UI thread.

Ryosuke
  • 3,592
  • 1
  • 11
  • 28
  • But I need a thread that run forever. It is not only a task like download an image. – Alex Oct 03 '18 at 18:27
  • Then use IntentService and access and update the data to a static variable. Use LocalBroadcastManager to notify the activity from the IntentService and vice versa. – Ryosuke Oct 03 '18 at 18:42