"javame + android in action" cct-cejug dezembro 2008

62
JavaME + Android in action Java Platforms for Mobility CEJUG CCT Coffee with Tapioca, December 2008 Vando Batista Some Rights Reserved

Upload: vando-batista

Post on 06-May-2015

4.536 views

Category:

Technology


2 download

DESCRIPTION

Mini-cursos de JavaME e Android no evento do CEJUG Café com Tapioca, em Dezembro de 2008. 1. Introdução: overview do desenvolvimento em Java para dispositivos portáteis/móveis 2. Java ME in action: tutorial hands-on de desenvolvimento (mini-curso) 3. Android in action: tutorial hands-on de desenvolvimento (mini-curso) Autor: Vando Batista

TRANSCRIPT

Page 1: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

JavaME + Android in action

Java Platforms for Mobility Java Platforms for Mobility

CEJUG CCT

Coffee with Tapioca, December 2008

Vando Batista

Some Rights Reserved

Page 2: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Objectivity

Introduce the Java development platforms for

mobile devices,

JavaME and Android, performing

implementations activities for each one, in a implementations activities for each one, in a

lab-style practice, with some APIs

Target audience: Java developers interested on

Mobile Platforms

Page 3: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Agenda

• Overview on Mobile Platforms

• Java ME in action

• Android in action

• Challenge • Challenge

Page 4: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

After this course you will…

• identify mobile platforms characteristics

• understand how to implement a JavaME

application

• understand how to implement a Android • understand how to implement a Android

application

• have implemented, at least, one application

for each platform

• maybe… win a book: Free Software and Digital

Inclusion (in Portuguese)

Page 5: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Applications…

• Location Based Systems

• m-commerce, collaboration, marketing

• Mobile Social Networks

• Military• Military

• Search and safety

On the move apps

Improve user experience

Page 6: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Users: mobile vs. desktop

• India: Many more mobile than desktop Web users (4 to 1),

The Economic Times, Oct 2007

Page 7: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Motivation

• Evolution, popularization

– Device resources

– Connectivity

• Business applications for mobile devices• Business applications for mobile devices

– Growing 102% per year, until 2012 - Mobile

Business Applications and Services, ABI Research

• New services, new opportunities

Page 8: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Mobile Ecosystem

Source: SDN - java.sun.com/javame

Page 9: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Mobile, Portable Devices

Page 10: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Mobile Networks

3G, 4G...

Page 11: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Development Platforms

Page 12: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Mobile Computing

“Computing that deals with connection

exploration on mobile devices”Coulouris, Dellimore, Kindberg. Distributed Systems (4th edition)

Mobility transparency Mobility transparency

allows the movement of resources and clients

within a system without affecting the

operation of users or programs. Two flavours:

migration and relocation

Page 13: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

"small is the better"

• Challenges

– Devices

– Networks

– Limitations and heterogeneity– Limitations and heterogeneity

• Dependencies

– Power: battery autonomy

– Resource: connection availability

“Remember that there is no code faster than no

code” Taligent‘s Guide to Designing Programs

Page 14: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Mobile Development

• Software Development Kit (SDK)

– Emulator, Documentation, Tools, Services, etc.

• Integrated Development Environment (IDE)

• IDE Plugins• IDE Plugins

Page 15: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Are you ready?

Set your Set your

environments!

http://www.cejug.org/display/cejug/Ambiente+para+os+mini-

cursos+de+JavaME+e+Android

Page 16: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Welcome to the…

JavaME

DevelopmentDevelopment

Page 17: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Introduction

• Java Platform, Micro Edition

– Java Community Process (JCP) based

– Java Specification Request (JSR)

• Reference Implementation (RI)• Reference Implementation (RI)

• Test Compatibility Kit (TCK)

• Proprietary APIs

• Native Applications

• Since 2000

• 80% handsets: 1.2bi phones, 1200 models,

180 carriers

Page 18: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Architecture

• JVM, KVMSource: SDN - java.sun.com/javame

Page 19: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Core APIs

• Configuration

– CLDC, CDC, BD-J

• Profile

– MIDP, IM, PP, PBP, FP– MIDP, IM, PP, PBP, FP

• Umbrella

– Fragmentation: Computational, Physical,

Functional

– JTWI, MSA (Full, Subset)

Page 20: "JavaME + Android in action" CCT-CEJUG Dezembro 2008
Page 21: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Development Infrastructure

• Many environments: handset dependency

• For this lab:

– SDK: Sun WTK 2.5.2, JME SDK 3.0

– IDE: Eclipse Ganymede (3.4)– IDE: Eclipse Ganymede (3.4)

– Plugin: Mobile Tools for Java 0.9

• Unified Emulator Interface (UEI)

Page 22: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Hello JavaME World!

• build, obfuscate, preverify, run, debug, and

deploy

• Create a project

• Project structure• Project structure

• Create a MIDlet

– javax.microedition.midlet.MIDlet

• Over the Air (OTA) / installation

• On Device Deployment/Debug

• MIDlet.this.platformRequest(“tel:*”)

Page 23: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

MIDlet Lifecycle

