Introduction to mobile programming with Androids.
-
Upload
maksim-golivkin -
Category
Technology
-
view
1.718 -
download
1
description
Transcript of Introduction to mobile programming with Androids.
Intro to programming with Androids
@MaksimGolivkin
Android dev @Uber
Plan
• Brave mobile world
• Daily life-cycle
• Different screens
• Hybrid applications
Why “mobile” development?
In developing world
And in the most posh economies.
Smartphones, tablets and more
Only an Internet device?
What smart devices are made of?
Memory Ports Positioning Radios Sensors Build-‐in Audio Cellular Mobile Audio
SD card USB GPS Wi-‐Fi Photo/video
HDMI Wi-‐Fi Bluetooth Light
A-‐GPS NFC AcceleraBon
MagneBc
Gyroscope
Proximity
… Flash, Stylo, Second Screen
Dongle empowering 3 billion $ business
Why Android?
Android creates a sweet choice.
Open mobile computing platform
64% in smartphones
40% in tablets
of device sales in 2012 Q3
Android accounts for
GooGPS show-case
Two big butts
• App profitability 1/6 of iOS
• Fragmentation
Summary
• Dawn of connected devices era.
• HW knowledge creates opportunities.
• Android is leading it.
Android OS
Designed for fast switching between apps
Applications “talk” between each other
There is always a Back button
Programming Android
APIs
• SDK – Java applications
• NDK – mostly games
• Hybrid – everybody should
Tools
• Eclipse
• Eclipse ADT plugin
• Android SDK
• USB drivers
Learn Android • d.android.com • stackoverflow.com
• AppDemo sample application • youtube for Google I/O
• Android OS source code • grepcode.com Books!
Script vs. Application
uber.com Init
Process
Output Display
Die PHP script life-cycle
Interact
Init
Process
Die
Display
State
Application life-cycle
Screen ~= Activity
Created
Resumed
Stopped
Launch
Press Home
Interact
Foreground In Memory
Created
Started
Resumed
Stopped
Launch
Paused
Interact
Open other
Activity
Visible Foreground In Memory
Started
Resumed
Stopped
Re-Launch
Press Home
Paused
Interact
Visible Foreground In Memory
Activities Stack (briefly)
1st Screen
Resumed 1.
2nd Screen
Stopped
Resumed
1.
2.
3rd Screen
Stopped
Resumed
Stopped
1.
2.
3.
Closes an application
Delegates responsibility
Pressing Back
Stopped
Resumed
Stopped
1.
2.
3.
Destroying last activity
Stopped
Resumed
1.
2.
Pressing Home
Stopped
Resumed
Stopped
1.
2.
3.
Stops everything
Stopped
Stopped
1.
2.
Stopped 3.
Returned to the app
Stopped
Resumed
Stopped
1.
2.
3.
Running In The Background?
… not all of them
Maintaining state
Activity/app life-time
• Parameters
+ Saved Instance State
• Singleton
Singleton is king. Mind the GC!
Persistence
• Shared Preferences
• Files
• Server
• SQLite
Do you really need it?
Service
Use cases
• Long actions in between activities
• Notifications, when app is dead
• Intensive calculations
Consider simply using Threads. Twice!
Fragments
Fragments enable multi-pane layouts
Resources
Life of an image
Resources r = getResources(); Drawable d =
r.getDrawable(R.drawable.ic_american_express); ImageView icon =
(ImageView) findViewById(R.layout.card_logo); icon.setDrawable(d);
Using resources
Drawable vs. View
Screen ~= Activity Everything else ~= Views
Summary (Android OS) • Android is popular but poor, yet
• Learn life-cycle by heart
• Assess feasibility of Persistence and
Services. Twice.
Many screens
Different resolutions
320x480 px 1280x720 px
42 dp
Same physical size
screen_ density = pixel _width2 + pixel _height2
diagonal _ in_ inches
Screen density
Many resolutions 320x426 legacy phones 240x320 legacy phones 320x533 320x576
legacy phones
320x480 phones 320x533 320x568
phones
320x480 360x640 400x640
new phones
640x1067 640x1138
tweener tablets
480x800 480x854
600x1024
tweener tablets
1024x768 1280x768 1280x800
tablets
...
Landscape is yet another resolution
Patterns
Stretching
Adding margins
Multi-pane
Switch points
Switch points
small size portrait small size landscape normal size portrait
normal size landscape large size portrait
Home base 240dp 320dp 360dp 400dp 426dp 480dp 533dp 568dp 578dp 640dp
resized elements
margins added
switch point (another layout)
Expensive way
small size portrait small size landscape normal size portrait
normal size landscape large size portrait
Home base 240dp 320dp 360dp 400dp 426dp 480dp 533dp 568dp 578dp 640dp
Boom!
resized elements
margins added
switch point (another layout)
Prepairing graphics
Density buckets
ldpi (low) 100 ~ 140 dp
mdpi (medium) 140 ~ 200 dp
hdpi (high) 200 ~ 280 dp
xhdpi (extra high) 280 ~ 340 dp
1 dp = ? px
ldpi 0.75
mdpi 1
hdpi 1.5
xhdpi 2
Nine-patch
Resizable area
Content area
Nine patch • Buttons
• Backgrounds
Summary (Many screens) • Needs investment, but little surprise.
• One layout for a start.
• Resolution ignorance is ugly,
but not ineffective.
Hybrid apps
“Our biggest mistake was betting too much on HTML5”, - Mark Zuckerberg
HTML5 reality in 2012
Hybrid champion: LinkedIn
Native
Native
HTML/CSS
Hybrid architecture
Native
JavaScript API
HTML/CSS/JavaScript
WebView webView = (WebView) findViewByid(R.id.webview);
webView.addJavascriptInterface(obj, "Android"); final String html = AssetUtil.readAssetsFile(
context, filename); webView.loadDataWithBaseURL(
"file://", html, "text/html","utf-8", null);
Native side
function onClick() {
Android. jsOnNextArticle(this.id); }
JavaScript side
public void showArticle(long id, String content) { webView.loadUrl("javascript: jsShowArticle(" + id
+ ", \" + Uri.encode(content) + "\");") } .. public void jsOnNextArticle(long articleId) {
… }
“API” glue
public void showArticle(long id, String content) { webView.loadUrl("javascript: jsShowArticle(" + id
+ ", \" + Uri.encode(content) + "\");") } .. public void jsOnNextArticle(long articleId) {
… }
“API” glue
Summary (Hybrid apps)
• Content centered apps
• FAQ, User License, …
• 1-1.5x more effort than native
• Pays of when targeting >= 3 platforms
Read ON - The real problem with Android fragmentation
- Where does Android fragmentation hide?
- The technical adventure building a hybrid app.
- Fast track to Android design.
Interested in Android? @MaksimGolivkin
Care to give feedback? [email protected]