Introduction to J2ME

954 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
954
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to J2ME

  1. 1. Activity Data © 2008 Haim Michael
  2. 2. Introduction  The android platform aims at eliminating the duplication of functionality in different applications by allowing the functionality of each and every application be discovered and invoked on the fly by other applications.  The two main facilities that allow separated applications interact with each other are 'Intents' and 'Remote Services'. © 2008 Haim Michael
  3. 3. The Intent Class  The Intent objects can specify an activity or a service.  Each Intent object can deliver data from one activity to another. © 2008 Haim Michael
  4. 4. Start Activity Methods  The Activity class extends Context from which it inherits various methods we can use to send Intent objects from one activity to another, whether in the same process or in a separated one. ... public abstract void startActivity (Intent intent) ... public void startActivityForResult( Intent intent, int requestCode) ... © 2008 Haim Michael
  5. 5. Coherent User Experience  The Intent and the Activity objects allow inter-process communication that link different parts of different applications into one coherent user experience that allows the user navigating between the different parts as if they were one application. © 2008 Haim Michael
  6. 6. Sample  The following sample includes two applications. Each one of them includes one activity. The activity that belongs to the first application (Dictionary Project) calls the activity that belongs to the second (Hello Languages). © 2008 Haim Michael
  7. 7. Sample  The DictionaryActivity calls the HelloActivity by using an Intent object matched with the IntentFilter XML element that was configured with the HelloActivity. © 2008 Haim Michael
  8. 8. Sample <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.abelski.samples.dictionaryapp" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".DictionaryActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="6" /> </manifest> The AndroidManifest file that defines DictionaryActivity as the entrance point of the first application © 2008 Haim Michael
  9. 9. Sample ... public class DictionaryActivity extends Activity The DictionaryActivity Source Code { TextView text; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); text = new TextView(this); setContentView(text); Intent helloLanguagesIntent = new Intent(); helloLanguagesIntent.setAction("android.intent.action.PICK"); helloLanguagesIntent.setType("vnd.abelski.hello.translate/vnd.dictionary"); startActivityForResult(helloLanguagesIntent, 0); } protected void onActivityResult(int requestCode, int resultCode, Intent result) { if (resultCode == RESULT_OK) { String str = result.getStringExtra("result"); text.setText(str); } } } ... © 2008 Haim Michael
  10. 10. Sample  The IntentFilter XML element configured with HelloActivity should match the Intent object in use. © 2008 Haim Michael
  11. 11. Sample <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.abelski.samples" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.PICK" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType= "vnd.abelski.hello.translate/vnd.dictionary" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="6" /> </manifest> The AndroidManifest file that defines HelloActivity with the required intent filter. © 2008 Haim Michael
  12. 12. Sample  When executing the DictionaryActivity the HelloActivity will be executed as part of one coherent application.  The DictionaryActivity can be part of another application. © 2008 Haim Michael
  13. 13. Sample ... public class HelloActivity extends Activity { The HelloActivity Source Code protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ((Button)findViewById(R.id.spanish)). setOnClickListener(spanishHelloListener); ((Button)findViewById(R.id.english)). setOnClickListener(englishHelloListener); ((Button)findViewById(R.id.hebrew)). setOnClickListener(hebrewHelloListener); ((Button)findViewById(R.id.arabic)). setOnClickListener(arabicHelloListener); } private OnClickListener spanishHelloListener = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("result", "Holla"); setResult(RESULT_OK, intent); finish(); } }; © 2008 Haim Michael
  14. 14. Sample private OnClickListener englishHelloListener = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("result", "Hello"); setResult(RESULT_OK, intent); finish(); } }; private OnClickListener hebrewHelloListener = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("result", "Shalom"); setResult(RESULT_OK, intent); finish(); } }; © 2008 Haim Michael
  15. 15. Sample private OnClickListener arabicHelloListener = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("result", "Salam"); setResult(RESULT_OK, intent); finish(); } }; } © 2008 Haim Michael
  16. 16. Sample © 2008 Haim Michael
  17. 17. © 2008 Haim Michael 04/08/10 Activity Data 04/08/10 © 2008 Haim Michael 1 © 2008 Haim Michael 1
  18. 18. © 2008 Haim Michael 04/08/10 Introduction  The android platform aims at eliminating the duplication of functionality in different applications by allowing the functionality of each and every application be discovered and invoked on the fly by other applications.  The two main facilities that allow separated applications interact with each other are 'Intents' and 'Remote Services'. 04/08/10 © 2008 Haim Michael 2 Using the intents facility one activity can select executing another activity based on the action and the data on which it operates, without having the need to know the exact path to the other activity. The 'Remote Methods' facility allows APIs to be accessible remotely. Remote objects allow other activities calling methods on them. The 'Remote Methods' facility is involved with using the Android’s interface definition language (AIDL). © 2008 Haim Michael 2
  19. 19. © 2008 Haim Michael 04/08/10 The Intent Class  The Intent objects can specify an activity or a service.  Each Intent object can deliver data from one activity to another. 04/08/10 © 2008 Haim Michael 3 © 2008 Haim Michael 3
  20. 20. © 2008 Haim Michael 04/08/10 Start Activity Methods  The Activity class extends Context from which it inherits various methods we can use to send Intent objects from one activity to another, whether in the same process or in a separated one. ... public abstract void startActivity (Intent intent) ... public void startActivityForResult( Intent intent, int requestCode) ... 04/08/10 © 2008 Haim Michael 4 © 2008 Haim Michael 4
  21. 21. © 2008 Haim Michael 04/08/10 Coherent User Experience  The Intent and the Activity objects allow inter-process communication that link different parts of different applications into one coherent user experience that allows the user navigating between the different parts as if they were one application. 04/08/10 © 2008 Haim Michael 5 © 2008 Haim Michael 5
  22. 22. © 2008 Haim Michael 04/08/10 Sample  The following sample includes two applications. Each one of them includes one activity. The activity that belongs to the first application (Dictionary Project) calls the activity that belongs to the second (Hello Languages). 04/08/10 © 2008 Haim Michael 6 © 2008 Haim Michael 6
  23. 23. © 2008 Haim Michael 04/08/10 Sample  The DictionaryActivity calls the HelloActivity by using an Intent object matched with the IntentFilter XML element that was configured with the HelloActivity. 04/08/10 © 2008 Haim Michael 7 © 2008 Haim Michael 7
  24. 24. © 2008 Haim Michael 04/08/10 Sample <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.abelski.samples.dictionaryapp" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".DictionaryActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="6" /> </manifest> The AndroidManifest file that defines DictionaryActivity as the entrance point of the first application 04/08/10 © 2008 Haim Michael 8 © 2008 Haim Michael 8
  25. 25. © 2008 Haim Michael 04/08/10 Sample ... public class DictionaryActivity extends Activity The DictionaryActivity Source Code { TextView text; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); text = new TextView(this); setContentView(text); Intent helloLanguagesIntent = new Intent(); helloLanguagesIntent.setAction("android.intent.action.PICK"); helloLanguagesIntent.setType("vnd.abelski.hello.translate/vnd.dictionary"); startActivityForResult(helloLanguagesIntent, 0); } protected void onActivityResult(int requestCode, int resultCode, Intent result) { if (resultCode == RESULT_OK) { String str = result.getStringExtra("result"); text.setText(str); } } } ... 04/08/10 © 2008 Haim Michael 9 © 2008 Haim Michael 9
  26. 26. © 2008 Haim Michael 04/08/10 Sample  The IntentFilter XML element configured with HelloActivity should match the Intent object in use. 04/08/10 © 2008 Haim Michael 10 © 2008 Haim Michael 10
  27. 27. © 2008 Haim Michael 04/08/10 Sample <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.abelski.samples" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.PICK" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType= "vnd.abelski.hello.translate/vnd.dictionary" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="6" /> </manifest> The AndroidManifest file that defines HelloActivity with the required intent filter. 04/08/10 © 2008 Haim Michael 11 © 2008 Haim Michael 11
  28. 28. © 2008 Haim Michael 04/08/10 Sample  When executing the DictionaryActivity the HelloActivity will be executed as part of one coherent application.  The DictionaryActivity can be part of another application. 04/08/10 © 2008 Haim Michael 12 © 2008 Haim Michael 12
  29. 29. © 2008 Haim Michael 04/08/10 Sample ... public class HelloActivity extends Activity { The HelloActivity Source Code protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ((Button)findViewById(R.id.spanish)). setOnClickListener(spanishHelloListener); ((Button)findViewById(R.id.english)). setOnClickListener(englishHelloListener); ((Button)findViewById(R.id.hebrew)). setOnClickListener(hebrewHelloListener); ((Button)findViewById(R.id.arabic)). setOnClickListener(arabicHelloListener); } private OnClickListener spanishHelloListener = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("result", "Holla"); setResult(RESULT_OK, intent); finish(); } }; 04/08/10 © 2008 Haim Michael 13 © 2008 Haim Michael 13
  30. 30. © 2008 Haim Michael 04/08/10 Sample private OnClickListener englishHelloListener = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("result", "Hello"); setResult(RESULT_OK, intent); finish(); } }; private OnClickListener hebrewHelloListener = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("result", "Shalom"); setResult(RESULT_OK, intent); finish(); } }; 04/08/10 © 2008 Haim Michael 14 © 2008 Haim Michael 14
  31. 31. © 2008 Haim Michael 04/08/10 Sample private OnClickListener arabicHelloListener = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("result", "Salam"); setResult(RESULT_OK, intent); finish(); } }; } 04/08/10 © 2008 Haim Michael 15 © 2008 Haim Michael 15
  32. 32. © 2008 Haim Michael 04/08/10 Sample 04/08/10 © 2008 Haim Michael 16 © 2008 Haim Michael 16

×