• Application Management Software (AMS)

– Java Application Manager (JAM)

• notifyDestroyed()

• notifyPaused()

• resumeRequest()

Page 24: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

MIDlet Lifecycle

Codingimport javax.microedition.midlet.MIDlet;

public class HelloWorld extends MIDlet {

public HelloWorld() {

System.out.println(“MIDlet constructor");

}

protected void destroyApp(boolean arg0) {protected void destroyApp(boolean arg0) {

System.out.println(" MIDlet destroyApp()");

}

protected void pauseApp() {

System.out.println(" MIDlet pauseApp()");

}

protected void startApp() {

System.out.println(" MIDlet startApp()");

}

}

Page 25: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Application Packaging

• Profile dependent

• MIDlet Suite

– Java ARchive (.JAR)

– Java Application Descriptor (.JAD)– Java Application Descriptor (.JAD)

• Package generation

Page 26: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Application Configuration

• Java Application Descriptor (.JAD), Manifest

• MIDlets

• Permissions

• Properties:• Properties:

– System

System.getProperty(“javax.microedition.*”)

– Application

MIDlet.this.getAppProperty("propertyName")

Page 27: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Code Optimization

• Free objects

• String Vs. StringBuffer

• Arrays Vs. Collection

• Moderate use• Moderate use

– Synchronized

– Instance variables

– Parameter number

– Resources initiation

– Interfaces, internal classes

• JAR obfuscation, compression

Page 28: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

GUI

Source: IBM - ibm.com

Page 29: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

GUI

Codingpublic class HelloWorld extends MIDlet {

private Display display;

private Form myForm;

// initialization on MIDlet constructor

public startApp() {

display = Display.getDisplay(this);

// display.getCurrent();

display.setCurrent(myForm);

}

...

}

Page 30: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Keyboard Handling

• Components

– javax.microedition.lcdui.CommandListener

– javax.microedition.lcdui.Command

• Registration• Registration

– setCommandListener(CommandListener l)

• Notification

– commandAction(Command c, Displayable d) {}

• Potential deadlock: operations such as IO and

networking should run on a separate thread

Page 31: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Keyboard Handling

Codingpublic class HelloWorld extends MIDlet {

Displayable d = new TextBox(“Title”, “Body”,

20,TextField.ANY);

Command c = new Command(“Exit”, Command.EXIT, 0);

d.addCommand(c);

d.setCommandListener(new CommandListener() {d.setCommandListener(new CommandListener() {

public void commandAction(Command c, Displayable

s) {

doSomeAction();

}

} );

}

Page 32: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Persistent Storage

• Record Management System (RMS)

– javax.microedition.rms.*

• RecordStore

• RecordEnumeration• RecordEnumeration

• RecordComparator

• RecordFilter

Page 33: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Persistent Storage

Coding...

RecordStore rs = null;

String value = "Java ME in action";

...

try {

rs = RecordStore.openRecordStore(“RecName”, true);

byte[] recData = value.getBytes();

rs.addRecord(recData, 0, recData.length);

String data = new String(rs.getRecord(1));

} catch (Exception e) {

...

}

Page 34: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Connectivity

• Generic Connection Framework (GCF)

– javax.microedition.io.*

• Remote (Infrastructured)

– HTTP, HTTPS

– TCP, UDP– TCP, UDP

– Wireless Messaging (JSR120, 205)

– Push Registry (MIDP)

– SIP (JSR180)

• Local (Ad hoc)

– JABWT (JSR 82)

– Ad Hoc Networking API (JSR 259)

Source: SDN - java.sun.com/javame

Page 35: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Connectivity

CodingHttpConnection httpConn = null;

InputStream = null;

try {

httpConn = (HttpConnection)

Connector.open("http://www.cejug.org");

httpConn.setRequestMethod(HttpConnection.GET);

httpConn.setRequestProperty("User-Agent",

"Profile/MIDP-2.0 Configuration/CLDC 1.1");

is = httpConn.openInputStream();

int ch = -1;

while((ch = is.read()) != -1){

...

}

Page 36: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

References

• Mobile and Embedded Guide to JavaOne 2008

– http://wiki.java.net/bin/view/Mobileandembedded/JavaO

ne2008

• A Survey of Java ME Today

– http://developers.sun.com/mobility/getstart/articles/surv

ey/

• Java ME Device Table

– http://developers.sun.com/mobility/device/

• JEDI course (DFJUG)

– http://jedi.wv.com.br

Page 37: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Welcome to the…

Android

DevelopmentDevelopment

Page 38: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Open Handset Alliance

Manufactures

CarriersSemiconductors

SoftwareContent

Page 39: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Introduction

• Software stack

– Operating System

• Linux Kernel (v2.6)

– Middleware– Middleware

• Services

– Applications

• Java

• Dalvik: custom virtual machine for embedded

• Since 2008

HTC Dream (G1)

Page 40: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Characteristics

• Applications…

– without borders

– are created equal

– can run in parallel– can run in parallel

– can easily embed the web

• Open source

– http://source.android.com

• Apache 2.0 and GLP v2 license

Page 41: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Architecture

Page 42: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Basic Components

• Activity

– UI component (Form like) typically corresponding

to one screen

• Intent Receiver• Intent Receiver

– Set and respond to an external event: notifications

or status changes. Can wake up your app

• Services

– Task without UI that runs in the background

• Content Provider

– Allow applications to share data

Page 43: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Packages

android.util

android.os

android.graphics

android.text

android.telephony

android.hardware

android.text

android.database

android.content

android.view

android.widget

android.app

android.net.wifi

android.location

android.media

android.opengl

Page 44: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Development Infrastructure

• Recommended environment

• For this lab:

– SDK: AndroidSDK 1.0

• QEMU-based (system image)• QEMU-based (system image)

– IDE: Eclipse Ganymede (3.4)

– Plugin: Android Development Tool 0.8, WST

• Add to environment variable PATH

– tools directory

• A lot of XML for application, activity, intent,

layout, view, variable

Page 45: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Hello Android World!

• Create a project

• Create an Activity

– android.app.Activity

• Project structure• Project structure

• LogCat

• Deployment/Debug

Page 46: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Application Configuration

• AndroidManifest.xml

• Application

– Activity, Intent Filter

• Permissions• Permissions

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

<manifest

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

package="org.cejug.android"

android:versionCode="1"

android:versionName="1.0.0"

android:screenOrientation="landscape">

...

Page 47: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Application Anatomy

• Activity

– Can reuse functionality from other components by

making a request in the form of an Intent

– Can be replaced at any time by a new Activity – Can be replaced at any time by a new Activity

with an equivalent IntentFilter

• Intent

– Request to do something: move from screen to

screen. Activity.startActivity(myIntent)

• Intent Filter

– Description of Intent types

Page 48: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Application Configuration

...

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

android:label="@string/app_name">

<activity android:name=".HelloActivity"

android:label="@string/app_name">

<intent-filter><intent-filter>

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

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

</intent-filter>

</activity>

<activity android:name=".HelloListActivity"

android:label="ListActivity">

</activity>

</application>

</manifest>

Page 49: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

• Activities are

stacked

• Only one is • Only one is

active

• Only one is

open

Page 50: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Application

Codingpublic class HelloActivity extends Activity {

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

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TextView tv = new TextView(this);

• onStart(), onResume(), onPause(), onStop(),

onRestart(), onDestroy()

TextView tv = new TextView(this);

tv.setText("Hello, Android");

setContentView(tv);

...

}

}

Page 51: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

SDK Tools

• Emulator

• Dalvik Debug Monitoring System (ddms)

• Android Debug Bridge (adb)

• Android Asset Packaging Tool (aapt)• Android Asset Packaging Tool (aapt)

• Android Interface Description Language (aidl)

• sqlite3

• Traceview

• mksdcard

• dx, activitycreator, and others

Page 52: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Application Packaging/Deployment

• APK file

– .DEX

• Log into a Linux server via a shell

• Installation• Installation

– adb install Application.apk

– Uninstall• adb shell (remove file from /data/app/)

Page 53: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

GUI

• Define in: code or XML

• res/layout

• Views

– Text, Edit, List– Text, Edit, List

– Image, Web, Map

• Layouts

– Frame, Linear, Relative, Table, Absolute

Page 54: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

res/layout/main.xml - Views

<LinearLayout

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

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@drawable/icon">

Page 55: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

res/layout/main.xml - Layouts

<Button android:id="@+id/callButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Show Dialer" />

<EditText android:id="@+id/phoneNumber"

android:layout_width="fill_parent"android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

Page 56: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Event Handling

Coding…

final EditText phoneNumber = (EditText)

findViewById(R.id.phoneNumber);

Button callButton = (Button) findViewById(R.id.callButton);

callButton.setOnClickListener(new Button.OnClickListener()

{

public void onClick(View v) {public void onClick(View v) {

Intent CallIntent = new Intent(Intent.ACTION_CALL, Uri

.parse("tel:" + “+5585" + phoneNumber.getText()));

CallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(CallIntent);

}

});

Page 57: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Emulator: No support for…

• Placing or receiving actual phone calls

• USB connections

• Camera/video capture (input)

• Audio input• Audio input

• Determining connected state

• Determining battery charge level / AC state

• Bluetooth

Page 58: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

References

• Android Documentation

– http://code.google.com/android

• Android A Programmers Guide (Jerome

DiMarzio, Mc Graw Hill)DiMarzio, Mc Graw Hill)

• Professional Android Application Development

(Reto Meier, Willey Publishing)

• Many sites, forums, videos, screencasts,

presentations…

Page 59: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Would you like to be challenged?

Practice matters!Practice matters!

Page 60: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Challenge Requirements

• Based on our lab practice

• Create an application on JavaME or Android

platform following the requirement

– Receive a user text input through a wizard– Receive a user text input through a wizard

Page 61: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Questions & Answers

Page 62: "JavaME + Android in action" CCT-CEJUG Dezembro 2008

Thank you!Vando Batista

vandofb at gmail.com

msn: vfbatista at hotmail.com

skype: vfbatista