0

I am making an application that should do the following tasks:

  1. image processing in a bitmap
  2. receive String by doing OCR(optical character recognition) in the bitmap that received from task 1
  3. retrieve data from the app's Room's database by searching the keyword that received from task 2
  4. Show the retrieved data on a TextView

I want to find a way to these 3 tasks sequentially since they are connected with each other. But i don't know how.

At first i thought that if i could just do

Bitmap b = imageProcessing(bitmap);
String keyword = ocr(b);
String data = retrievedata(keyword);
textview.setText(data);

But as I had read in many articles that's not the right way to run different tasks, because like that I run everything on the main thread

I also thought of using Executors for every different task but the issue is that by this way i won't have access to whatever i need to receive from every task. (e.x i won't be able to use the string from the task 3 to the task 4).. if i understood right

 ExecutorService executor = Executors.newSingleThreadExecutor();
    Handler handler = new Handler(Looper.getMainLooper());

    executor.execute(() -> {
        //Background work here
        handler.post(() -> {
            //UI Thread work here
        });
    });

I found this codelab that talks about WorkManager and i think that maybe is suitable for me. https://developer.android.com/codelabs/android-workmanager-java#0

I also found this tutorial about HandlerThread but I am still not sure if this is suitable for me. https://www.youtube.com/watch?v=n0mkYSjldeA

I am really confused because i am very new to android. Could someone recommend me something?

nCoder
  • 53
  • 5
  • WorkManager is really meant for doing stuff when you app is in the background like syncing content. Using work manager here I dont think makes much sense since you are no trying to do stuff while your app is in the background – tyczj Oct 01 '21 at 20:27
  • hello @tyczj and thank you for the reply. Understandable. But what do you think it would be the best way to run these 4 tasks sequentially ? – nCoder Oct 01 '21 at 20:31

1 Answers1

0

There are many ways to run a bunch of codes sequentially but I think in this way it is better for you to use three LiveData and write answers to those LiveData then observe them in your activity/fragment/... then call next function to do that. I know it seems to be complicated but you will be able to edit your answer in every step without changing sequentially or changing another job. And I suggest using coroutine for every process that does not need UI thread.

Reza Abedi
  • 419
  • 4
  • 16