androidpres-131017065017-phpapp02
TRANSCRIPT
-
8/13/2019 androidpres-131017065017-phpapp02
1/62
Introduction to Android
Development
Owain Lewis
[email protected]@Ow_Zone
mailto:[email protected]:[email protected] -
8/13/2019 androidpres-131017065017-phpapp02
2/62
Goals of this session
Give an overview of the Android Ecosystem
Explain the core components of an Android
App
Drink beer
Create a simple working Android Application
Drink more beer
-
8/13/2019 androidpres-131017065017-phpapp02
3/62
Order of Service
Part 1:
Introduction to Android Development (45mins)
Beer Break
Part 2:
Live Coding(45mins - or until it compiles)
Socialise
-
8/13/2019 androidpres-131017065017-phpapp02
4/62
Android VM != JVM
Android Apps are:
Written in java (mostly)
Converted to .dex files
Compiled to bytecode
Run on the Dalvik VM
-
8/13/2019 androidpres-131017065017-phpapp02
5/62
-
8/13/2019 androidpres-131017065017-phpapp02
6/62
Whats inside an App
Compiled Java source code
Library dependencies e.g. MIME type lib
XML FilesUI layouts, fragments etc. Images, sounds, videos etc.
AndroidManifest.xml
-
8/13/2019 androidpres-131017065017-phpapp02
7/62
Application Security
Each App is assigned a user in Android linux Each linux process has its own VM
Each Application runs in 1 process.
Applications can request permissions to
system resources etc. - but user must agree.
-
8/13/2019 androidpres-131017065017-phpapp02
8/62
App Fundamentals: UI
-
8/13/2019 androidpres-131017065017-phpapp02
9/62
App Fundamentals: UI
-
8/13/2019 androidpres-131017065017-phpapp02
10/62
UI Design... Wrong talk
-
8/13/2019 androidpres-131017065017-phpapp02
11/62
Application Resources
Additional files and static content that your
code uses
Layouts for different types of devices and
device orientations.
Images for different screen sizes and densities
Referenced by unique ID within code
Allow easy localization
-
8/13/2019 androidpres-131017065017-phpapp02
12/62
Application Components
-
8/13/2019 androidpres-131017065017-phpapp02
13/62
ApplicationManifest.xml
Defines application components
Identifies required permissions
Defines required API level Definers hardware/software features required
API libs to link against (e.g. maps API)
Components not in the manifest arent usable
-
8/13/2019 androidpres-131017065017-phpapp02
14/62
Application Components
-
8/13/2019 androidpres-131017065017-phpapp02
15/62
Intro to Activities
Single screen with UI
Multiple activities exist in same app
Activities work together but each is standalone
Any app can trigger any activity in any app (if
allowed)
More on that later
-
8/13/2019 androidpres-131017065017-phpapp02
16/62
Intro to Services
Services generally run in the background
Perform work that a user isnt aware of
Multiple components can make use of arunning service
Example: GPS service, mp3 playing
-
8/13/2019 androidpres-131017065017-phpapp02
17/62
Intro to Content Providers
Mechanism to allow apps to interact with data
Create, Query, Store, Update
Can store data in multiple ways File System, DB,network storage, or anywhere else the App can
get to.
Apps can query any Content Provider if they havepermission
-
8/13/2019 androidpres-131017065017-phpapp02
18/62
Intro to Broadcast Receivers
A listener for system wide events
No UI component
Tend to be lightweight and do little work Example: Low battery handler
-
8/13/2019 androidpres-131017065017-phpapp02
19/62
Intro to Intents
Intent binds components to each other at
runtime (e.g. Event mechanism)
Explicit Intent = message to activate specific
component by name
Implicit Intent = message to activate type of
component
Components define which Intent Types they
pick up with Intent Filters
-
8/13/2019 androidpres-131017065017-phpapp02
20/62
Component Interoperability
Any app can start any other apps component e.g.
use the camera app activity to capture pictures
Don't need that code in your app
Components run in the process of the holder app (e.g.camera take pic activity runs under camera process).
Android apps don't have single entry point (main
method)
Activating a component in another app is not
allowed by system permissions - so use Intent.
-
8/13/2019 androidpres-131017065017-phpapp02
21/62
Activating Components
Activity: pass Intent to startActivity()
startActivityForResult()- gets a result
Service: pass Intent to startService()or
bindService()if running already
Broadcast: pass Intent to sendBroadcast(),
sendOrderedBroadcast()sendStickyBroadcast()
Content: call query()on a ContentResolver
-
8/13/2019 androidpres-131017065017-phpapp02
22/62
Make sense so far?
-
8/13/2019 androidpres-131017065017-phpapp02
23/62
-
8/13/2019 androidpres-131017065017-phpapp02
24/62
ActivitiesStates
Resumed/Running: up front and has focus
Paused: Another Activity is on top of this one -
but this is still visible.
Stopped: Backgrounded - still alive, not
attached to window manager
The OS can kill activities without focusbut
will restart if possible.
-
8/13/2019 androidpres-131017065017-phpapp02
25/62
Activities - Lifecycle
-
8/13/2019 androidpres-131017065017-phpapp02
26/62
Activities
Must be declared in Manifest
Can declare IntentFiltershow other apps can
use the activity
E.g. Action=MAIN and Category=LAUNCHER
tells Android this can be launched.
-
8/13/2019 androidpres-131017065017-phpapp02
27/62
Activities - Manifest
-
8/13/2019 androidpres-131017065017-phpapp02
28/62
ActivitiesStarting
Explicit Intent to start Activity:Intent intent = new Intent(this, SignInActivity.class);
startActivity(intent);
Implicit Intent to start Activity:Intent intent = new Intent(Intent.ACTION_SEND);intent.putExtra(Intent.EXTRA_EMAIL, recipientArray);
startActivity(intent);
Need a result? Call startActivityForResult()then implement onActivityResult()callback
Get the result in an Intent
-
8/13/2019 androidpres-131017065017-phpapp02
29/62
-
8/13/2019 androidpres-131017065017-phpapp02
30/62
Activities - Advanced
Fragments
Sub Activity with own lifecycle
Can be an invisible worker
Loaders (3.0 +)
Async pull data into Activity Views
-
8/13/2019 androidpres-131017065017-phpapp02
31/62
Services
Long running operations - no UI. e.g. play music
Service can run forever, but should know how tostop itself
Started service = runs in background - does it'sown thing until it stops
Bound service = allows interaction/feedback.Destroyed when all components unbind.
Determine type of Activity by implementingcallbacks
-
8/13/2019 androidpres-131017065017-phpapp02
32/62
Services - Callbacks
onCreate()
onStartCommand() onBind()
which returns IBinder = the defined interface for
comms.
onDestroy()
-
8/13/2019 androidpres-131017065017-phpapp02
33/62
ServicesStart/Stop
startService(Intent)
stopSelf()
bindService (Intentservice, ServiceConnectionconn, int flags)
System destroys bound servicewhen clientsdisconnect.
Note that system can kill service without warning - butwill restart it
http://developer.android.com/reference/android/content/Intent.htmlhttp://developer.android.com/reference/android/content/ServiceConnection.htmlhttp://developer.android.com/reference/android/content/ServiceConnection.htmlhttp://developer.android.com/reference/android/content/Intent.html -
8/13/2019 androidpres-131017065017-phpapp02
34/62
-
8/13/2019 androidpres-131017065017-phpapp02
35/62
Services - Manifest
Must declare all services in your
application's manifest file.
Supports IntentFilters and permissions.
-
8/13/2019 androidpres-131017065017-phpapp02
36/62
Services - Threads
Services use the Main UI threadthis is bad.
Your service should create threads it needs to
handle work
If you want to handle multiple thread
simultaneously you need to manage this here.
You need to manage stopping the thread too
Alternative is to extend IntentServiceoffersworker per requestone at a time.
-
8/13/2019 androidpres-131017065017-phpapp02
37/62
Services - IntentService
Creates a default worker thread that executes all intentsdelivered to onStartCommand() separate from yourapplication's main thread.
Creates a work queue that passes one intent at a time toyour onHandleIntent() implementation, so you never haveto worry about multi-threading.
Stops the service after all start requests have beenhandled, so you never have to call stopSelf().
Provides default implementation of onBind() that returns
null. Provides a default implementation of onStartCommand()that sends the intent to the work queue and then to youronHandleIntent() implementation.
-
8/13/2019 androidpres-131017065017-phpapp02
38/62
-
8/13/2019 androidpres-131017065017-phpapp02
39/62
ServicesUser Comms
Services can communicate with the user in a limitedway.
Once running, a service can notify the user of eventsusing Toast Notificationsor Status Bar Notifications.
Toast notification is a message that appears on thesurface of the current window for a moment thendisappears.
Status bar notification provides an icon in the statusbar with a message, which the user can select in orderto take an action (such as start an activity).
http://developer.android.com/guide/topics/ui/notifiers/toasts.htmlhttp://developer.android.com/guide/topics/ui/notifiers/notifications.htmlhttp://developer.android.com/guide/topics/ui/notifiers/notifications.htmlhttp://developer.android.com/guide/topics/ui/notifiers/toasts.html -
8/13/2019 androidpres-131017065017-phpapp02
40/62
Services- Advanced
Messengers
AIDL
-
8/13/2019 androidpres-131017065017-phpapp02
41/62
Broadcast Receivers
Component that responds to system-wide
broadcast announcements
Many broadcasts originate from the system
Register for intents via Manifest
Meant to do very little work
-
8/13/2019 androidpres-131017065017-phpapp02
42/62
Broadcast Receivers
Receiving a broadcast involves:
Registering receiver in Manifest or dynamically
Implementing: onReceive(Context, Intent)
-
8/13/2019 androidpres-131017065017-phpapp02
43/62
Broadcast Receivers
Normal broadcasts: Context.sendBroadcast()
Asynchronous.
All receivers of the broadcast are run in an undefinedorder, often at the same time.
Ordered broadcasts:Context.sendOrderedBroadcast()
Delivered to one receiver at a time.
Each receiver executes in turn, and can propagate aresult to the next receiver, or abort the broadcast sothat it won't be passed to other receivers. Similar toFilters in JEE
-
8/13/2019 androidpres-131017065017-phpapp02
44/62
Content Providers
Manage secure access to data - across process
boundaries
Don't need one if you're just providing data to
own app
Use ContentResolver to access data in a
ContentPtorvider
-Android includes ContentProviders forcontacts, audio, video, images etc
-
8/13/2019 androidpres-131017065017-phpapp02
45/62
Content Providers
Data can be file (video etc) or Structured (tabular)
Data is provided as tables (similar to relational dbs)with primary keys (if needed)
Content is referenced using a URI scheme
e.g. content://user_dictionary/words
Need to ask for permission to use a provider - this is donein the manifest
Define a query to perform against the URI.
Returns a Cursor
Cursor can be wrapped in a CursorAdaptor and bound to aUI element.
-
8/13/2019 androidpres-131017065017-phpapp02
46/62
Content Providers - Query
-
8/13/2019 androidpres-131017065017-phpapp02
47/62
Content Providers
Also can insert, update and delete.
Batch access = process lots of data at a time
Contract Classes = Constants used in accessing
a ContentProvider.
-
8/13/2019 androidpres-131017065017-phpapp02
48/62
Content Providers
-
8/13/2019 androidpres-131017065017-phpapp02
49/62
Intent and IntentFilter
Activities, Services and Broadcast receivers are activated throughIntents
Intent is a passive bundle of information. OS figures out how to getit to the correct place.
Intent is:
Optional component name Action (e.g. ACTION_BATTERY_LOW) - you can invent your own
Data - URI of data and MIME type
Category - type of Intent (e.g. CATEGORY_LAUNCHER)
Extras - key/value pairs
Flags - how to deal with the intent
Explicit Intent = named
Implicit Intent = category of intents
-
8/13/2019 androidpres-131017065017-phpapp02
50/62
Intent and IntentFilter
IntentFilter tells the system which typeof
Intent a component can handle
Component can specify multiple filters
Filter has fields that parallel the action, data,
and category fields of an Intent object
Intent must match all 3 fields to be delivered
Note that Intent may not specify a field
-
8/13/2019 androidpres-131017065017-phpapp02
51/62
IntentFilter Matching
Action: Intent action must match at least one Filteraction. Intent can leave action blank and match
Category: Every category in the Intent object mustmatch a category in the filter. Filter can list additional
categories, but it cannot omit any that are in theintent.
android.intent.category.DEFAULT must be implimented
Data:
No data = match
More complex rules for data matching if specified
-
8/13/2019 androidpres-131017065017-phpapp02
52/62
IntentFilter Matching
Multiple filter match: Ask the user
-
8/13/2019 androidpres-131017065017-phpapp02
53/62
Intent and IntentFilter
-
8/13/2019 androidpres-131017065017-phpapp02
54/62
-
8/13/2019 androidpres-131017065017-phpapp02
55/62
Processes and Threads
-
8/13/2019 androidpres-131017065017-phpapp02
56/62
AsyncTask
AsyncTask allows you to perform asynchronouswork on your user interface. It performs theblocking operations in a worker thread and thenpublishes the results on the UI thread, withoutrequiring you to handle threads and/or handlersyourself.
Subclass AsyncTask and implement:
doInBackground() onPreExecute, onPostExecute(), onProgressUpdate()
Run Task from UIThread by calling execute()
-
8/13/2019 androidpres-131017065017-phpapp02
57/62
AsyncTask
-
8/13/2019 androidpres-131017065017-phpapp02
58/62
Development Tools
Android Platform + SDK
Android Developer Tools (ADT) for Eclipse
Android Studio - Based on IntelliJ IDEA
Currently in Early Access release
Uses Gradle
Emulators and Hardware debugging
-
8/13/2019 androidpres-131017065017-phpapp02
59/62
Questions?
-
8/13/2019 androidpres-131017065017-phpapp02
60/62
Beer!
-
8/13/2019 androidpres-131017065017-phpapp02
61/62
Live Coding
-
8/13/2019 androidpres-131017065017-phpapp02
62/62
Introduction to Android
DevelopmentOwain Lewis
[email protected]@Ow_Zone
mailto:[email protected]:[email protected]