the happy path to android development
TRANSCRIPT
@andrezzoid
Happy Path
Happy Path - [hap-ee pahth]
“In the context of software or information modeling, a happy path is a default scenario featuring no exceptional or error conditions, and comprises the sequence of activities executed if everything goes as expected.” –Wikipédia
@andrezzoid
About
AGE: 26 ANOS
WORK: COMPUTER SCIENCE AND ENGINEERING @ ISEL
TEACHER, COURSE CREATOR @ FORMABASE
FREELANCER @ EVERYWHERE
André Jonas
@andrezzoid
Agenda
Android Overview
App Structure
Activities
User Interface
Intents
Broadcast Receivers
Services
@andrezzoid
Requirements
Object-Oriented Programming
Classes, interfaces, inheritance, polymorphism, etc.
Event-based Programming
Callback
Concurrent Programming
Concept of Process, Thread
Design Patterns (not mandatory)
@andrezzoid
Environment
You should have this setup:
Java JDK 6 or JDK 7 (for Android 5.0)
Android SDK
HTTP://DEVELOPER.ANDROID.COM/SDK/
Android Studio IDE (Intellij CE)
Genymotion
FREE FOR PERSONAL USE
HTTPS://WWW.GENYMOTION.COM/
Hardware Virtualization (e.g. Intel VT-x)
@andrezzoid
Android Overview
Linux-based Operating System
Designed primarily for touchscreen mobile devices
Now also seen on TV’s, computers and cars are comingsoon
Has it’s own optimized JVM
Dalvik (JIT compilation) or ART (AOT compilation)
Already in 5.0 (Lollipop)
Each version has it’s own API level (e.g. Lollipop is API 21)
Open Source
www.grepcode.com/search/?query=google+android&entity=project
@andrezzoid
Android App
Written in Java (no shit sherlock!)
Compilation is done to a .dex format(android specific bytecode)
APK (Android Package) file:
Compiled Code
Resources
XML (VIEWS, STRINGS, STYLES, …)
IMAGES
@andrezzoid
Android App
In Android Studio The app structure:
|--\src|---- \main|------ \java|-------- // your java code here|------ \res|-------- // your resources here|---- AndroidManifest.xml
@andrezzoid
App Components
Essential building blocks of any Android app
Each has a very well defined lifecycle
Activity
Broadcast Receiver
Service
Content Provider (maybe next talk)
@andrezzoid
Manifest.xml
Where app components are registered
Where permissions are declared
Where target, min and max SDK are defined
Defined in XML
@andrezzoid
Activity
Represents a single action of the app
Write e-mail, check weather, view music list, etc.
Can have none, one or several Views
Needs to be declared in the Manifest
<activity android:name=“xxx” android:label=“xxx”>
Extend from Activity class
@andrezzoid
Activity
Lifecycle:
Callbacks (onCreate, onStart, etc.)
Need to overridesome of thesemethods and specifywhat needs to bedone in that state
@andrezzoid
Activity
State is preserved ifActivity is destroyed
Change to otherActivity
Rotate device
Etc.
Android uses Bundleclass to transportstate
Key-Value Map
@andrezzoid
A Detour From the HappyPath
UI Thread
Application is launched
SYSTEM CREATES A THREAD OF EXECUTION FOR THE
APPLICATION, CALLED “MAIN” (OR UI THREAD)
RESPONSIBLE FOR UI, TOUCH EVENTS, YOUR CODE, ETC.
Long operation should be executed in another thread
One needs to explicitly create them(AsyncTasks)
@andrezzoid
A Detour From the HappyPath
ANR (Application Not Responding)
If app doesn’t respond to key press or touchevents for 5 sec.
If BroadcastReceiver hasn’t finishedexecuting within 10 sec.
THIS IS REEEEALLY BAD!!!
SERIOUSLY, DON’T DO THIS OR IMMA HIGH FIVE YOU IN
THE FACE!
@andrezzoid
A Detour From the HappyPath
The 2 rules of the Android single threadmodel:
1. Do not block the UI Thread
2. Do not block the UI Thread
@andrezzoid
User Interface
UI Elements that form the UI
Buttons, Labels, Texts, Lists, Dialogs, DatePickers, …
Defined in XML
Can contain other Views (e.g. Lists, Layouts)
Layouts files stay in res\layout folder
Can be associated to Activity
@andrezzoid
User Interface
Vision and Principles
https://developer.android.com/design/index.html
Material Design
Visual “language” based on paper surfaces, light and shadow
http://www.google.com/design/spec/material-design/introduction.html
COMPONENTS , PATTERNS, COLORS, ICONOGRAPHY, ETC.
@andrezzoid
User Interface
How to bind a View to an Activity?
public static void onCreate(Bundle savedInstanceState) {// ...setContentView(R.layout.activity_main);
}
@andrezzoid
User Interface
What if I wanted to, let’s say, get a button so I can register an “on click” event?public static void onCreate(Bundle savedInstanceState) {// ...Button myButton = (Button) findViewById(R.id.btn);myBytton.setOnClickListener(...);
}
@andrezzoid
R
Ok, so WTF is that R thing?
setContentView(R.layout.activity_main);
findViewById(R.id.btn);
@andrezzoid
R
R is a class generated by an SDK tool
Stores resource identifiers (Images, Strings, Layouts, View elements, andmore …)
public final class R {public static final class id { public static final int btn=0x7f080031;// ...}// ...
}
@andrezzoid
Intents
Mechanism to invoke app components
Your app or other apps
Defines the “intention” to do some work
Can pass data around
Not an App Component (more of a Foundational Component)
@andrezzoid
Implicit Intents
The system decides what app serves yourIntent based upon an action name
Normally used to call other activities thatdo things your app can’t do
Open an URL, add event to Calendar, viewcontacts
public static void invokeWebBrowser(Activity activity) {Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://www.google.com"));activity.startActivity(intent);
}
@andrezzoid
Explicit Intents
You explicitly tell which Component willserve your Intent
The component that
shall be called
public static void redirectLogin(Activity activity) {Intent intent = new Intent(activity, LoginActivity.class);activity.startActivity(intent);
}
@andrezzoid
Broadcast Receivers
Normally just called “Receiver”
Responds to broadcast events (yeah, really!)
Application or System events
The message is an Intent
@andrezzoid
Broadcast Receivers
Can be registered in the Manifest
<receiver android:name=“xxx” enabled=“true">
Which events to notify?
Extend BroadcastReceiver
Implement onReceive( )
Runs on the ui thread
REMEMBER, DON’T BLOCK THE UI THREAD!
@andrezzoid
Broadcast Receivers
Custom broadcasts
Some app may register on your event
Android may start the other app processso the receiver handles the event
public static void broadcastSomething(Activity activity) {Intent intent = new Intent("pt.andrezzoid.example.SMARTTALK");activity.sendBroadcast(intent);
}
APP B APP C
APP A
@andrezzoid
Services
Can perform long-running operations
Runs in the background (but not in a background thread)
Even if the user switches to another application
No interaction with User. No UI.
Needs to be declared in the Manifest
<service android:name=“xxx” />
Can make it visible to other apps withexported=“true”
@andrezzoid
Services
Start service by calling startService( )
With explicit Intent
public static void downloadFile(Activity activity) {Intent intent = new Intent(activity, DownloadService.class);intent.putExtra("DOWNLOAD_URL", ...);activity.startService(intent);
}
@andrezzoid
Services
So we can stay on the happy path
Extend from IntentService
Implement onHandleIntent( )
Already uses a worker thread
Stops itself after all requests have beenhandled
@andrezzoid
Where to go next?
Async that shit
Loaders
AsyncTasks
Handlers
Database
Content Providers
View re-use
Fragments
Optimizations
@andrezzoid
More
Books
Pro Android 4
Websites
http://developer.android.com/training/index.html
http://www.vogella.com/tutorials/android.html
http://www.grokkingandroid.com/
https://github.com/JStumpp/awesome-android
@andrezzoid
Contacts
http://andrejonas.me
@andrezzoid
Code:
http://github.com/andrezzoid/happy_path_android