android development - the basics, mff uk, 2013

73
Android Development - the basics Tomáš Kypta

Upload: tomas-kypta

Post on 12-May-2015

751 views

Category:

Technology


1 download

DESCRIPTION

Introductory lecture to Android development. Faculty of Mathematics and Physics in Prague, 2013.

TRANSCRIPT

Page 1: Android development - the basics, MFF UK, 2013

Android Development - the basics

Tomáš Kypta

Page 2: Android development - the basics, MFF UK, 2013

Agenda

• Android platform and ecosystem

• Android SDK and development tools

• Hello World

• building blocks of Android apps & the manifest file

• activities, widgets, intents

Page 3: Android development - the basics, MFF UK, 2013

Agenda

• dialogs, toasts, notifications

• fragments

Page 4: Android development - the basics, MFF UK, 2013

Android platform

• Linux-based operating system

• open-source (http://source.android.com/)

• originally phone OS

• tablet support (since Honeycomb, Android 3.0)

• Google TV

Page 5: Android development - the basics, MFF UK, 2013

History

• 2003, Android inc.

• 2005, acquired by Google

• Sep 2008, the first Android phone

• T-Mobile G1

• May 2010, Froyo (Android 2.2)

• Feb 2011, Honeycomb (Android 3.0)

Page 6: Android development - the basics, MFF UK, 2013

History

• Oct 2011, Ice Cream Sandwich (Android 4.0)

• July 2012, Jelly Bean (Android 4.1)

• July 2013, Jelly Bean (Android 4.3)

• Oct 2013, KitKat (Android 4.4)

Page 7: Android development - the basics, MFF UK, 2013

Platform Versions

Page 8: Android development - the basics, MFF UK, 2013

Android ecosystem

• thousands of devices

• the most popular mobile platform

• 1.5 million new devices activated every day

• September 3, 2013, 1 billion Android devices have been activated

• most devices made by Samsung

Page 9: Android development - the basics, MFF UK, 2013

Google Play

• app can be acquired by app stores

• Google Play, http://play.google.com

• other stores

• > 50 billion apps have been installed from Google Play

• July 2013, 1 million apps

Page 10: Android development - the basics, MFF UK, 2013

Google Play

• purchasing

• selling

• Play Music, Play Books, ...

Page 11: Android development - the basics, MFF UK, 2013

Google Play

• selling apps

• 15 min return period

• ads

• AdMob, ...

• in-app billing

Page 12: Android development - the basics, MFF UK, 2013

• fragmentation

• manufacturer/carrier enhancements

• updates & support

• openness - low quality apps in Google Play

• malware

• users

Android “problems”

Page 13: Android development - the basics, MFF UK, 2013

Android security

• app can be installed directly

• .apk file

• user accepts app permissions when installing or updating the app

Page 14: Android development - the basics, MFF UK, 2013

Android security

• Verify Apps (Android 2.3+)

• checks every app install

• Google Play can remotely uninstall harmful app

Page 15: Android development - the basics, MFF UK, 2013
Page 16: Android development - the basics, MFF UK, 2013
Page 17: Android development - the basics, MFF UK, 2013

Development

• programming in “Java”

• native apps possible (C++)

• development tools platform friendly

• Windows, Linux, Mac OS X

Page 18: Android development - the basics, MFF UK, 2013

Development

• IDE support

• Android Studio, IntelliJ IDEA, ADT plugin for Eclispse, Netbeans

• you can freely develop on any device

Page 19: Android development - the basics, MFF UK, 2013

Android SDK

• android - Android SDK and AVD Manager

• adb - Android Debug Bridge

• monitor - (ddms & hierarchyviewer)

• emulator

• lint, Traceview, ProGuard

• docs, samples

Page 20: Android development - the basics, MFF UK, 2013

Libraries

• compatibility libraries

• v4 - backports lots of newer functionality to Android 1.6+

• Google Play In-app Billing

Page 21: Android development - the basics, MFF UK, 2013

Libraries

• Google Play Services

• Google Maps

• Games

• Google+

• Authorization

Page 22: Android development - the basics, MFF UK, 2013

Libraries

• AdMob

• Google Analytics, Flurry, Crittercism

• Google Cloud Messaging

Page 23: Android development - the basics, MFF UK, 2013
Page 24: Android development - the basics, MFF UK, 2013

Hello World

Page 25: Android development - the basics, MFF UK, 2013

Build

Page 26: Android development - the basics, MFF UK, 2013
Page 27: Android development - the basics, MFF UK, 2013
Page 28: Android development - the basics, MFF UK, 2013

Android building blocks

• Activity

• Service

• Content provider

• Broadcast receiver

• AndroidManifest.xml

Page 29: Android development - the basics, MFF UK, 2013

Activity

• screen with user interface

• the only visual component

• example - an email app

• list of emails

• details of an email

• email composition

Page 30: Android development - the basics, MFF UK, 2013

Service

• has no UI

• long-running tasks

• examples

• music playback service

• download service

• sync service

Page 31: Android development - the basics, MFF UK, 2013

Content Provider

• managers and shares application data

• data storage doesn’t matter (db, web, filesystem)

• apps can query and modify data through content provider

• r/w permissions can be defined

• examples - all system dbs (SMS, contacts, ...)

Page 32: Android development - the basics, MFF UK, 2013

Broadcast Receiver

• responds to broadcasts

• broadcasts are system wide

• can be registered statically or dynamically

• system or custom messages

• examples - incoming SMS, incoming call, screen turned off, low baterry, removed SD card, BT device available, ...

Page 33: Android development - the basics, MFF UK, 2013

AndroidManifest.xml

• defines what parts the app have

• defines which endpoints are exposed

• minimum/maximum API level

• permissions

• declare hardware and software features

• require configuration

Page 34: Android development - the basics, MFF UK, 2013

Intent

• asynchronous message

• binds components together (all except Content Provider)

• starting activities

• starting services and binding to services

• sending broadcasts

Page 35: Android development - the basics, MFF UK, 2013

Activity

• a subclass of android.app.Activity

• app usually has many activities

• activities managed in activity stack

• newly started activity is placed on the top of the stack

Page 36: Android development - the basics, MFF UK, 2013

Activity Lifecycle

• activity can be in different states during its lifecycle

• foreground, visible, stopped, killed

• when activity state changes a system callback is called

Page 37: Android development - the basics, MFF UK, 2013

Activity callbacks

• onCreate() - activity created

• onStart() - activity visible for the user

• onResume() - activity gains user focus

• onPause() - system resuming another activity

• onStop() - activity becoming invisible to the user

Page 38: Android development - the basics, MFF UK, 2013

Activity callbacks

• onDestroy() - before activity is destroyed

• onRestart() - called if activity was previously stopped, called prior to onStart()

Page 39: Android development - the basics, MFF UK, 2013
Page 40: Android development - the basics, MFF UK, 2013
Page 41: Android development - the basics, MFF UK, 2013

Configuration changes

• when configuration changes, activities are destroyed and recreated

• default behaviour, can be changed

• properly handle config changes

• onSaveInstanceState(Bundle)

Page 42: Android development - the basics, MFF UK, 2013

Intent & Activity

• starting activity explicitly

• new Intent(context, MyActivity.class)!

• starting activity implicitly

• new Intent(Intent.ACTION_VIEW, Uri.parse(“http://developer.android.com”))!

• starting activity for result

Page 43: Android development - the basics, MFF UK, 2013

User Interface

• defined by a hierarchy of views

• layouts = containers

• LinearLayout, RelativeLayout, FrameLayout, ...

Page 44: Android development - the basics, MFF UK, 2013

User Interface

• widgets

• UI objects

• Button, TextView, EditText, RadioButton, ...

• WebView

Page 45: Android development - the basics, MFF UK, 2013

User Interface

• list widgets

• subclasses of AdapterView

• display a list of items

• use adapter to bind list do data

• ListView, GridView, Spinner, ...

Page 46: Android development - the basics, MFF UK, 2013

Resources

• drawables

• bitmaps

• 9-patch png

• state lists

• layer lists

• shape drawables

Page 47: Android development - the basics, MFF UK, 2013

Resources

• layout

• strings

• colors

• menus

• dimensions

• animations

Page 48: Android development - the basics, MFF UK, 2013

Resources

• arrays

• ids

• raw

• xml

• ...

Page 49: Android development - the basics, MFF UK, 2013

Screen sizes and densities

Page 50: Android development - the basics, MFF UK, 2013

Screen sizes and densities

• How to handle different screen sizes and densities?

Page 51: Android development - the basics, MFF UK, 2013

Resources

• resources can be created in several versions

• the best version is selected according to current device configuration in runtime

• resource units

• dp - density-independent pixel

• sp - scale-independent pixel (for fonts)

Page 52: Android development - the basics, MFF UK, 2013

Resource qualifiers

• suffixes for resource folders

• drawables, drawable-mdpi, ...

• values, values-cs

• layout, layout-sw640dp

• drawable-hdpi-v11

Page 53: Android development - the basics, MFF UK, 2013

Resource qualifiers

• screen density - ldpi, mdpi, hdpi, xhdpi, ...

• screen size - small, normal, large, xlarge

• screen orientation - port, land

• language - en, cs, sk, ...

• version - v11, v14, ...

Page 54: Android development - the basics, MFF UK, 2013

Resource qualifiers

• since Android 3.2

• w<N>dp - available screen width, w600dp

• h<N>dp - available screen heights, h720dp

• sw<N>dp - smallest width (does not change with orientation)

Page 55: Android development - the basics, MFF UK, 2013

Resources

• accessed from code via generated R.java file and resource ids

• view.findViewById(R.id.txt_name)!

• txtName.setText(R.string.txt_name_label)

Page 56: Android development - the basics, MFF UK, 2013

Android version fragmentation

• How to handle different API levels available on different devices?

Page 57: Android development - the basics, MFF UK, 2013

Android version fragmentation

• build target

• API level the app is compiled against

• AndroidManifest.xml

• <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" />

Page 58: Android development - the basics, MFF UK, 2013

Android version fragmentation

• handling versions in code if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {!

// code for Android < 2.3!

}

Page 59: Android development - the basics, MFF UK, 2013

Android version fragmentation

private boolean functionalitySupported = false;!

!static {!

try {!

checkFunctionalitySupported();!

} catch (NoClassDefFoundError e) {!

! functionalitySupported = false;!

}!

}!

!private static void checkFunctionalitySupported() throws ! ! !

! ! ! ! NoClassDefFoundError {!

!functionalitySupported = android.app.Fragment.class != null;!

}!

Page 60: Android development - the basics, MFF UK, 2013

Fragments

• a piece of application UI

• introduced to support more flexible UI

• phones and tablets together in one app

• fragment != activity

• fragments are used within activities

Page 61: Android development - the basics, MFF UK, 2013

Fragments

• since Android 3.0

• support library v4 backports them to Android 1.6+

Page 62: Android development - the basics, MFF UK, 2013

Threads

• main thread = UI thread

• do not ever block the UI thread!!!

• use worker threads for time consuming operations

• UI toolkit not thread safe - never manipulate UI from a worker thread

Page 63: Android development - the basics, MFF UK, 2013

Menu

• menu resource

• Android < 3.0 the whole menu hidden under menu button

• ActionBar since Android 3.0

• items can be displayed in the action bar

Page 64: Android development - the basics, MFF UK, 2013

Menu

• behaviour for items that don’t fit in the action bar

• hidden under menu button (if the device has one)

• system overflow icon in the action bar

Page 65: Android development - the basics, MFF UK, 2013

Dialogs

• floating window screen

Page 66: Android development - the basics, MFF UK, 2013

Dialogs

• standard dialogs

• custom dialogs

• activity with dialog style

• since fragments used via DialogFragment

Page 67: Android development - the basics, MFF UK, 2013

Dialogs

• might be tedious to create

• difficult to style

• StyledDialogs

• https://github.com/inmite/android-styled-dialogs

• the library makes styling and using dialogs a piece of cake

Page 68: Android development - the basics, MFF UK, 2013

Toast

• simple non-modal information

• displayed for a short period of time

• doesn’t have user focus

Page 69: Android development - the basics, MFF UK, 2013

Notifications

• a message that can be displayed to the user outside your normal UI

• displayed in notification area

Page 70: Android development - the basics, MFF UK, 2013

Notifications

• user can open notification drawer to see the details

• app can define UI and click action

• NotificationCompat.Builder

Page 71: Android development - the basics, MFF UK, 2013

PreferencesSharedPreferences prefs = PreferenceManager!

.getDefaultSharedPreferences(context);!

!SharedPreferences prefs = !

config.getSharedPreferences(PREFS_FILE_NAME,!

Activity.MODE_PRIVATE);!

!int storedValue = prefs.getInt(SOME_KEY, defaultValue);!

!SharedPreferences.Editor editor = prefs.edit();!

editor.putInt(SOME_KEY, storedValue);!

editor.commit();

Page 72: Android development - the basics, MFF UK, 2013

Sources

• developer.android.com

• android-developers.blogspot.com

• source.android.com

• stackoverflow.com

• youtube.com/androiddevelopers

• svetandroida.cz

Page 73: Android development - the basics, MFF UK, 2013

THE END