This is an old revision of the document!


Android Session 3: Bluetooth Low Energy Communication

Objectives

  • Store data using SharedPreferences
  • Build and interact with databases in Android

Threading in Android

When an application is launched, the system creates a process for it (or more, if it declares services in separate processes) and a main thread (UI thread). As discussed in the previous labs, any code you write in your components (unless they are Intent Services) is run on the main thread. But an app might need to perform processing in worker threads, for example, in case of networking or database operations. If we block the UI thread while performing computational intensive code or waiting for certain events, the system will display an ANR (Application Not Responding) dialog.

For working with threads and scheduling tasks we can leverage language-specific APIs for threading, Android specific components or many third-party libraries.

:!: 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 runOnUiThread.

TASK

  1. Setup SharedPreferences (Follow TODO 1)
    1. Get sharedPreferences
    2. Get co2Threshold and vocThreshold from the corresponding EditText and save the values to sharedPreferences
    3. Get co2Threshold and vocThreshold from sharedPreferences and set the values to the corresponding EditText
  2. Setup database (Follow TODO 2)Room library documentation
    1. Setup Entity
    2. Setup Dao
    3. Setup Database
  3. Save records into db every X seconds using HandlerThread (Follow TODO 3)
  4. Show records in the HistoryActivity (Follow TODO 4)
    1. Load data on a different thread (not on the UI thread)
    2. Set the list of records to the adapter on the UI thread
fss/sessions/session4-android.1562849990.txt.gz ยท Last modified: 2019/07/11 15:59 by beniamin.dobre
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