android workshop (night of chances 2015)
TRANSCRIPT
Android
Workshop Štefan Mitrík
Why Android?
1 000 000 000 active users
+
Workshop overview
• Basic theory
– Very short
– Quite a candy
• App description
– Simple architecture
• Coding
Activity
Activity
• Usually single screen
• Custom lifecycle
3. onStop()
Activity 1. onCreate()
4. onDestroy()
2. onStart()
...
Activity
• Screen orientation change
Activity
Activity A
1
2
3
Activity
Activity A
1
2
3
Activity
Activity A Activity B
1
2
3
2
Activity
Activity A Activity B
1
2
3
2 Intent
Intent
Intent
Activity A Activity B
1
2
3
2
Intent
Activity B
Item 2
Intent
start external app
Intent - start
external app
Activity A
1
2
3
Intent
open
website
eset.com
Apps lifecycle
Apps lifecycle
Apps lifecycle
Apps lifecycle
Memory (RAM)
Apps lifecycle
Memory (RAM)
onCreate()
Apps lifecycle
Memory (RAM)
Memory (RAM)
onStop()
Memory (RAM)
Apps lifecycle
Memory (RAM)
onCreate()
Apps lifecycle
Memory (RAM)
Memory (RAM)
onStop()
Apps lifecycle
Memory (RAM)
Apps lifecycle
Memory (RAM)
Low memory
Apps lifecycle
Memory (RAM)
Low memory onDestroy()
Apps lifecycle
Memory (RAM)
Apps lifecycle
Memory (RAM)
onCreate()
How to work in background? For example music player
Service
• Long running operations
• Custom lifecycle
• No UI
Activity A
Playlists
Service – Playing music
Service
Activity B
Player controls
Activity A
Playlists
Service – Playing music
Service
Activity B
Player controls
Activity A
Playlists
Service – Playing music
Service
Activity B
Player controls
play pause
Apps lifecycle
Memory (RAM)
Low memory
App with
service
Apps lifecycle
Memory (RAM)
Low memory onDestroy()
App with
service
Broadcasts
Broadcasts
Broadcasts
Broadcast
receivers
Broadcasts
Broadcast
receivers
Incomming
message
Broadcasts
Broadcast
receivers
Incomming
message
• Activity – usually single screen
• Service – background, long running
• Intent – communication (inner/inter)
• Broadcast – data/events propagation
Theory summary
Problem to solve
Measure phone usage
Inspiration
Display
Statistics
Display
Statistics
Architecture
Single Activity App
UML 5.5 notation
Architecture
Single Activity App
Architecture Broadcast receiver
(display on/off)
Single Activity App
Architecture Broadcast receiver
(display on/off)
screen turned on
Single Activity App
Architecture Broadcast receiver
(display on/off)
screen turned on
Service
Single Activity App
Let’s code!
Step 1
MainActivity.java
WakeUps Application
AndroidManifest.xml
• Declarations
• Permissions
• …
Step 2
MainActivity
WakeUps Application
Service DisplayStatsService
ScreenBroadcastReceiver
Step 2
MainActivity
WakeUps Application
Service DisplayStatsService
ScreenBroadcastReceiver
1 Start Service
Step 2
MainActivity
WakeUps Application
Service DisplayStatsService
ScreenBroadcastReceiver
1 Start Service
2
Listen to OFF
broadcasts
Step 2
MainActivity
WakeUps Application
Service DisplayStatsService
ScreenBroadcastReceiver
1 Start Service
2
Listen to OFF
broadcasts
3
Display “Hello Kitty” message
when screen turns on
4
Step 3
MainActivity
WakeUps Application
Service DisplayStatsService
ScreenBroadcastReceiver
ON/OFF 5
Pass info to
service
Step 4
MainActivity
WakeUps Application
Service DisplayStatsService
ScreenBroadcastReceiver
ON/OFF
6 Calculate
last wake up
duration
Persistence Helper
Step 5
MainActivity
WakeUps Application
Service DisplayStatsService
ScreenBroadcastReceiver
ON/OFF
Display total
wake ups
number Persistence Helper
7
Thank you!