COMP 5047 Pervasive Computing

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

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

76 εμφανίσεις

COMP 5047 Pervasive Computing

Week 6: Smartphones & the Android OS

Dr.
-
Ing. Rainer Wasinger

School of Information Technologies

Part 1: Web vs. Native Approaches


The first age of the Web:


Netscape vs. Microsoft and aggressive extension of HTML, with Microsoft claiming
96% of the browser market by 2002.


The second age of the Web:


Minimal improvements. Introduction of IE6 and

a long period of stagnation.


Finally, Safari arrives in 2003 and Firefox in 2004.


The third age of the Web (the present):


Native apps have received most attention. The

Apple AppStore solved two large problems:


Trusted device access and


Micro
-
payments.


… but the web ended up 10 to 20 times bigger in

the process and is:


Usable on any device, is inherently searchable, and

composable with a huge selection of mashup services.

2

Web vs Native Approaches


The Web:

(+): It is open; extensible; currently has some strong supporters (e.g. Apple, Google); is
very broadly used by all users.

(
-
): Limited in functionality (w.r.t. access to device
-
specific features); has slightly
different methods for interaction; typically has lesser performance.


Native:

(+): Access to underlying device APIs gives flexibility to customise functionality and
features; the latest APIs are always available to native first; performance, e.g. w.r.t.
direct access to the GPU for games.

(
-
): Sooooooo hard to develop for all mobile operating systems and then maintain the
applications as the operating systems continue to evolve.


The Hybrid approach:

Utilise web technologies for content and use native approaches only where required.

3

Smartphone Development

Statistics: Application Store Fronts


Android Google Play: 600,000+ applications

Apple App Store: 650,000+ applications


(as at June.2012)

Part 2: Android OS


6

Android OS

7


Android OS:


An open source mobile platform based on the Linux operating
system.


Applications are developed using the Java programming language.


Applications run on Dalvik, a custom virtual machine designed for
embedded use, which runs on top of a Linux kernel.


Although Android reuses the Java language syntax, it does not
provide the full
-
class libraries and APIs bundled with Java SE or
Java ME, using the Harmony Java implementation instead (due to
Apache’s license allowing for closed source derived works).


The latest Android OS release for smartphones is 4.1 (API Level
16).

Android OS


Android, License:


Most of the Android code has been released under the Apache License.


Although Android is an open
-
source product, some development has been
continuing under private development branches.


The Apache License allows vendors to add proprietary extensions without
submitting those back to the open source community.


Android, Origins:


Developed originally by Google, and later/now by the Open
Handset Alliance.


Announced on 05.November.2007, together with the founding of
the Open Handset Alliance (OHA).


The OHA:


A consortium of 84 firms (hardware, software, and telecoms)
devoted to advancing open standards for mobile devices.


Companies include: Google, HTC, Intel, Motorola, Qualcomm, Texas
Instruments, Samsung, LG, T
-
Mobile.

8

The Android Stack


The Android platform is a software stack for mobile devices including:


An operating

system


Middleware


Key

applications


9

Android OS Features

10

Android Development


Required software for developing for the Android
platform (for Windows XP/Vista/7, Mac OS X
10.5.8 and later, and Linux):



JDK 6 (JRE alone is not sufficient)


Eclipse v3.6.2 Classic or later


http://www.eclipse.org/downloads/


Android SDK Starter Package v20.0.3


http://developer.android.com/sdk/installing/index.html


Android Development Tools (ADT) v20.0.3 plugin for Eclipse


http://developer.android.com/tools/sdk/eclipse
-
adt.html

11

Android Developer Resources

12


A good book on Android: The Busy Coder’s Guide
to Android Development, by Mark Murphy.


URL: http://commonsware.com/Android/Android_3
-
4
-
CC.pdf


Topic: Android 2.3 SDK


Length: 596 pages long


Price: Free (as part of the Four
-
to
-
Free

model).

Android Developer Resources


URL:
http://developer.android.com/about/index.html



13

Android Developer Resources

14

Android Developer Resources

15

Android Developer Resources

16

Android Developer Resources

17


API Guides:

Android Developer Resources


Reference:

18

Some Quick Questions


Who hasn’t used the Eclipse IDE before?



