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
"JavaME + Android in action" CCT-CEJUG Dezembro 2008
1. JavaME + Android in action
Java Platforms for Mobility
CEJUG CCT
Coffee with Tapioca, December 2008
Vando Batista
Some Rights Reserved
2. Objectivity
Introduce the Java development platforms for
mobile devices,
JavaME and Android, performing
implementations activities for each one, in a
lab-style practice, with some APIs
Target audience: Java developers interested on
Mobile Platforms
3. Agenda
• Overview on Mobile Platforms
• Java ME in action
• Android in action
• Challenge
4. After this course you will…
• identify mobile platforms characteristics
• understand how to implement a JavaME
application
• 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)
5. Applications…
• Location Based Systems
• m-commerce, collaboration, marketing
• Mobile Social Networks
• Military
• Search and safety
On the move apps
Improve user experience
6. Users: mobile vs. desktop
• India: Many more mobile than desktop Web users (4 to 1),
The Economic Times, Oct 2007
7. Motivation
• Evolution, popularization
– Device resources
– Connectivity
• Business applications for mobile devices
– Growing 102% per year, until 2012 - Mobile
Business Applications and Services, ABI Research
• New services, new opportunities
12. Mobile Computing
“Computing that deals with connection
exploration on mobile devices”
Coulouris, Dellimore, Kindberg. Distributed Systems (4th edition)
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
13. quot;small is the betterquot;
• Challenges
– Devices
– Networks
– 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
14. Mobile Development
• Software Development Kit (SDK)
– Emulator, Documentation, Tools, Services, etc.
• Integrated Development Environment (IDE)
• IDE Plugins
15. Are you ready?
Set your
environments!
http://www.cejug.org/display/cejug/Ambiente+para+os+mini-
cursos+de+JavaME+e+Android
29. GUI
Coding
public 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);
}
...
}
30. Keyboard Handling
• Components
– javax.microedition.lcdui.CommandListener
– javax.microedition.lcdui.Command
• Registration
– setCommandListener(CommandListener l)
• Notification
– commandAction(Command c, Displayable d) {}
• Potential deadlock: operations such as IO and
networking should run on a separate thread
31. Keyboard Handling
Coding
public 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() {
public void commandAction(Command c, Displayable
s) {
doSomeAction();
}
} );
}
32. Persistent Storage
• Record Management System (RMS)
– javax.microedition.rms.*
• RecordStore
• RecordEnumeration
• RecordComparator
• RecordFilter
33. Persistent Storage
Coding
...
RecordStore rs = null;
String value = quot;Java ME in actionquot;;
...
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) {
...
}
34. Connectivity
• Generic Connection Framework (GCF)
– javax.microedition.io.*
• Remote (Infrastructured)
– HTTP, HTTPS
– TCP, UDP
– Wireless Messaging (JSR120, 205)
– Push Registry (MIDP)
– SIP (JSR180)
• Local (Ad hoc)
Source: SDN - java.sun.com/javame
– JABWT (JSR 82)
– Ad Hoc Networking API (JSR 259)
39. Introduction
• Software stack
– Operating System
• Linux Kernel (v2.6)
– Middleware
• Services
– Applications
HTC Dream (G1)
• Java
• Dalvik: custom virtual machine for embedded
• Since 2008
40. Characteristics
• Applications…
– without borders
– are created equal
– can run in parallel
– can easily embed the web
• Open source
– http://source.android.com
• Apache 2.0 and GLP v2 license
42. Basic Components
• Activity
– UI component (Form like) typically corresponding
to one screen
• 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
47. 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
with an equivalent IntentFilter
• Intent
– Request to do something: move from screen to
screen. Activity.startActivity(myIntent)
• Intent Filter
– Description of Intent types
49. • Activities are
stacked
• Only one is
active
• Only one is
open
50. Application
Coding
public 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);
tv.setText(quot;Hello, Androidquot;);
setContentView(tv);
...
}
}
• onStart(), onResume(), onPause(), onStop(),
onRestart(), onDestroy()
52. Application Packaging/Deployment
• APK file
– .DEX
• Log into a Linux server via a shell
• Installation
– adb install Application.apk
– Uninstall
• adb shell (remove file from /data/app/)
53. GUI
• Define in: code or XML
• res/layout
• Views
– Text, Edit, List
– Image, Web, Map
• Layouts
– Frame, Linear, Relative, Table, Absolute
56. 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) {
Intent CallIntent = new Intent(Intent.ACTION_CALL, Uri
.parse(quot;tel:quot; + “+5585quot; + phoneNumber.getText()));
CallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(CallIntent);
}
});
57. Emulator: No support for…
• Placing or receiving actual phone calls
• USB connections
• Camera/video capture (input)
• Audio input
• Determining connected state
• Determining battery charge level / AC state
• Bluetooth
58. References
• Android Documentation
– http://code.google.com/android
• Android A Programmers Guide (Jerome
DiMarzio, Mc Graw Hill)
• Professional Android Application Development
(Reto Meier, Willey Publishing)
• Many sites, forums, videos, screencasts,
presentations…
60. 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