Top Banner
@andrezzoid THE HAPPY PATH TO ANDROID DEVELOPMENT #happypath #smarttalks
40
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The happy path to Android development

@andrezzoid

THE HAPPY PATH TO

ANDROID DEVELOPMENT

#happypath #smarttalks

Page 2: The happy path to Android development

@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

Page 3: The happy path to Android development

@andrezzoid

About

AGE: 26 ANOS

WORK: COMPUTER SCIENCE AND ENGINEERING @ ISEL

TEACHER, COURSE CREATOR @ FORMABASE

FREELANCER @ EVERYWHERE

André Jonas

Page 4: The happy path to Android development

@andrezzoid

Agenda

Android Overview

App Structure

Activities

User Interface

Intents

Broadcast Receivers

Services

Page 5: The happy path to Android development

@andrezzoid

Requirements

Object-Oriented Programming

Classes, interfaces, inheritance, polymorphism, etc.

Event-based Programming

Callback

Concurrent Programming

Concept of Process, Thread

Design Patterns (not mandatory)

Page 6: The happy path to Android development

@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)

Page 7: The happy path to Android development

@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

Page 8: The happy path to Android development

@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

Page 9: The happy path to Android development

@andrezzoid

Android App

In Android Studio The app structure:

|--\src|---- \main|------ \java|-------- // your java code here|------ \res|-------- // your resources here|---- AndroidManifest.xml

Page 10: The happy path to Android development

@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)

Page 11: The happy path to Android development

@andrezzoid

Manifest.xml

Where app components are registered

Where permissions are declared

Where target, min and max SDK are defined

Defined in XML

Page 12: The happy path to Android development

@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

Page 13: The happy path to Android development

@andrezzoid

Activity

Lifecycle:

Callbacks (onCreate, onStart, etc.)

Need to overridesome of thesemethods and specifywhat needs to bedone in that state

Page 14: The happy path to Android development

@andrezzoid

Activity

Page 15: The happy path to Android development

@andrezzoid

Activity

State is preserved ifActivity is destroyed

Change to otherActivity

Rotate device

Etc.

Android uses Bundleclass to transportstate

Key-Value Map

Page 16: The happy path to Android development

@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)

Page 17: The happy path to Android development

@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!

Page 18: The happy path to Android development

@andrezzoid

A Detour From the HappyPath

Page 19: The happy path to Android development

@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

Page 20: The happy path to Android development

@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

Page 21: The happy path to Android development

@andrezzoid

User Interface

Page 22: The happy path to Android development

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

Page 23: The happy path to Android development

@andrezzoid

User Interface

How to bind a View to an Activity?

public static void onCreate(Bundle savedInstanceState) {// ...setContentView(R.layout.activity_main);

}

Page 24: The happy path to Android development

@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(...);

}

Page 25: The happy path to Android development

@andrezzoid

R

Ok, so WTF is that R thing?

setContentView(R.layout.activity_main);

findViewById(R.id.btn);

Page 26: The happy path to Android development

@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;// ...}// ...

}

Page 27: The happy path to Android development

@andrezzoid

R

Two ways to access

In Code: R.id.btn

In XML: @id/btn Resource Name

Resource Type

Page 28: The happy path to Android development

@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)

Page 29: The happy path to Android development

@andrezzoid

Intents

Two types of Intents

Implicit Intent

Explicit Intent

Page 30: The happy path to Android development

@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);

}

Page 31: The happy path to Android development

@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);

}

Page 32: The happy path to Android development

@andrezzoid

Broadcast Receivers

Normally just called “Receiver”

Responds to broadcast events (yeah, really!)

Application or System events

The message is an Intent

Page 33: The happy path to Android development

@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!

Page 34: The happy path to Android development

@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

Page 35: The happy path to Android development

@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”

Page 36: The happy path to Android development

@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);

}

Page 37: The happy path to Android development

@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

Page 38: The happy path to Android development

@andrezzoid

Where to go next?

Async that shit

Loaders

AsyncTasks

Handlers

Database

Content Providers

View re-use

Fragments

Optimizations

Page 39: The happy path to Android development

@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

Page 40: The happy path to Android development

@andrezzoid

Contacts

http://andrejonas.me

[email protected]

@andrezzoid

Code:

http://github.com/andrezzoid/happy_path_android