17. Logging
Why is logging important?
Good Practice.
Constant monitoring.
Helps debug fast without the need for breakpoints.
In the context of this lesson: Thread awareness.
Don’t leave logs open in production.
26. Processes - Android Priority
If system is stressed and needs to clear memory - the process will
be killed together with its’ threads.
ProcessProcess
56. Main Thread
UI thread responsibilities:
Dispatches events.
Draws on screen.
Handles user input.
57. Process
What’s On The Main Thread?
Main Thread (UI Thread)
System Event Input Event Service Application UI Drawing
58. Example: Action lifecycle on UI thread
User presses on a
button
UI Thread
dispatches touch
event to the
widget
Widget sets its
pressed state
Posts an
invalidate to the
request queue on
UI thread
The UI thread
dequeues the
request and
notifies widget to
redraw itself
59. Reminder - Blocking Action Definition
A Thread that is blocked is waiting for a completion of an operation.
60. Process
What’s On The Main Thread?
Main Thread (UI Thread)
System
Event
UI
Drawing
Your code!~!!!~!
16ms 16ms 16ms
UI
Drawing
UI
Drawing
16ms 16msDropped Frame
61. We Have A Winner!
Smooth
Motion
60
No
Difference
60+
Flip Book
12
Movies
Frames Per Second
Fluid Motion
24
+effects
Smooth
Motion
60
67. Main Thread
What happens if the app is slow to respond?
Do you think users will say:
“Oh it’s my slow connection”
“I shouldn’t have scrolled so fast”
“Silly me wanting to see the whole image right when the screen opens”
68.
69. We’ve established it’s the
developer’s job
to offload long operations from the
Main Thread
to an a Non UI Thread
75. AsyncTask
Fit for short operations.
Fit for operations which require UI Manipulation at the end.
Unaware of activity life cycle.
Single time execution.
Serial vs. Concurrent for all AsyncTasks.
Has a state and can be cancelled.
86. Before we go let’s see how to get back!
Activity.runOnUiThread(Runnable)
View.post(Runnable)
BroadcastReceiver
Main Thread (UI Thread)
Thread A
Thread B
93. Worker Thread - Recipe
1.Create a Thread.
2.Attach a Looper to a Thread.
3.Get the Handler from the Looper.
4.Use Handler to send messages (tasks).
98. Looper
Class used to run a message loop for a thread. Threads by default do
not have a message loop associated with them; to create one, call
prepare() in the thread that is to run the loop, and then loop() to have
it process messages until the loop is stopped.
99. A Deeper Look
Main Thread (UI Thread)
Looper
Message queue
Handler
Handle
MSG
Send
MSG
Process
145. Permissions
Android is a privilege-separated operating system, in which each
application runs with a distinct system identity (Linux user ID and
group ID). Parts of the system are also separated into distinct
identities. Linux thereby isolates applications from each other and
from the system.
147. Permissions - Level
Protection level
Normal - (flashlight, internet, NFC)
Dangerous - (Read / write external storage)
Signature - (defined per use case)
SigantureOrSystem - (Install 3rd party applications)
Application defined permission.
148. Permissions - Normal vs. Dangerous
Normal: Bluetooth, Internet, NFC.
Dangerous: read/write external storage, contacts, location.
What is the key difference?
149. Permissions - Android 6.0
TargetSDKVersion
If you wrote 24 you will crash if not handled.
If you wrote <24 you will be spared.
150. Permissions - Android 6.0
Still define permissions in AndroidManifest.xml
protectionLevel = normal are granted by default.
protectionLevel = Dangerous require you
ask at runtime.
Request 1 time without consequence.
151. Permissions - Android 6.0
Still define permissions in AndroidManifest.xml
protectionLevel = normal are granted by default.
protectionLevel = Dangerous require you
ask at runtime.
Request 1 time without consequence.
157. Network requests
Under the hood Android’s HTTP libraries use java socket API.
Socket definition: a software endpoint that can create a bi-directional
communication link between software processes.
Socket SocketTCP/IP