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.
Android Architecture
Comparison with J2ME * Top Layer APIs/SPIs Complete Stack Kernel, libs, VM, framework, apps.. Big impact on security & permissions! Very broad scope From a door-knob to PDA Well defined scope A modern mobile phone Many optional APIs not all supported by all devices Little control and interaction with the phone Wide differences and peculiarities between devices Higher requirement from devices Larger common denominator 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
Comparison with J2ME Provisioning mostly via service providers Open Market Standalone apps Background possible on some devices but no interaction Naïve UI framework and canvas Testing with Simulator Very simple Ubiquitous Promotes Mash-ups Multi-processed Much richer UI set Open GLE, effects, Testing with Emulator More complex Time will tell
Main Concepts Activity & Views Intents Data Provider Broadcast Intent Service Network
Demo #1
Project Structure
Manifest <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="examples.sidekick"> <application android:label= SideKick" android:icon="@drawable/sidekick"> <activity android:name="mainactivity android:label="sidekick"> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher"/> </intent-filter> </activity> </application> </manifest>
Layout (main_screen.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" android:gravity="center"> <Button android:id="@+id/criticalbutton" android:text="should I?" android:layout_width="100px" android:layout_height="wrap_content" /> /> </LinearLayout>
Activity public class MainActivity extends Activity { public void oncreate(bundle bundle) { super.oncreate(bundle); setcontentview(r.layout.main_screen); Button button = (Button)findViewById(R.id.criticalButton); button.setonclicklistener(new View.OnClickListener() { public void onclick(view view) { toss(); ); ); private void toss() { boolean doit = Math.random() > 0.5; int int message = doit? R.string.possitive : R.string.negative; new new AlertDialog.Builder(this).setTitle(message).create().show();
Resources (strings.xml) <?xml version="1.0" encoding="utf-8"?> <resources> <string name="possitive">yes, you should do do it!</string> <string name="negative">no, I wouldn't do do it!</string> </resources>
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
Simple game skeleton
Simple Game Canvas public class GameView extends View {... // // instance variables public GameView(Context context) { super(context); playerbitmap = BitmapFactory.decodeResource( getresources(), R.drawable.android_32x40); paint = new Paint(); // // for drawing the background protected void ondraw(canvas canvas) { canvas.drawoval(new RectF( // // drawing background getwidth()/2-40, getheight()/2-40, getwidth()/2+40, getheight()/2+40), paint); canvas.drawbitmap(playerbitmap, x, x, y, y, paint);
Simple Game Canvas public class GameActivity extends Activity { private GameView gameview; public void oncreate(bundle bundle) { super.oncreate(bundle); gameview = new GameView(this); setcontentview(gameview); gameview.setontouchlistener(new View.OnTouchListener() { public boolean ontouch(view view, MotionEvent e) e) { gameview.setfigurelocation(e.getx(), e.gety()); gameview.postinvalidate(); return true; );
Simple Game Canvas protected void onstart() { super.onstart(); gameview.setfigurelocation( gameview.getwidth()/2, gameview.getheight()/2);
Http Example
Http Example public class DictionaryActivity extends Activity { private HttpClient client; public void oncreate(bundle bundle) { super.oncreate(bundle); client = new new DefaultHttpClient(); setcontentview(r.layout.dictionary_screen); Button subbutton = (Button)findViewById(R.id.submitButton); subbutton.setonclicklistener(new View.OnClickListener() { public void onclick(view view) { EditText inputfield = (EditText) findviewbyid(r.id.wordfield); String word = inputfield.gettext().tostring(); Runnable task = new new FetchDefinitionTask(word); new new Thread(task, "FetchDefinition").start(); ); );
Http Example class FetchDefinitionTask implements Runnable { private String word; FetchDefinitionTask(String word) { this.word = word; public void run() { String url url = "http://127.0.0.1:8080/dictionary/dictionary"+ "?word= +URLEncoder.encode(word); HttpGet request = new new HttpGet(url); request.addheader("content-type", "text/plain"); try try { HttpResponse response = client.execute(request); if if (response.getstatusline().getstatuscode()!=!= 200) { displayresult( Sorry, failed to to fetch definition. ); return;
Http Example HttpEntity entity = response.getentity(); String body = EntityUtils.toString(entity); displayresult(body); catch (IOException e) e) { Log.e("dictionary", "failed: "+url, e); e); displayresult("sorry, Failed to to fetch definition!"); private void displayresult(final String message) { DictionaryActivity.this.runOnUiThread(new Runnable() { public void run() { TextView resultfield = (TextView)findViewById(R.id.definitionField); resultfield.settext(message); ); );
Http Example <?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" android:gravity="center"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/wordfield" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> /> <Button android:id="@+id/submitbutton" android:text="submit" android:layout_width="100px" android:layout_height="wrap_content" /> /> </LinearLayout> <TextView android:id="@+id/definitionfield" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"/> </LinearLayout>
Http Example <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="dictionary"> <uses-permission android:name="android.permission.internet"/> <application android:label="online-dictionary"> <activity android:name="dictionaryactivity" android:label="online-dictionary"> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher"/> </intent-filter> </activity> </application> </manifest>
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