Android Topics UI Thread and Limited processing resources Construct an exploratory app Three Experiments to test Threads
What are Intents? Most apps require us to send intents Example: the Coffee Ordering App will require us to send off the order summary in an email. The app must launch an email app that exists on the device. An intent is a message that requests some type of action to be performed by another app component. This could be a camera, email, Google Maps, or even an activity in another app.
Limited Processing Resources Mobile devices have a lot of resource constraints: memory and CPU (processing power). Low-processing devices must consider background threads for heavy processing.
UI Thread UI Thread is the main thread of execution. All your code runs on the UI thread by default. The UI Thread is important thread for interacting with your user. Being able to design code that doesn’t blocking the UI thread is a very important concept.
Exploratory App Experiment with code that runs on UI thread. What happens when time-consuming work is done on the main thread. How can we use a background thread for time-consuming work? TASK: Build the layout.
Experiment 1 Start work Button and End work Button. Test the app using a non intensive workload, displaying a message in the Log.i. public void workItOut(View view){ if (view.getId() == R.id.start_button) Log.i("THREAD DEMO", "Working on it"); else Log.i("THREAD DEMO", "Not Working"); }
Experiment 2 Send an unreasonable amount of output to the log window when the user clicks “Start Work”. The logcat should fill up with the string “Working on it”. Implement this by throw the Log.i statement in a loop and only stop when the user clicks the button labelled “Stop Work”. What happens?
Experiment 2 Produces a ANR Why?
Why did the ANR occur? The loop work is completely taking over the the UI Thread. Too much work on this thread often leads to a poor performing app or an application that doesn’t respond (ANR – Application Not Responding). The UI Thread MUST be able to respond to the user. Resource intensive work should never be done on the UI thread. Once it is blocked, it cannot handle anything else.
Android Operating System At runtime, Android Operating System hosts app as a process. Each app gets a dedicated process for execution.
Consider an Individual Process/App There can be any number of threads in a process. In most apps, there is a need to be smart in how you allocate work for the main thread, shown in red. If you have a task that takes a significant amount of time, such as 5 to 3 seconds, then it will block the UI Thread and eventually seriously impact the app.
Background Thread A best approach is to move time- consuming work off the main UI thread. Create a separate thread to can handle all tasks interfering with the main UI thread. An App can have multiple threads of execution running concurrently.
Creating a Background Thread in Java The class Thread implements Runnable. Runnable is an interface specifically for executing background threads. The Thread must define a method called run().
Solution to Experiment 3: public void workItOut(View view) { if (view.getId() == R.id.start_button) { if (!isDoingWork) { isDoingWork = true; new Thread(new Runnable() { @Override public void run() { while (isDoingWork) Log.i("THREAD DEMO", "Working on it"); } }).start(); } } else { isDoingWork = false; } }
Operations which should be carried out on separate threads. Heavy calculations Long initialization Networking Database operations