Development Android

quantityforeheadΚινητά – Ασύρματες Τεχνολογίες

10 Δεκ 2013 (πριν από 3 χρόνια και 6 μήνες)

88 εμφανίσεις

DEVELOPMENT
ANDROID
Jan Balata
1
Development Android
Topics
!
What is Android?

Architecture

Design Guidlines

Interaction
!
Development

IDE

Application Components
and Resources

Activity

Fragment

Data storage

Sensors
!
Accessibilty

Text to Speech

HID

Custom ROMs
2
Development Android
3
What is Android?
Development Android
What is Android?
!
Android – OS for mobile devices
5
Development Android
Architecture
!
Developed by Open Handset Alliance led by Google
!
Free under Apache Licence
!
Java libraries based on Apache Harmony
6
Applications
Application Framework
Libraries
Android Runtime
Linux Kernel
Development Android
Android Runtime
!
Core Java Libraries

APIs

Utilities,

File Access,

Network,

Graphics, ...
!
Dalvik Virtual Machine

Each app own process with own instance of Dalvik Virtual
Machine
!
Java compiled to Dalvik Executable format
7
Java Compiler
Dalvik Executable
.java
.class
.dex
Development Android
Libraries & Application Framework
!
C/C++ System libraries

Available through Application Frameworks

Media Libraries, Audio Manager, 3D Libraries, SGLite, ...
!
Application Framework

Enable and simplify reuse of components

View System – buttons, text boxes, web browser, ...

Resource Manager – non-code resources: audio, images, ...

Notification Manager – alerts in status bar, ...
8
Development Android
Make your Android apps look great
9
Development Android
Design Principles
Enchant Me
!
Enchant Me

Delight me in surprising ways

A beautiful surface, a carefully-placed animation,
or a well-timed sound effect is a joy to experience

Real objects are more fun than buttons
and menus

Allow people to directly touch and manipulate
objects

Reduces the cognitive effort needed to perform a
task while making it more emotionally satisfying

Let me make it mine

Get to know me

Learn peoples' preferences over time
10
Development Android
Design Principles
Simplify My Life
!
Simplify My Life

Keep it brief

Pictures are faster than words

Decide for me but let me have the final say

Take your best guess and act rather than asking first

Too many choices and decisions make people unhappy

Only show what I need when I need it

Hide options that aren't essential at the moment

I should always know where I am

Make places in your app look distinct and use
transitions to show relationships among screens

Provide feedback on tasks in progress

Never lose my stuff

If it looks the same, it should act the same

Only interrupt me if it's important
11
Development Android
Design Principles
Make Me Amazing
!
Make Me Amazing

Give me tricks that work everywhere

People feel great when they figure things out for themselves.

Make your app easier to learn by leveraging visual patterns
and muscle memory from other Android apps

It's not my fault

Be gentle in how you prompt people to make corrections

Sprinkle encouragement

Break complex tasks into smaller steps that can be easily
accomplished

Give feedback on actions, even if it's just a subtle glow

Do the heavy lifting for me

Make novices feel like experts by enabling them to do things
they never thought they could

Make important things fast

Not all actions are equal, decide what's most important in
your app and make it easy to find and fast to use
12
Development Android
Typography
!
Default type colors
!
Typographic Scale
13
Development Android
Color & Iconography
!
Colors

Use primarily for emphasis

Choose colors that fit with
your brand and provide
good contrast between
visual components

Red and green may be
indistinguishable to color-
blind users
!
Iconography

Launcher icons:
48x48 dp

Action bar icons:
32x32 dp

Small icons:
16x16 dp

Google Play:
512x512 px
14
More reading:

http://developer.android.com/design/style/iconography.html
Development Android
Notifications
!
New in Jelly Bean

Can include actions

Can be expanded to show additional
information details

Can have a priority flag
!
Make it personal

Time sensitive events

Involve other people
!
Correctly set and manage
notification priority
!
Use distinct icons

The user should be able to discern
what kinds of notifications are
currently pending

Don't
use color to distinguish your
app from others
15
More reading:

http://developer.android.com/design/patterns/notifications.html
Development Android
Building Blocks
16
More reading:

http://developer.android.com/design/building-blocks/index.html
Development Android
Gestures
17
Touch
Long press
Swipe
Drag
Double touch
Pinch open
Pinch close
Long press

