Sie sind auf Seite 1von 60

Mobile Application Development in

Android
Bangladesh-Korea Information Access Center (IAC)
Department of Computer Science and Engineering (CSE)
Bangladesh University of Engineering and Technology (BUET)
E-mail: eunus@cse.buet.ac.bd
Objective
IAC/CSE/BUET - 2012
To provide comprehensive knowledge on basic java
programming
OOP concepts, classes, objects, data types, conditions, loops, etc.
Inheritance, polymorphism, GUIs, threads, etc.
To provide the basic building blocks of Android
programming
To create and deploy mobile applications for end
users/clients
To introduce students with the advanced topic of
Android
Location based services (lbs), multimedia, web, etc.
2
Smart Phones

IAC/CSE/BUET - 2012 3
Jan 9,2007: iPhone
Its the ultimate digital device!
Display: Multi-Touch
Wireless: HSPDA, GSM, Wi-fi, Bluetooth
Location: A-GPS, Wi-fi, Cellular
Sensors: Accelerometer, Proximity
sensor, Ambient light sensor , Gyroscope
Camera: 3-5 Megapixel
IAC/CSE/BUET - 2012 4
Android, Nokia,
Goggle-Phone
Nokia-Phone
IAC/CSE/BUET - 2012 5

Market Size and Share

Share of worldwide 2010 Q4
Smartphone sales to end users by
operating system, according to
Canalys
RBC estimates there will be 449 million Smartphone users globally
by calendar year 2011, up from estimates of 102 million by the end of
calendar year 2007

IAC/CSE/BUET - 2012 6

Look Out, Apple: Android App Market About To
Blow Through 100,000 Apps

IAC/CSE/BUET - 2012 7
Why is mobile programming different?

IAC/CSE/BUET - 2012
So why cant we just put
our desktop code on a
mobile device verbatim?
Easy answer: mobile
devices have different
types of resources (RAM,
long term storage, etc)
than your laptop.
Slightly more difficult
answer:
The field is moving quickly,
driven by industry,
companies are driven by time
to market.
Consequence: APIs are
specific to devices.
Mobile devices have
increased RAM daily, so
why dont we wait until we
have enough?
Easy answer, we never will
Harder answer:
Other differences too.
Device manufacturers or
other big companies give
you the tool chain.
The API is restricted:
Dont want a program to
send data or text without
your knowledge!
User interaction is
different on a mobile
device (small screen?)
8
What is Android?
A software platform and operating system for mobile devices

Based on the Linux kernel

Developed by Google and later the Open Handset Alliance
(OHA)

Allows writing managed code in the Java language

Possibility to write applications in other languages and compiling
it to ARM native code (support of Google? No)

Unveiling of the Android platform was announced on 5
November 2007 with the founding of OHA

IAC/CSE/BUET - 2012 9
What is Open Handset Alliance (OHA)?

IAC/CSE/BUET - 2012 10
Android Features
Application Framework
Dalvik virtual machine
Integrated (webkit) browser
2D and 3D graphics APIs with HW
SQLite
Video and audio codecs
Bluetooth, EDGE, 3G, and WiFi
Camera, GPS, compass, and accelerometer

