Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Enterprise Application
Development using J2EE
Shmulik London
Lecture #2
A Crash Course to
Android Mobile Platform
Enterprise Application Development Using J2EE / © Shmulik London 2004
Interdisciplinary Center Herzeliza Israel
Context
We’ve already mentioned the jungle of
mobile platforms. For the course exercise
J2ME serves us well as it is small and easy to
learn with no much background. So a single
lecture is enough for the mobile part of the
exercises.
However we would like to encourage you to
look also at more modern platforms such as
Android and iPhone and maybe use them in
the course project.
This lecture is just a crash course for the
Android platform (mainly an hands-on demo)
to motivate you to read further.
Enterprise Application Development Using J2EE / © Shmulik London 2008
Android Architecture
Enterprise Application Development Using J2EE / © Shmulik London 2008
*
Comparison with J2ME
Top Layer
Complete Stack
APIs/SPIs
Kernel, libs, VM, framework, apps..
Big impact on security & permissions!
Very broad scope
Well defined scope
From a door-knob to PDA
A modern mobile phone
Many optional APIs not all
supported by all devices
Little control and interaction
with the phone
Higher requirement from devices
Larger common denominator
Wide differences and peculiarities
between devices
Didn’t held up to the judge of time
* We are not really comparing apples with apples, different scope, Android came up 7 years after J2ME
Enterprise Application Development Using J2EE / © Shmulik London 2008
Comparison with J2ME
Provisioning mostly
via service providers
Open Market
Standalone apps
Promotes Mash-ups
Multi-processed
Background possible on
some devices but no
interaction
Naïve UI framework
and canvas
Much richer UI set
Open GLE, effects, …
Testing with Simulator
Testing with Emulator
Very simple
More complex
Ubiquitous
Time will tell…
Enterprise Application Development Using J2EE / © Shmulik London 2008
Main Concepts
Activity
&
Views
Broadcast
Intent
Intents
Service
Data Provider
Network
Enterprise Application Development Using J2EE / © Shmulik London 2008
Demo #1
Enterprise Application Development Using J2EE / © Shmulik London 2008
Project Structure
Enterprise Application Development Using J2EE / © Shmulik London 2008
Manifest
<manifest
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
package="examples.sidekick">
package="examples.sidekick">
<application
<application
android:label=“SideKick"
android:label=“SideKick"
android:icon="@drawable/sidekick">
android:icon="@drawable/sidekick">
<activity
<activity android:name="MainActivity“
android:name="MainActivity“
android:label="SideKick">
android:label="SideKick">
<intent-filter>
<intent-filter>
<action
<action
android:name="android.intent.action.MAIN"/>
android:name="android.intent.action.MAIN"/>
<category
<category
android:name="android.intent.category.LAUNCHER"/>
android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</intent-filter>
</activity>
</activity>
</application>
</application>
</manifest>
</manifest>
Enterprise Application Development Using J2EE / © Shmulik London 2008
Layout (main_screen.xml)
<?xml
<?xml version="1.0"
version="1.0" encoding="utf-8"?>
encoding="utf-8"?>
<LinearLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
android:gravity="center">
<Button
<Button android:id="@+id/criticalButton"
android:id="@+id/criticalButton"
android:text="Should
android:text="Should I?"
I?"
android:layout_width="100px"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_height="wrap_content"
/>
/>
</LinearLayout>
</LinearLayout>
Enterprise Application Development Using J2EE / © Shmulik London 2008
Activity
public
public class
class MainActivity
MainActivity extends
extends Activity
Activity {{
public
public void
void onCreate(Bundle
onCreate(Bundle bundle)
bundle) {{
super.onCreate(bundle);
super.onCreate(bundle);
setContentView(R.layout.main_screen);
setContentView(R.layout.main_screen);
Button
Button button
button == (Button)findViewById(R.id.criticalButton);
(Button)findViewById(R.id.criticalButton);
button.setOnClickListener(new
button.setOnClickListener(new View.OnClickListener()
View.OnClickListener() {{
public
public void
void onClick(View
onClick(View view)
view) {{
toss();
toss();
}}
});
});
}}
private
private void
void toss()
toss() {{
boolean
boolean doIt
doIt == Math.random()
Math.random() >> 0.5;
0.5;
int
int message
message == doIt
doIt ?? R.string.possitive
R.string.possitive :: R.string.negative;
R.string.negative;
new
new AlertDialog.Builder(this).setTitle(message).create().show();
AlertDialog.Builder(this).setTitle(message).create().show();
}}
}}
Enterprise Application Development Using J2EE / © Shmulik London 2008
Resources (strings.xml)
<?xml
<?xml version="1.0"
version="1.0" encoding="utf-8"?>
encoding="utf-8"?>
<resources>
<resources>
<string
<string name="possitive">Yes,
name="possitive">Yes, you
you should
should do
do it!</string>
it!</string>
<string
<string name="negative">No,
name="negative">No, II wouldn't
wouldn't do
do it!</string>
it!</string>
</resources>
</resources>
Enterprise Application Development Using J2EE / © Shmulik London 2008
Demo
Enterprise Application Development Using J2EE / © Shmulik London 2004
Interdisciplinary Center Herzeliza Israel
Few things we’ll need
for the course…
• A simple game skeleton
• Networking
Enterprise Application Development Using J2EE / © Shmulik London 2008
Simple game skeleton
Enterprise Application Development Using J2EE / © Shmulik London 2008
Simple Game Canvas
public
public class
class GameView
GameView extends
extends View
View {{
...
... //
// instance
instance variables
variables
public
public GameView(Context
GameView(Context context)
context) {{
super(context);
super(context);
playerBitmap
playerBitmap == BitmapFactory.decodeResource(
BitmapFactory.decodeResource(
getResources(),
getResources(), R.drawable.android_32x40);
R.drawable.android_32x40);
paint
paint == new
new Paint();
Paint(); //
// for
for drawing
drawing the
the background
background
}}
protected
protected void
void onDraw(Canvas
onDraw(Canvas canvas)
canvas) {{
canvas.drawOval(new
canvas.drawOval(new RectF(
RectF( //
// drawing
drawing background
background
getWidth()/2-40,
getWidth()/2-40, getHeight()/2-40,
getHeight()/2-40,
getWidth()/2+40,
getWidth()/2+40, getHeight()/2+40),
getHeight()/2+40), paint);
paint);
canvas.drawBitmap(playerBitmap,
canvas.drawBitmap(playerBitmap, x,
x, y,
y, paint);
paint);
}}
}}
Enterprise Application Development Using J2EE / © Shmulik London 2008
Simple Game Canvas
public
public class
class GameActivity
GameActivity extends
extends Activity
Activity {{
private
private GameView
GameView gameView;
gameView;
public
public void
void onCreate(Bundle
onCreate(Bundle bundle)
bundle) {{
super.onCreate(bundle);
super.onCreate(bundle);
gameView
gameView == new
new GameView(this);
GameView(this);
setContentView(gameView);
setContentView(gameView);
gameView.setOnTouchListener(new
gameView.setOnTouchListener(new View.OnTouchListener()
View.OnTouchListener() {{
public
public boolean
boolean onTouch(View
onTouch(View view,
view, MotionEvent
MotionEvent e)
e) {{
gameView.setFigureLocation(e.getX(),
gameView.setFigureLocation(e.getX(), e.getY());
e.getY());
gameView.postInvalidate();
gameView.postInvalidate();
return
return true;
true;
}}
});
});
}}
Enterprise Application Development Using J2EE / © Shmulik London 2008
Simple Game Canvas
protected
protected void
void onStart()
onStart() {{
super.onStart();
super.onStart();
gameView.setFigureLocation(
gameView.setFigureLocation(
gameView.getWidth()/2,
gameView.getWidth()/2, gameView.getHeight()/2);
gameView.getHeight()/2);
}}
}}
Enterprise Application Development Using J2EE / © Shmulik London 2008
Http Example
Enterprise Application Development Using J2EE / © Shmulik London 2008
Http Example
public
public class
class DictionaryActivity
DictionaryActivity extends
extends Activity
Activity {{
private
private HttpClient
HttpClient client;
client;
public
public void
void onCreate(Bundle
onCreate(Bundle bundle)
bundle) {{
super.onCreate(bundle);
super.onCreate(bundle);
client
client == new
new DefaultHttpClient();
DefaultHttpClient();
setContentView(R.layout.dictionary_screen);
setContentView(R.layout.dictionary_screen);
Button
Button subButton
subButton == (Button)findViewById(R.id.submitButton);
(Button)findViewById(R.id.submitButton);
subButton.setOnClickListener(new
subButton.setOnClickListener(new View.OnClickListener()
View.OnClickListener() {{
public
public void
void onClick(View
onClick(View view)
view) {{
EditText
EditText inputField
inputField == (EditText)
(EditText)
findViewById(R.id.wordField);
findViewById(R.id.wordField);
String
String word
word == inputField.getText().toString();
inputField.getText().toString();
Runnable
Runnable task
task == new
new FetchDefinitionTask(word);
FetchDefinitionTask(word);
new
Thread(task,
"FetchDefinition").start();
new Thread(task, "FetchDefinition").start();
}}
});
});
}}
Enterprise Application Development Using J2EE / © Shmulik London 2008
Http Example
class
class FetchDefinitionTask
FetchDefinitionTask implements
implements Runnable
Runnable {{
private
private String
String word;
word;
FetchDefinitionTask(String
FetchDefinitionTask(String word)
word) {{
this.word
this.word == word;
word;
}}
public
public void
void run()
run() {{
String
String url
url == "http://127.0.0.1:8080/dictionary/dictionary"+
"http://127.0.0.1:8080/dictionary/dictionary"+
"?word=“+URLEncoder.encode(word);
"?word=“+URLEncoder.encode(word);
HttpGet
HttpGet request
request == new
new HttpGet(url);
HttpGet(url);
request.addHeader("Content-Type",
request.addHeader("Content-Type", "text/plain");
"text/plain");
try
try {{
HttpResponse
HttpResponse response
response == client.execute(request);
client.execute(request);
if
if (response.getStatusLine().getStatusCode()
(response.getStatusLine().getStatusCode() !=
!= 200)
200) {{
displayResult(“Sorry,
displayResult(“Sorry, failed
failed to
to fetch
fetch definition.”);
definition.”);
return;
return;
}}
Enterprise Application Development Using J2EE / © Shmulik London 2008
Http Example
HttpEntity
HttpEntity entity
entity == response.getEntity();
response.getEntity();
String
String body
body == EntityUtils.toString(entity);
EntityUtils.toString(entity);
displayResult(body);
displayResult(body);
}} catch
catch (IOException
(IOException e)
e) {{
Log.e("dictionary",
Log.e("dictionary", "failed:
"failed: "+url,
"+url, e);
e);
displayResult("Sorry,
displayResult("Sorry, Failed
Failed to
to fetch
fetch definition!");
definition!");
}}
}}
}}
private
private void
void displayResult(final
displayResult(final String
String message)
message) {{
DictionaryActivity.this.runOnUiThread(new
DictionaryActivity.this.runOnUiThread(new Runnable()
Runnable() {{
public
public void
void run()
run() {{
TextView
TextView resultField
resultField ==
(TextView)findViewById(R.id.definitionField);
(TextView)findViewById(R.id.definitionField);
resultField.setText(message);
resultField.setText(message);
}}
});
});
}}
Enterprise Application Development Using J2EE / © Shmulik London 2008
Http Example
<?xml
<?xml version="1.0"
version="1.0" encoding="utf-8"?>
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
android:gravity="center">
<LinearLayout
<LinearLayout
android:orientation="horizontal"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content">
<EditText
<EditText android:id="@+id/wordField"
android:id="@+id/wordField"
android:layout_width="fill_parent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_weight="1"
/>
/>
<Button
<Button android:id="@+id/submitButton"
android:id="@+id/submitButton"
android:text="Submit"
android:text="Submit"
android:layout_width="100px"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_height="wrap_content"
/>
/>
</LinearLayout>
</LinearLayout>
<TextView
<TextView android:id="@+id/definitionField"
android:id="@+id/definitionField"
android:layout_width="fill_parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
Enterprise Application Development Using J2EE / © Shmulik London 2008
Http Example
<manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
package="dictionary">
package="dictionary">
<uses-permission
<uses-permission android:name="android.permission.INTERNET"/>
android:name="android.permission.INTERNET"/>
<application
<application
android:label="Online-Dictionary">
android:label="Online-Dictionary">
<activity
<activity android:name="DictionaryActivity"
android:name="DictionaryActivity" android:label="Online-Dictionary">
android:label="Online-Dictionary">
<intent-filter>
<intent-filter>
<action
<action android:name="android.intent.action.MAIN"/>
android:name="android.intent.action.MAIN"/>
<category
<category android:name="android.intent.category.LAUNCHER"/>
android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</intent-filter>
</activity>
</activity>
</application>
</application>
</manifest>
</manifest>
Enterprise Application Development Using J2EE / © Shmulik London 2008
Summary
• We only gave you a taste of
Android, there is much more
– services, data-providers, OpenGL,
Location API, XMPP, Accelerometer…
• And don’t forget the other platform
you can choose from:
– iPhone, Blackberry, Windows-Mobile…
Enterprise Application Development Using J2EE / © Shmulik London 2008