Who isn’t familiar with the Java programming
language?



Who isn’t familiar with XML?



Has anyone programmed for Android before?



How many here have smartphones?


19

Part 3: Eclipse


Using the IDE


20

Using the Eclipse IDE


Editing code

21

Using the Eclipse IDE

22


Designing a

GUI

Using the Eclipse IDE

23


Using the Debugger

Using the Eclipse IDE

24


LogCat

***Lab Exercises***


In this lab, you will learn the following:

1.
Creation of a HelloWorld Android project and the
running of the application in the emulator.

2.
Familiarisation with the Eclipse IDE.

3.
Familiarisation with the Android Device Emulator
(ADE).

4.
Importing the sample COMP5047 Property Application
project into Eclipse, in preparation for the next lab’s
GUI design and Activity & Intent coding exercises.

5.
Running other code samples that come with the
Android SDK.

25

Part 4: Example Mobile Applications

26


Multi
-
Channel Content Repurposing & Mobile
Personalisation Project

Example Mobile Applications

Property Organiser (Android)

Content Repurposing (Volantis)

Museum Tour (iPhone)

Dangerous Australians (iPhone)

Long
-
term Career Management (Android)

Neighbourhood Networks (Android)

Sydney Dashboard (mobile Web)

PersonisJ User Modelling across Multiple Mobile Apps (Android)

Part 5: Anatomy of an Android Application


Android applications don’t have a single entry point for
everything in the application (i.e. no main() function).
Instead, they have essential components that the system
can instantiate and run as needed.


This is partly to make the applications more crash
-
resistant.


There are 4 types of components:


Activities


Services


Content providers


Broadcast receivers


We will focus on the Activity component.

28

Android Components


Services:


Services run in the background to perform long
-
running operations. They
do not provide a visual user interface.


E.g. a service might play background music, or might fetch data over the
network without blocking user interaction with an activity.


Content providers:


Content providers make a specific set of an application’s data available to
other applications. Such data can for example be stored in the file system,
or in an SQLite database.


E.g. the Android system uses a content provider to manage the user’s
contact information.


Broadcast receivers:


These are components that do nothing but receive and react to system
-
wide broadcast announcements.


E.g. an announcement might be that the time zone has changed, that the
battery is low, or that a picture has been taken.

29

Android Components (cont.)


Activities:


Activities represent the building block of the user
interface.


An activity presents a visual user interface for one
focused endeavour that the user can undertake.


E.g. an activity might present a list of contacts to send
messages to, while other activities might deal with the
writing of messages, the viewing of old messages, and
the changing of settings.


Each activity is independent of the others, and each one
is implemented as a subclass of the Activity base class.

30

Tasks and the Activity Stack


A ‘task’ is what the user experiences as an ‘application’. It is a
group of related activities, arranged in a stack.


The root activity in the stack is the one that began the task, e.g.
the activity the user selected in the application launcher.


One activity can start another activity. When this happens, the
new activity is pushed onto the stack and it becomes the running
activity.


When the user presses the back key, the current activity is
popped from the stack, and the previous one resumes as the
running activity.


Starting a task: An activity is set up as the entry point for a task
by giving it an intent filter with “android.intent.action.MAIN” as
the specified action and “android.intent.category.LAUNCHER”
as the specified category.


31

The Lifecycle of an Activity


The main lifecycle states:


Resumed
: The activity is in the foreground of the screen and users can
interact with it. Also referred to as the “running” state.


Paused
: The activity is partially obscured by another activity and will not
receive user input during this time and cannot execute any code.


Stopped
: The activity is completely hidden by other activities that have
been launched; it is considered to be in the background. While stopped, the
activity instance and state information is retained, but it cannot execute any
code.

32

The Lifecycle of an Activity (cont.)


onCreate
() and
onDestroy
():


The entire lifetime of an Activity happens between the first call to
onCreate
() through to a single final call to
onDestroy
().


An Activity does all of its initial setup in
onCreate
() and releases all
remaining resources by the time
onDestroy
() is called.



onStart
(),
onRestart
(), and
onStop
():


The visible lifetime of an Activity happens between a call to
onStart
() until a corresponding call to
onStop
().


onStart
() is called when an activity is first launched.