11 IAC/CSE/BUET - 2012
Android Architecture / Software Stack
Program Stack
Linux Kernel Core services (including hardware
drivers, process and memory management, security,
network, and power management) are handled by a
Linux 2.6 kernel. The kernel also provides an
abstraction layer between the hardware and the
remainder of the stack.
Libraries Running on top of the kernel, Android
includes various C/C++ core libraries such as libc
and SSL, as well as: multimedia, OpenGL, SQLite
IAC/CSE/BUET - 2012 13
Program Stack
Android Run Time What makes an Android phone an
Android phone rather than a mobile Linux
implementation is the Android run time
Core Libraries While Android development is done in Java,
Dalvik is not a Java VM. The core Android libraries provide most
of the functionality available in the core Java libraries as well as
the Android-specifi c libraries.
Dalvik Virtual Machine Dalvik is a register-based virtual machine
thats been optimized o ensure that a device can run multiple
instances efficiently. It relies on the Linux kernel for threading
and low-level memory management.
IAC/CSE/BUET - 2012 14
Program Stack
Application Framework The application framework
provides the classes used to create Android applications.
It also provides a generic abstraction for hardware
access and manages the user interface and application
resources.
Application Layer All applications, both native and third
party, are built on the application layer using the same
API libraries. The application layer runs within the
Android run time using the classes and services made
available from the application framework.
IAC/CSE/BUET - 2012 15
Challenges
CPU typically runs 500-600 Mhz
RAM available to an App may only be a few megabytes
Disk (flash) access is very slow
Lifecycle - apps must pause/quit often, and restore to give
the illusion that they are always running
UI design
o typical screen may be HVGA (320x480)
o may be in portrait or landscape
o very high DPI - small text may not be readable
o touch resolution is very low (~25 pixel)
Network access may be slow and (very) intermittent
16 IAC/CSE/BUET - 2012
Opportunities
100s of millions of mobile phone users
Very high growth, esp. in Asia
"Wild wild west" of application development
o think 1985 on the desktop (if you were born by then)
o no dominant 3rd party developers.... yet
o what will the killer app categories be?
o what does it mean to have any app + the internet in your pocket?
You can develop for it today!
17 IAC/CSE/BUET - 2012
Possibilities

IAC/CSE/BUET - 2012
Augmented Campus Reality
camera + maps + compass =
heads up info

Games
multi-player (bluetooth, wifi,
cell)
location-based (scavenger hunt)

Custom overlays for maps
weather, traffic, friends
Web scraping
news filters
sport scores
dook news eliminators

Distributed computing?
10,000 phones all donating
their idle time
tiny terminal (phone) + fast
cpu/network (server)

Shared whiteboard
18
Android Platform

Hardware
Android is not a single piece of hardware; it's a complete, end-to-
end software platform that can be adapted to work on any
number of hardware configurations. Everything is there, from the
bootloader all the way up to the applications.

IAC/CSE/BUET - 2012 19
Platform
Operating System

- Android uses Linux for its device drivers, memory
management, process management, and networking.

- The next level up contains the Android native libraries. They are
all written in C/C++ internally, but youll be calling them through
Java interfaces. In this layer you can find the Surface Manager,
2D and 3D graphics, Media codecs, the SQL database (SQLite),
and a native web browser engine (WebKit).

- Dalvik Virtual Machine. Dalvik runs dex files, which are
coverted at compile time from standard class and jar files.

IAC/CSE/BUET - 2012 20
Platform
Networking

- GSM mobile-phone technology

- 3G

- Edge

- 802.11 Wi-Fi networks

IAC/CSE/BUET - 2012 21
Platform
Security
Android is a multi-process system, in which each application
(and parts of the system) runs in its own process. Most security
between applications and the system is enforced at the process
level through standard Linux facilities, such as user and group
IDs that are assigned to applications.

Additional finer-grained security features are provided through a
"permission" mechanism that enforces restrictions on the specific
operations that a particular process can perform, and per-URI
permissions for granting ad-hoc access to specific pieces of
data.

IAC/CSE/BUET - 2012 22
Platform
Performance
IAC/CSE/BUET - 2012 23
Supported OS
1. Windows XP (32-bit), Vista (32- or 64-bit), or Windows 7
(32- or 64-bit)
2. Mac OS X 10.5.8 or later (x86 only)
3. Linux (tested on Ubuntu Linux, Lucid Lynx)
Supported Development Environment (Eclipse IDE based)
1. Eclipse 3.4 (Ganymede) or greater/Eclipse Classic (versions
3.5.1 and higher)
2. JDK 1.5 or 1.6
3. ADT Plugin

