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 1 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 2 Demo #1 Enterprise Application Development Using J2EE / © Shmulik London 2008 Project Structure Enterprise Application Development Using J2EE / © Shmulik London 2008 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> Enterprise Application Development Using J2EE / © Shmulik London 2008 3 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> Enterprise Application Development Using J2EE / © Shmulik London 2008 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 message = doIt ? R.string.possitive : R.string.negative; new AlertDialog.Builder(this).setTitle(message).create().show(); } } Enterprise Application Development Using J2EE / © Shmulik London 2008 Resources (strings.xml) <?xml version="1.0" encoding="utf-8"?> <resources> <string name="possitive">Yes, you should do it!</string> <string name="negative">No, I wouldn't do it!</string> </resources> Enterprise Application Development Using J2EE / © Shmulik London 2008 4 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 5 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, y, paint); } } Enterprise Application Development Using J2EE / © Shmulik London 2008 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) { gameView.setFigureLocation(e.getX(), e.getY()); gameView.postInvalidate(); return true; } }); } Enterprise Application Development Using J2EE / © Shmulik London 2008 Simple Game Canvas protected void onStart() { super.onStart(); gameView.setFigureLocation( gameView.getWidth()/2, gameView.getHeight()/2); } } Enterprise Application Development Using J2EE / © Shmulik London 2008 6 Http Example Enterprise Application Development Using J2EE / © Shmulik London 2008 Http Example public class DictionaryActivity extends Activity { private HttpClient client; public void onCreate(Bundle bundle) { super.onCreate(bundle); client = 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 FetchDefinitionTask(word); new Thread(task, "FetchDefinition").start(); } }); } Enterprise Application Development Using J2EE / © Shmulik London 2008 Http Example class FetchDefinitionTask implements Runnable { private String word; FetchDefinitionTask(String word) { this.word = word; } public void run() { String url = "http://127.0.0.1:8080/dictionary/dictionary"+ "?word=“+URLEncoder.encode(word); HttpGet request = new HttpGet(url); request.addHeader("Content-Type", "text/plain"); try { HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() != 200) { displayResult(“Sorry, failed to fetch definition.”); return; } Enterprise Application Development Using J2EE / © Shmulik London 2008 7 Http Example HttpEntity entity = response.getEntity(); String body = EntityUtils.toString(entity); displayResult(body); } catch (IOException e) { Log.e("dictionary", "failed: "+url, e); displayResult("Sorry, Failed 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); } }); } Enterprise Application Development Using J2EE / © Shmulik London 2008 Http Example <?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> Enterprise Application Development Using J2EE / © Shmulik London 2008 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> Enterprise Application Development Using J2EE / © Shmulik London 2008 8 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 9