Android 3+

Enters data
selection
mode

Avoid using
long press
for showing
contextual
menus
Development Android
Navigation
!
Up vs. Back

The
Up button
is used to navigate within an app
based

on the
hierarchical relationships
between screens

The system
Back button
is used to navigate,
in reverse
chronological order
, through the history of screens the
user has recently worked with
18
More reading:

http://developer.android.com/design/patterns/navigation.html
Development Android
Multi-pane Layouts
!
Combining Multiple Views Into One
!
Compound Views and Orientation Changes
19
From phone to tablet
Development Android
Devices and Display
!
Millions of phones, tablets, and other devices

Wide variety of screen sizes and form factors
!
Each device is falling into a particular size bucket
and density bucket

LDPI, MDPI, HDPI, and XHDPI

Design alternative layouts for some of the different size
buckets

Provide alternative bitmap images for different density
buckets
20
Development Android
Devices and Display
48dp Rhythm
!
Touchable UI components are generally laid out along 48dp units.

48dp translate to a physical size of about 9mm (with some variability).

In the range of recommended target sizes (7-10 mm) for touchscreen objects

Users will be able to reliably and accurately target them with their
fingers

Your targets will never be smaller than the minimum recommended target size of 7mm
regardless of what screen they are displayed on

You strike a good compromise between overall information density on the one hand, and
targetability of UI elements on the other
!
Mind the gaps

Spacing between each UI element is 8dp
21
Development Android
Devices and Display
!
Use RelativeLayout

Allows to specify layout in terms of the spacial relationships
between components not only linear
!
Use Size Qualifiers

Stretching the space within and around components, may not
provide the best user experience for each screen size

Application should not only implement flexible layouts, but should
also provide several alternative layouts to target different screen
configurations
!
Use Density-independent Pixels

Defining layout dimensions with pixels is a problem because
different screens have different pixel densities

Always use either
dp
or
sp
units
!
Provide Alternative Bitmaps
22
More reading:

http://developer.android.com/training/multiscreen/index.html
Development Android
Pure Android
!
Keep in mind that different platforms play by different
rules and conventions

Don't
mimic UI elements from other platforms

Don't
carry over platform-specific icons

Don't
use bottom tab bars

Don't
hardcode links to other apps

Don't
use labeled back buttons on action bars

Don't
use right-pointing carets on line items
!
Device Independence

Remember that your app will run on a wide variety of
different screen sizes
23
Development Android
Development
!
Eclipse IDE

Full Java IDE,

Graphical UI Builders,

Develop on Hardware Devices,

Develop on Virtual Devices,

Powerful Debugging, ...
!
Language

Apps usually developed in the
Java

Parts of app using native-code languages such as C and C++

Generally does not result in a noticable performance improvement, but it
always increases your app complexity

Signal processing, physics simulation, ...

Or HTML5
24
Development Android
Instalation
!
JDK 6


JRE alone is not sufficient
!
Android SDK

developer.android.com
+ tools, guidlines, documentation, tutorials
!
IDE – Eclipse 3.6.2 or greater

Eclipse ADT Plugin

Help > Install New Software

Add, Repository
https://dl-ssl.google.com/android/eclipse/

Restart Eclipse
!
Android Platforms and Android Virtual Devices

Download packages
25
Development Android
Distribution
Versions
!
Relative number of active devices running a given
version of the Android platform
26
Development Android
Distribution
Screens
!
Data about the relative number of active devices that
have a particular screen configuration

Defined by a combination of screen size and density
27
October 1, 2012
Development Android
Application Fundamentals
!
Multi-user Linux system

Each application is a different user
!
Each process has its own virtual machine (VM)

Application's code runs in isolation from other applications
!
Every application runs in its own Linux process

Android starts the process when any of the application's
components need to be executed

Then shuts down the process

When it's no longer needed or

When the system must recover memory for other applications
28
Development Android
Application Components
!
Activites

A single screen with a user interface

Each one is independent of the others

Different application can start any one of these activities
!
Services

Component that runs in the background to perform long-running operations or to
perform work for remote processes

Does not provide a user interface
!
Content Providers

Manages a shared set of application data