Setting up Development Env.
24 IAC/CSE/BUET - 2012
Setting up Development Env.
To set up the development environment, we
need:
Java Development Kit (JDK 1.5+, 1.6 is preferable)
Eclipse IDE
Android SDK
ADT plugin
IAC/CSE/BUET - 2012 25
Setting up Development Env.
Java Development Kit (JDK)
IAC/CSE/BUET - 2012
Download Link: http://www.java.com/en/download/index.jsp
Download JDK from here and Install JDK
26
Eclipse IDE
IAC/CSE/BUET - 2012
Download Link: http://eclipse.org/downloads/
27
Android SDK
IAC/CSE/BUET - 2012
Link: http://developer.android.com/sdk/index.html
28
Setting Dev. Env.
Android SDK Setup
IAC/CSE/BUET - 2012 29
Setting Dev. Env.
Android SDK Setup
IAC/CSE/BUET - 2012 30
What is in the SDK?
The Android APIs
Development Tools
The Android Emulator
Full Documentation
Sample Code
Online Support
IAC/CSE/BUET - 2012 31
ADT Plugin: Android Development Tools (ADT) is a
plugin for the Eclipse IDE for

1. extending the capabilities of Eclipse to set up new Android
projects
2. creating an application UI
3. adding components based on the Android Framework API
4. debugging your applications using the Android SDK tools
5. exporting signed (or unsigned) APKs in order to distribute your
application
IAC/CSE/BUET - 2012 32
Installing the ADT
IAC/CSE/BUET - 2012
1. Start Eclipse, then select Help >
Install New Software....
2. Click Add, in the top-right
corner.
3. In the next window, you'll see a
list of the tools to be
downloaded. Click Next.
4. Read and accept the license
agreements, then click Finish.
5. When the installation
completes, restart Eclipse.

33
Set SDK path
IAC/CSE/BUET - 2012
1. Start Eclipse, then select
Window > Preferences
2. Browse and show the path of
Android SDK.
3. Press Apply and OK.
34
IAC/CSE/BUET - 2012 35
Hello World !!!
Create a very simple
application
Run it on a real device
Run it on the emulator
Examine its structure
IAC/CSE/BUET - 2012 36
Google Tutorial
We will follow the tutorial at:
http://developer.android.com/resources/tutorials/hello-
world.html
Start Eclipse (Start -> All Programs -> Eclipse)
Create an Android Virtual Device (AVD)
Create a New Android Project
Creating an Application
Start Eclipse and Go to File>New>Android Project
We keep project name HelloAndroid
Build Target Android2.1
Application name: Hello Android
Package name: com.example.helloandroid
Initial Activity Name: HelloAndroid
Min SDK Version: 6
Click Finish


IAC/CSE/BUET - 2012 37
IAC/CSE/BUET - 2012 38
Package Content
Java code for our activity All source code here
Generated Java code
Helps link resources to
Java code
Layout of the activity
Strings used in the
program
All non-code
resources
Android Manifest
Images
IAC/CSE/BUET - 2012 39
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloAndroid"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>


</manifest>
IAC/CSE/BUET - 2012 40
Activity
An Android activity is
focused on a single
thing a user can do.
Most applications have
multiple activities
IAC/CSE/BUET - 2012 41
Activities start each other
IAC/CSE/BUET - 2012 42
Revised HelloAndroid.java
package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android by hand");
setContentView(tv);
}
}
Set the view by hand
from the program
Inherit
from the
Activity
Class
IAC/CSE/BUET - 2012 43
Run it!
IAC/CSE/BUET - 2012 44
/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>

Further redirection to
/res/values/strings.xml
IAC/CSE/BUET - 2012 45
/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloAndroid by resources!</string>
<string name="app_name">Hello, Android</string>
</resources>

IAC/CSE/BUET - 2012 46
HelloAndroid.java
package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Set the layout of the view
as described in the
main.xml layout
IAC/CSE/BUET - 2012 47
/gen/R.java
package com.example.helloandroid;

public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int textview=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
IAC/CSE/BUET - 2012 48
Run it!
IAC/CSE/BUET - 2012 49

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=Hello World, Welcome to Android"
/>
</LinearLayout>

Accessing UI Elements
To get access to your UI elements in code, you add
identifier attributes to them in the XML definition.
You can then use the findViewById method to return
a reference to each named item.
<TextView
android:id=@+id/myTextView
android:layout_width=fill_parent
android:layout_height=wrap_content
android:text=Hello World, HelloWorld
/>
How to get access to it in code:
TextView myTextView =
(TextView)findViewById(R.id.myTextView);

