0

I am new to the development of Android applications. I came across a situation where i need to execute a code in a background thread that does not do something with the UI. Based on the result obtained from the background thread I need to display an alert message on the UI. I heard about AsyncTask class which can be used to execute code in background process but in Android documentation i found that it was deprecated from API Level R.

Can you please suggest on how a particular block of code can be executed in background thread? and also based on the result display the alert on the main UI.

Kamal
  • 453
  • 1
  • 10
  • 22
  • Google recommends that you use the concurrency framework that java already has. https://developer.android.com/reference/android/os/AsyncTask – libanbn May 27 '20 at 16:41
  • https://github.com/crawlinknetworks/android-promise This is excellent Library, similar to Prowerful Javascript Promises to execute async work. – Ravi Rajput May 27 '20 at 16:41
  • since you are starting on Android , it would be easier for you to use a normal thread class to perform your operation in background and a Handler class to post it back to the UI thread – p.mathew13 May 27 '20 at 16:47
  • @libanbn It says that AsyncTask was deprecated.. – Kamal May 27 '20 at 16:54
  • @Kamal Yes, and on that site they also said to use the default concurrency classes in java. Here are some examples on how to use them: https://winterbe.com/posts/2015/04/07/java8-concurrency-tutorial-thread-executor-examples/ – libanbn May 27 '20 at 17:02
  • @libanbn is it the threads and runnables? Or executor services ?that needs to used for my purpose What is the best solution? Please suggest – Kamal May 27 '20 at 17:21
  • @p.mathew13 Can you Please suggest an example on how it can be implemented? Any links that needs to be referred will be much more helpful – Kamal May 27 '20 at 17:24
  • @Kamal The tread and runnable should work fine. If you want to display something based on the result of the async thread, you can use callbacks to your UI thread. – libanbn May 27 '20 at 17:58
  • @Kamal This can be a good place for you to start https://medium.com/@ankit.sinhal/handler-in-android-d138c1f4980e – p.mathew13 May 28 '20 at 06:45

0 Answers0