Your SlideShare is downloading. ×
Mobile Application Development: Lecture 09
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Mobile Application Development: Lecture 09

709
views

Published on

Published in: Technology, Design

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

  • Be the first to like this

No Downloads
Views
Total Views
709
On Slideshare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Mobile Application Development Lecture 09 Vladimir Kulyukin Department of Computer Science Utah State Universityhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 2. Outline ● Review: Explicit & Implicit Intents ● Implicit Intents ● Processing Intent Results ● Using Application Objects for Inter-Activity Data Transfershttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 3. Reviewhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 4. Review: Explicit and Implicit Intents ● Android supports two types of intents: explicit or implicit ● Explicit intents must know the Java class name of the component that they want to start ● Implicit intents must know the action type they want to be done for themhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 5. Review: Explicit Intents ● If Android OS resolves an explicit intent via its Java class identifier & the component specified by the class identifier is accessible, the component will be launched ● Explicit intents are typically used within one application when the developers have control over the Java class identifiers of the componentshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 6. Review: Explicit Intent Example ● Intent i = new Intent(this, ImageDisplayAct01.class); ● If ImageDisplayAct01.class is resolved, the corresponding component is launched ● If not, there is a run-time errorhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 7. Review: Implicit Intents ● Implicit intents must specify the type of action they should perform ● If there is a component in the Android ecosystem that is registered to perform the specified action type, it will be launched ● If there are several components in the Android ecosystem that are registered, Android will ask the user to select one via a selection dialog ● If no component is found, nothing happenshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 8. Review: Implicit Intent Example Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); ● If there is a component who can perform MediaStore.ACTION_IMAGE_CAPTURE, it will be launched ● Multiple components may be registered to perform the same actionhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 9. Review: Data Transfer with Intents ● Both types of intents can bundle data ● An intent sender creates an intent, put a few key-value pairs into it and send it to the ecosystem ● An intent receiver receives an intent, checks it for the presence/absence of specific keys and then does some action ● Keys are always Stringshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 10. Implicit Intentshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 11. Implicit Intents ● Implicit intents provide a mechanism for anonymous (no explicit activity specification) requests ● An activity asks Android OS to do an action by specifying high-level requirements but without knowing which activity or application will actually perform the action ● The requesting action can specify a URI to the data which the performing activity must process or add that data to the implicit intenthttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 12. Native Android Actions ● Android OS defines many actions that can be satisifed by native or 3-rd party components ● Here are some examples: – ACTION_ANSWER – open an activity that handles incoming calls – ACTION_CALL – open an activity with a phone dialer – ACTION_PICK – launch an activity to pick an item from a list of items – ACTION_WEB_SEARCH – launch an activity to perform a web search – ACTION_VIEW – launch an activity that can view the specified data in a reasonable mannerhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 13. Implicit Intents ● Implicit intents provide a mechanism for anonymous requests ● An implicit intent is launched with startActivity(Intent i) ● Activities and applications use intent filters to tell Android ecosystem about the actions they respond to and/or which data types they work with ● An component that has an intent filter is called an intent filterhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 14. Intent Filters Structure ● Action – Name of the action being serviced – Name should be a unique string – Best practice is to use the Java package naming convention ● Category – When the action should be serviced ● Data – Data types that the action can work withhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 15. Intent Resolution ● Intent resolution starts when an activity calls startActivity() or startActivityForResult() with some intent Intnt ● Android OS gets the list of all intent filters available from the installed packages ● Intent filters that do not match Intnts requested action or category are removed from the listhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 16. Intent Resolution ● An action match occurs when Intnts action and the filters action are the same or no action is specified ● A category match occurs when Intnts categories and the filters categories are the same ● A data match occurs when Intnts data types are the same as the filters data typeshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 17. Intent Resolution ● Native Android components are part of the Intent resolution process ● Native components are no different from 3rd- part components ● Native components do not have higher- priorities and can be completely replaced with rd 3 -party components with the users consent ● Android ecosystem does not give anyone a priori advantages to any componentshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 18. Example ● Develop an app that uses an implicit intent to request Android ecosystem to find a component that can display the wiki entry about Kurt Gödel ● This implicit intent must contain two components: Intent.ACTION_VIEW and the URI (e.g., URL of a web page) ● The default native component that responds to this intent is the web browserhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 19. LaunchWebBrowser Application Code Snippetshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 20. String Constants <resources> ... <string name=”kurt_godel_wiki_url”> http://en.wikipedia.org/wiki/Kurt_Godel </string> ... </resources>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 21. Launching Intent for Viewing URL Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources() .getString(R.string.kurt_godel_wiki_url))); this.startActivity(myIntent);http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 22. startActivity() vs startActivityForResult() ● In some cases, a component wants to get a result from an activity that it wants to launch either explicitly or implicity ● In this case, the launching component must use startActivityForResult() and implement onActivityResult() ● startActivityForResult() takes an intent and a request code (intenger constant) ● onActivityResult() is called when Android ecosystem responds to the intent launcherhttp://www.youtube.com/vkedco
  • 23. Example ● Develop an app that attaches uses an implicit intent to check for the availability of the Text-to- Speech (TTS) engine ● If the the TTS data are not installed, another intent is launched to install the data ● If all installations are successful, the app displays an activity that allows the user to enter texts and send them to the TTShttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 24. SimpleTTSDemo Code Snippetshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 25. Launching Intent with Expected Result // The request type constants. private static final int REQ_TTS_STATUS_CHECK = 0; // PICO TTS object private TextToSpeech mTts = null; // Create intent to check tts data Intent checkTtsIntent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); startActivityForResult(checkTtsIntent, REQ_TTS_STATUS_CHECK);http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 26. Processing Intents Result protected void onActivityResult(int requestCode, int resultCode, Intent data) { if ( requestCode == REQ_TTS_STATUS_CHECK ) { switch ( resultCode ) { case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS: mTts = new TextToSpeech(this, this); break; case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA: case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA: case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME: Intent installIntent = new Intent(); installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); startActivity(installIntent); case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL: default: Log.d(TAG, getResources().getString(R.string.tts_unavailable)); } }}http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 27. Stopping or Shutting TTS public void onStop() { super.onStop(); if ( mTts != null ) { mTts.stop(); } } public void onPause() { super.onPause(); if ( mTts != null ) { mTts.stop(); } } public void onDestroy() { super.onDestroy(); mTts.shutdown(); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 28. Using Application Objects for Inter-Activity Data Transfershttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 29. Data Transfer Methods ● Activities can use the System class object to transfer data if they are mounted into the same process ● Activities can transfer data with explicit intents ● Activities can transfer data with Application objectshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 30. Application Objects ● All activities within one application share the Application object that each activity can obtain via getApplication() ● An application can extend the Application class and request Android to create the object of that class ● The custom Application subclass can create data structures for each activity can access for inter- activity data transfershttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 31. Example ● Develop an app that subclasses the Application class (ImageDisplayerApp) to create its own custom Application class ● The custom Application object allows two activities (ImageRequestAct and ImageRequestProcessorAct) to display original or grayscale bitmaps ● ImageRequestAct stores its request in ImageDisplayerApp ● ImageRequestProcessorAct retrieves the request and executes it ● LuminosityConverter does the grayscale conversionhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 32. ImageDisplayerApp Code Snippetshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 33. Customizing Application Class public class ImageDisplayerApp extends Application { protected int mImgId = -1; protected String mImgOper = ""; public ImageDisplayerApp() { mImgId = -1; mImgOper = ""; } public void storeRequest(int img_id, String oper) { mImgId = img_id; mImgOper = oper; } public void removeRequest() { mImgId = -1; mImgOper = ""; } public int getRequestImgId() { return mImgId; } public String getRequestImgOper() { return mImgOper; } }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 34. Customizing Application Class <application android:icon="@drawable/rus_uzor" android:label="@string/app_name" android:theme="@style/AppTheme" android:name=".ImageDisplayerApp" > // Activity nodes </application>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 35. Accessing Customized App Object public class ImageRequestAct extends Activity { // This is the custom application object. ImageDisplayerApp mApp = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the custom app object mApp = (ImageDisplayerApp) this.getApplication(); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 36. References ● developer.android.com/guide/topics/ui/menus.html ● http://developer.android.com/guide/components/intents-filters.htmlhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 37. Feedback Bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com