IAC/CSE/BUET - 2012 50
IAC/CSE/BUET - 2012 51

Alternatively (although its not considered good practice), if you need to, you
can create your layout directly in code as shown below:

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout.LayoutParams lp;
lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
LinearLayout.LayoutParams textViewLP;
textViewLP = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView myTextView = new TextView(this);
myTextView.setText(Hello World, HelloWorld);
ll.addView(myTextView, textViewLP);
this.addContentView(ll, lp);
}
IAC/CSE/BUET - 2012 52
Introduce a bug
package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;

public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Object o = null;
o.toString();
setContentView(R.layout.main);
}
}
IAC/CSE/BUET - 2012 53
Run it!
Development Tools
The Android Emulator:
An implementation of the Android virtual machine designed to run on
your development computer. You can use the emulator to test and
debug your android applications.
Dalvik Debug Monitoring Service (DDMS):
Use the DDMS perspective to monitor and control the Dalvik virtual
machines on which youre debugging your applications.
Android Asset Packaging Tool (AAPT):
Constructs the distributable Android package files (.apk).
Android Debug Bridge (ADB):
The ADB is a client-server application that provides a link to a running
emulator. It lets you copy files, install compiled application packages
(.apk), and run shell commands.
IAC/CSE/BUET - 2012 54
Android Components
Android applications consist of loosely coupled
components, bound using a project manifest that
describes each component and how they interact.
There are six components that provide the building
blocks for your applications:
Activities
Services
Content Providers
Intents
Broadcast Receivers
Notifications
IAC/CSE/BUET - 2012 55
Android Components
Activities:
Your applications presentation layer. Every screen in your
application will be an extension of the Activity class.
Activities use Views to form graphical user interfaces that display
information and respond to user actions.
In terms of desktop development, an Activity is equivalent to a
Form.
Services:
The invisible workers of your application. Service components
run invisibly, updating your data sources and visible Activities
and triggering Notifications.
Theyre used to perform regular processing that needs to
continue even when your applications Activities arent active or
visible.
IAC/CSE/BUET - 2012 56
Android Components
Content Providers:
A shareable data store. Content Providers are used to manage
and share application databases.
Content Providers are the preferred way of sharing data across
application boundaries.
Android devices include several native Content Providers that
expose useful databases like contact information.
Intents:
A simple message-passing framework. Using Intents, you can
broadcast messages system-wide or to a target Activity or
Service, stating your intention to have an action performed.
The system will then determine the target(s) that will perform any
actions as appropriate.
IAC/CSE/BUET - 2012 57
Android Components
Broadcast Receivers
Intent broadcast consumers. By creating and registering a
Broadcast Receiver, your application can listen for broadcast
Intents that match specific filter criteria.
Broadcast Receivers will automatically start your application to
respond to an incoming Intent, making them ideal for event-
driven applications.
Notifications
A user notification framework. Notifications let you signal users
without stealing focus or interrupting their current Activities.
Theyre the preferred technique for getting a users attention
from within a Service or Broadcast Receiver.
For example, when a device receives a text message or an
incoming call, it alerts you by flashing lights, making sounds,
displaying icons, or showing dialog messages.
IAC/CSE/BUET - 2012 58
Android Components
Activity
Present a visual user interface for one focused endeavor the user
can undertake
Example: a list of menu items users can choose from
Services
Run in the background for an indefinite period of time
Example: calculate and provide the result to activities that need it
Broadcast Receivers
Receive and react to broadcast announcements
Example: announcements that the time zone has changed
IAC/CSE/BUET - 2012 59
Content Providers
Store and retrieve data and make it accessible to all applications
Example: Android ships with a number of content providers for
common data types (e.g., audio, video, images, personal contact
information, etc.)
Intents
Hold the content of a message
Example: convey a request for an activity to present an image to
the user or let the user edit some text


IAC/CSE/BUET - 2012 60

Das könnte Ihnen auch gefallen