Can store the data in the file system, an SQLite database, on the web, or any other
persistent storage location application can access
!
Broadcast Recievers

Component that responds to system-wide broadcast announcements

Can create a status bar notification to alert the user when a broadcast event occurs

A "gateway" to other components and is intended to do a very minimal amount of
work
29
Development Android
Application Components
Activation
!
Activated by an asynchronous message – intent

Activities, services, and broadcast receivers
!
Intent

“The messengers that request an action from other
components”
!
For broadcast receivers intent simply the
announcement being broadcast
!
Content provider not activated by intents

Activated when targeted by a request from a
ContentResolver

Handles all direct transactions with the content provider
30
Development Android
The Manifest File
!
The system must know that the component exists

Reads the application's AndroidManifest.xml file

Application must declare all its components
!
What the manifest does?

Identify any user permissions

Declare the minimum API Level

Declare hardware and software features

API libraries the application needs (e.g. Google Maps)

And more
31
More reading:

http://developer.android.com/guide/components/fundamentals.html
Development Android
The Manifest File
Declaring components
!
Declaring component

<application>
identifies
application

<activity>
for activities

name and label

<service>
for services

<reciever>
for recievers

<provider>
for providers
!
Component capabilities

<intent-filter>

In order to respond to
intents from other
applications
!
Application
requirements

<supports-screens>


<uses-configuration>

<uses-feature>

<uses-sdk>

<uses-permission>
!
Store uses declarations
to filter applications for
particular devices
32
Development Android
The Manifest File
Permissions
!
Using Permissions

<uses-permission>
in manifest

Wifi or GPS locations, SMS, Vibrations, Internet, Call phone, Wifi and
network state, Boot reciever, Getting accounts

And More
!
Permissions requested by the application are
granted to it by the package installer
!
Application was either granted a particular
permission when installed or the permission was not
granted

Any attempt to use the feature will fail without prompting
the user
33
Development Android
Resources
!
Providing

You should place each type of resource in a specific
subdirectory of your project's
res/
directory.

Almost every application should provide alternative
resources to support specific device configurations

Different screen densities and alternative string resources for different
languages
!
Accessing

Referencing by its resource ID

Resource IDs are defined in project's
R
class

In code
R.string.hello

In XML @string/hello

imageView.setImageResource(R.drawable.myimage);
34
More reading:

http://developer.android.com/guide/topics/resources/overview.html
Development Android
Activity
!
Implementing your activity lifecycle methods properly
ensures your app behaves well in several ways,
including that it:

Does not crash if the user receives a phone call or
switches to another app while using your app

Does not consume valuable system resources when the
user is not actively using it

Does not lose the user's progress if they leave your app
and return to it at a later time

Does not crash or lose the user's progress when the
screen rotates between landscape and portrait orientation
35
Development Android
Activity
Lifecycle
36
Development Android
Activity
Creating, Resuming and Pausing
!
onCreate()

Declaring the user interface (defined in an XML layout file),
defining member variables, and configuring some of the UI
!
onPause()

Partialy obscured (e.g. by dialog)

Stop animations

Commit unsaved changes (such as a draft email).

Release system resources, such as broadcast receivers,
handles to sensors, or any resources that may affect battery life
!
onResume()

System every time activity comes into the foreground, including
when it's created for the first time

Initialize components that you release during onPause()
37
Development Android
Activity
Stoping, Restarting and Destroying
!
onStop()

Not visible at all

Perform larger, more CPU intensive shut-down operations, such as
writing information to a database

Should release almost all resources that aren't needed while the user is
not using it

System might destroy the instance if it needs to recover system memory

In extreme cases, the system might simply kill your app process without calling the
activity's final onDestroy() callback, so it's important you use onStop() to release
resources that might leak memory
!
OnStart()

Counterpart to the onStop()

System calls onStart() both when it creates your activity and when it
restarts the activity from the stopped state
!
onDestroy()

Last chance to release system resources to prevent memory leaks
38
Development Android
Restart & Configuration Change
!
If the rientation of the device is changed the
activity restarts
!
For key-value pairs

Override
onSaveInstanceState()
method

Restore onCreate from Bundle savedInstanceState
!
For large data

Override
onRetainNonConfigurationInstance()
method to return an object
carrying your data