onRestart
() is called when an activity is brought back to the
foreground after having been hidden.


onStop
() is called when the activity is about to be stopped.

33

The AndroidManifest.xml File


Before Android can start an application, it must learn that
the component exists. Applications declare their
components in a manifest file that is bundled into the
Android package, i.e. the .apk file that holds the
application’s code, files, and resources.


The manifest is a structured XML file and is always named
AndroidManifest.xml for all applications.


In addition to declaring the application’s components, it
also identifies any permissions the application expects to
be granted and the minimum level of the Android API that
the application requires.

34

Intents


While one Activity will be special in that it is used
to start an application, the other activities also
need to be reached. This is done through the use
of asynchronous messages called Intents.


An Intent is an object that holds the content of a
message. Intents can be very specific (e.g.
requesting a specific Activity to be launched) or
can be more generic (e.g. when multiple criteria is
needed for selecting an Activity to launch).


Activities are launched by passing an Intent object
to the Context.startActivity() method.

35

Intent Filters


An Intent object can explicitly name a target component. If
it does, Android finds that component and activates it. If a
target is not explicitly named, Android must locate the best
component to respond to the intent.


A component can have any number of intent filters, each
one declaring a different set of capabilities.


If a component doesn’t have any filters, it can be activated
only by intents that explicitly name the component as the
target.


36

Designing the User Interface


User interfaces are built using View and ViewGroup objects.


The View class serves as the base for subclasses called “widgets”,
which offer fully implemented UI objects, like text fields and buttons.


The ViewGroup class serves as the base for subclasses called
“layouts”, which offer different kinds of layout architecture, like linear,
tabular, and relative.



To attach a view hierarchy tree to the

screen for rendering, an Activity must

call the setContentView() method

and pass a reference to the root node

object.


37

View Hierarchy

Designing the User Interface (cont.)


The most common way to define a layout and express the
view hierarchy is with an XML layout file.


Each element in XML is either a View or ViewGroup object.


View objects are the leaves in the tree.


ViewGroup objects are the branches in the tree.


Combining different Views in a nested XML structure is the
basis for GUI creation.

38

Part 6: The Code


39

Notepad Example
-

The Eclipse IDE

40

Notepadv3 Example


The Manifest

<?xml version=
"1.0" encoding="utf
-
8"?>

<manifest xmlns:android=
"http://schemas.android.com/apk/res/android"
package="com.android.demo.notepad3">


<application android:icon=
"@drawable/icon">


<activity android:name=
".
Notepadv3
"
android:label="@string/app_name">


<intent
-
filter>


<action android:name=
"android.intent.action.MAIN" />


<category
android:name=
"android.intent.category.LAUNCHER" />


</intent
-
filter>


</activity>


<activity android:name=
".NoteEdit"/>


</application>

</manifest>

41

Notepadv3 Example


An Activity Class

public class Notepadv3 extends ListActivity {

...


/** Called when the activity is first created. */


@Override


public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);


setContentView(R.layout.notes_list);


mDbHelper = new NotesDbAdapter(this);


mDbHelper.open();


fillData();


registerForContextMenu(getListView());


} //onCreate

...

42

Notepadv3 Example


An Intent


@Override


protected void onListItemClick(ListView l, View v, int position,
long id) {


super.onListItemClick(l, v, position, id);


Intent i = new Intent(this, NoteEdit.class);


i.putExtra(NotesDbAdapter.
KEY_ROWID, id);


startActivityForResult(i,
ACTIVITY_EDIT);


}

43

Notepadv3 Example


notes_list.xml GUI

<?xml version=
"1.0" encoding="utf
-
8"?>

<
LinearLayout

xmlns:android=
"http://schemas.android.com/apk/res/android"


android:layout_width=
"wrap_content"


android:layout_height=
"wrap_content">




<
ListView

android:id=
"@+id/android:list"


android:layout_width=
"wrap_content"


android:layout_height=
"wrap_content"/>



<
TextView

android:id=
"@+id/android:empty"


android:layout_width=
"wrap_content"


android:layout_height=
"wrap_content"


android:text=
"No Notes!"/>


</LinearLayout>


44

COMP5047 Property App Week 6 Lab Task

45


Screen shots