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


Published on

Published in: Technology, Design

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Mobile Application Development Lecture 09 Vladimir Kulyukin Department of Computer Science Utah State University
  • 2. Outline ● Review: Explicit & Implicit Intents ● Implicit Intents ● Processing Intent Results ● Using Application Objects for Inter-Activity Data Transfers
  • 3. Review
  • 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 them
  • 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 components
  • 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 error
  • 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 happens
  • 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 action
  • 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 Strings
  • 10. Implicit Intents
  • 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 intent
  • 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 manner
  • 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 filter
  • 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 with
  • 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 list
  • 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 types
  • 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 components
  • 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 browser
  • 19. LaunchWebBrowser Application Code Snippets
  • 20. String Constants <resources> ... <string name=”kurt_godel_wiki_url”> </string> ... </resources>
  • 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);
  • 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 launcher
  • 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 TTS
  • 24. SimpleTTSDemo Code Snippets
  • 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);
  • 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)); } }}
  • 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(); }
  • 28. Using Application Objects for Inter-Activity Data Transfers
  • 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 objects
  • 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 transfers
  • 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 conversion
  • 32. ImageDisplayerApp Code Snippets
  • 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; } }
  • 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>
  • 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(); }
  • 36. References ● ●
  • 37. Feedback Bugs to vladimir dot kulyukin at gmail dot com