Restore data when activity starts again with
getLastNonConfigurationInstance()
!
You can hadle orientation yourself

Define in manifest <activity>

android:configChanges="orientation|keyboardHidden"

And override
onConfigurationChanged
or do not if you don’t want the
orientation changed

Application should always be able to shutdown and restart with its previous
state intact

Don’t
consider this technique an escape from retaining state during lifecycle
39
Development Android
Fragment
!
Represents a behavior or a portion of user interface
in Activity
!
Combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in
multiple activities
!
Support tablets and smart phones in one application
with flexible UI
!
Introduced in Android 3.0

With Support Library can be used from Android 1.6
40
More reading:

http://developer.android.com/guide/components/fragments.html
Development Android
Fragment
Lifecycle
!
Creating Fragment

similar to Activity

onCreate()

Essential components to retain when the fragment is
paused or stopped and resumed

onCreateView()

Returns View of the fragment

onPause()

Commit any changed that should be persistent
!
Create interface for communication with
Activity
!
Deliver message from Activity to fragment

If it exist (two pane layout), else you have to
replace current fragment with another
41
Development Android
Saving Data
!
Shared Preferences

Store private primitive data in key-value pairs
!
Internal Storage

Store private data on the device memory
!
External Storage

Store public data on the shared external storage
!
SQLite Databases

Store structured data in a private database
!
Network Connection

Store data on the web with your own network server
42
More reading:

http://developer.android.com/guide/topics/data/data-storage.html
Development Android
Workers
!
Slow, blocking operations should be done in a new
Thread

Avoid slowing down the user interface

Avoid infamous "application not responding" dialog

Do not access the Android UI toolkit from outside the UI thread, instead
use one of following methods

Activity.runOnUiThread(Runnable)

View.post(Runnable)

View.postDelayed(Runnable, long)
!
Better using AsyncTask

Performs the blocking operations

doInBackground() and you can publishProgress()

Publishes the results on the UI thread

onPreExecute(), onPostExecute(), onProgressUpdate()
!
Loops using Handler

schedule runnable to be executed and schedule new one inside
runnable
43
More reading:

http://developer.android.com/guide/components/processes-and-threads.html
Development Android
Location & Sensors
!
Location and Maps

LocationManager

persmissions, battery drain

External Google Maps Library

Challenges

Multitude of location sources

User movement

Varying accuracy

Typical flow of procedures for obtaining the user location:

Start application

Sometime later, start listening for updates from desired location providers

Maintain a "current best estimate" of location by filtering out new, but less
accurate fixes

Stop listening for location updates

Take advantage of the last best location estimate
44
More reading:

http://developer.android.com/guide/topics/sensors/index.html
Development Android
Location & Sensors
Overview
!
All sensors return multi-dimensional array of values
for each SensorEvent except environment sensors
!
Motion sensors

accelerometer, gravity, gyroscope, linear acceleration,
rotation vector
!
Position sensors

magnetic field, orientation (deprecated in Android 2.2),
proximity
!
Environment sensors

ambient temperature (air), light, pressure, relative
humidity, temperature (device)
45
Development Android
Text to Speech
!
Screen reader support through whole Android
!
Czech language installation

Google TalkBack app

SVOX Classic app for external language support

SVOX Czech app with Czech Language
!
Activity implements
TextToSpeech.OnInitListener

Override
onInit
method

Instantiate with context and listener

and speek

myTTS.speak(“Hello World”, TextToSpeech.QUEUE_FLUSH, null);
46
Development Android
HID
!
Human Interface Device Protocol

One of the posibilities for visually impaired

External numeric keybord

Supported only by some devices

But you can use custom ROM
47
Development Android
Custom ROMs
!
Cyanogenmod

Aftermarket firmware for a number of cell phones based
on the open-source Android operating system

It offers features not found in the official Android based
firmwares of vendors of these cell phones
!
More at XDA Developers forums

http://www.xda-developers.com
!
You can loose warranty
48
Development Android
Resources
!
Developer Android

http://developer.android.com

Documentation, Guidlines, Tutorials, Sample Code

That’s all you need
!
Stack Overflow

http://stackoverflow.com

Tons of solved problems
49
Development Android
50
Thank for your attention!
Jan Balata, 9.10.2012