1 / 15

Android Topics

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.

hrobinson
Download Presentation

Android Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Android Topics UI Thread and Limited processing resources Construct an exploratory app Three Experiments to test Threads

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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"); elseLog.i("THREAD DEMO", "Not Working"); }

  7. 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.istatement in a loop and only stop when the user clicks the button labelled “Stop Work”. • What happens?

  8. Experiment 2 Produces a ANRWhy?

  9. 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.

  10. Android Operating System • At runtime, Android Operating System hosts app as a process. • Each app gets a dedicated process for execution.

  11. 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.

  12. 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.

  13. 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().

  14. Solution to Experiment 3: public void workItOut(View view) {if (view.getId() == R.id.start_button) {if (!isDoingWork) {isDoingWork= true;new Thread(new Runnable() { @Overridepublic void run() {while (isDoingWork)Log.i("THREAD DEMO", "Working on it"); } }).start(); } } else {isDoingWork= false; }}

  15. Operations which should be carried out on separate threads. • Heavy calculations • Long initialization • Networking • Database operations

More Related