Differences

This shows you the differences between two versions of the page.

Link to this comparison view

smd:laboratoare:04 [2021/04/08 11:20]
alexandru.chirvase [Threading in Android]
smd:laboratoare:04 [2021/05/12 17:46] (current)
adriana.draghici [Tasks]
Line 15: Line 15:
 :!: Calling methods from the Android UI API must be done on the UI thread. For example, if you want to show a toast from a worker-thread,​ use [[https://​developer.android.com/​reference/​android/​app/​Activity#​runOnUiThread(java.lang.Runnable)|runOnUiThread]]. :!: Calling methods from the Android UI API must be done on the UI thread. For example, if you want to show a toast from a worker-thread,​ use [[https://​developer.android.com/​reference/​android/​app/​Activity#​runOnUiThread(java.lang.Runnable)|runOnUiThread]].
  
-Regarding the relationship between the threading components and the **lifecycle** of the UI components (activities and fragments), certain threading objects can be tied and other not. The IntentServices we discussed in Lab3 are not tied to the activities or fragments lifecycle, but the AsyncTasks (Android specific threading components),​ are terminated when the UI components are destroyed.+Regarding the relationship between the threading components and the **lifecycle** of the UI components (activities and fragments), certain threading objects can be tied to the UI lifecycle ​and other not. The IntentServices we discussed in Lab3 are not tied to the activities or fragments lifecycle, but the AsyncTasks (Android specific threading components),​ are terminated when the UI components are destroyed.
  
 In [[smd:​laboratoare:​03#​using_intent_services|Lab 3]] we presented IntentServices which also run on separate threads. The mechanisms we will focus on this lab, some of them you might be familiar with from Java: In [[smd:​laboratoare:​03#​using_intent_services|Lab 3]] we presented IntentServices which also run on separate threads. The mechanisms we will focus on this lab, some of them you might be familiar with from Java:
Line 24: Line 24:
 We will also briefly discuss AsyncTasks, which are not recommended and will be deprecated in the next Android release (R). We will also briefly discuss AsyncTasks, which are not recommended and will be deprecated in the next Android release (R).
  
-Because ​the SMD labs' scope do not include learning a new language, we will not address in this lab asynchronous programming using the **[[https://​developer.android.com/​topic/​libraries/​architecture/​coroutines|coroutines]]** provided in **Kotlin**. Nonetheless,​ if you want to learn and code in Kotlin, or are already familiar with it, feel free to use them in your project. Useful video overview: [[https://​www.youtube.com/​watch?​v=BOHK_w09pVA|Understand Kotlin Coroutines on Android (Google I/​O'​19)]]. +<note tip>Because ​this course'scope does not include learning a new language, we will not address in this lab asynchronous programming using the **[[https://​developer.android.com/​topic/​libraries/​architecture/​coroutines|coroutines]]** provided in **Kotlin**. Nonetheless,​ if you want to learn and code in Kotlin, or are already familiar with it, feel free to use them in your project. Useful video overview: [[https://​www.youtube.com/​watch?​v=BOHK_w09pVA|Understand Kotlin Coroutines on Android (Google I/​O'​19)]]. 
 +</​note>​
 Other threading mechanisms you can use and are not covered in this lab: Other threading mechanisms you can use and are not covered in this lab:
   * **Reactive programming** using **[[https://​blog.gojekengineering.com/​multi-threading-like-a-boss-in-android-with-rxjava-2-b8b7cf6eb5e2|RxJava]]** or **RxKotlin** - do not need to explicitly create threads, just choose a scheduler   * **Reactive programming** using **[[https://​blog.gojekengineering.com/​multi-threading-like-a-boss-in-android-with-rxjava-2-b8b7cf6eb5e2|RxJava]]** or **RxKotlin** - do not need to explicitly create threads, just choose a scheduler
Line 105: Line 105:
 === Using Handlers ===  === Using Handlers === 
 We need a **messaging mechanism between threads**, and Android offers [[https://​developer.android.com/​reference/​android/​os/​Handler|Handlers]]. These are closely connected to other objects needed for messaging: We need a **messaging mechanism between threads**, and Android offers [[https://​developer.android.com/​reference/​android/​os/​Handler|Handlers]]. These are closely connected to other objects needed for messaging:
-  * [[https://​developer.android.com/​reference/​android/​os/​Looper|Looper]] - provided to a Handler, ​it runs the message queue in loop+  * [[https://​developer.android.com/​reference/​android/​os/​Looper|Looper]] - provided to a Handler, ​keeps thread alive, loops through MessageQueue and sends Messages to the corresponding Handler to process it
   * [[https://​developer.android.com/​reference/​android/​os/​Message|Message]] - the message to be sent to a Handler   * [[https://​developer.android.com/​reference/​android/​os/​Message|Message]] - the message to be sent to a Handler
   * [[https://​developer.android.com/​reference/​android/​os/​MessageQueue|MessageQueue]] - the messages dispatached to a Looper, the developer doesn'​t interact directly with it, most interaction is through the Handler class   * [[https://​developer.android.com/​reference/​android/​os/​MessageQueue|MessageQueue]] - the messages dispatached to a Looper, the developer doesn'​t interact directly with it, most interaction is through the Handler class
Line 253: Line 253:
 ===Task 2 (4p)=== ===Task 2 (4p)===
  
-Mock the download of several files (at least 10) in parallel on multiple threads using Runnables and ThreadPoolExecutor ([[https://​developer.android.com/​reference/​java/​util/​concurrent/​ThreadPoolExecutor|documentation]],​ [[https://developer.android.com/training/multiple-threads/​create-threadpool#​ThreadPool|creation ​example]]).+Mock the download of several files (at least 10) in parallel on multiple threads using Runnables and ThreadPoolExecutor ([[https://​developer.android.com/​reference/​java/​util/​concurrent/​ThreadPoolExecutor|documentation]],​ [[https://www.journaldev.com/1069/threadpoolexecutor-java-thread-pool-example-executorservice|example]]).
   * For the "​mock"​ download, each thread should sleep a random number between 100 and 200 ms.   * For the "​mock"​ download, each thread should sleep a random number between 100 and 200 ms.
   * The UI should update in real time a text view with the following content:   * The UI should update in real time a text view with the following content:
Line 262: Line 262:
       …       …
       </​code>​       </​code>​
-   ​* ​The text view must have the option ​android:​inputType="​textMultiLine".+   ​* ​To show multiple line you can add the ''​android:​inputType="​text"''​ to the TextView
  
  
smd/laboratoare/04.1617870040.txt.gz · Last modified: 2021/04/08 11:20 by alexandru.chirvase
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0