1 / 12

Intents

Intents. Intents. Message passing mechanism Most common uses: starting an Activity (open an email, contact, etc.)   starting an Activity for a result (scan a barcode, take a picture to attach to an email, etc .). Intents. Explicit

aysel
Download Presentation

Intents

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intents CS440

  2. Intents • Message passing mechanism • Most common uses: • starting an Activity (open an email, contact, etc.)  •  starting an Activity for a result (scan a barcode, take a picture to attach to an email, etc.) CS440

  3. Intents • Explicit Intent i = new Intent(this, ActivityTwo.class); i.putExtra("Value1", "This value one for ActivityTwo "); i.putExtra("Value2", "This value two ActivityTwo"); • Implicit Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.vogella.com")); startActivity(i); CS440

  4. Example: Share Intent • SenderIntent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(android.content.Intent.EXTRA_TEXT, "News for you!"); startActivity(intent); • Receiver Bundle extras = getIntent().getExtras(); if(extras == null) { return; } // Get data via the key String value1 = extras.getString(Intent.EXTRA_TEXT); if(value1 != null) { // Do something with the data } CS440

  5. Calling Sub-Activities for result data • If you need some information from the called Activity use thestartActivityForResult() method. publicvoidonClick(View view) { Intent i = new Intent(this, ActivityTwo.class); i.putExtra("Value1", "This value one for ActivityTwo "); i.putExtra("Value2", "This value two ActivityTwo"); // Set the request code to any code you like, you can identify the// callback via this code startActivityForResult(i, REQUEST_CODE); } CS440

  6. Calling Sub-Activities for result data • If you use the startActivityForResult() method then the started Activity is called a Sub-Activity. • If the Sub-Activity is finished it can send data back to its caller via Intent. This is done in the finish() method. @Override publicvoid finish() { // Prepare data intent Intent data = new Intent(); data.putExtra("returnKey1", "Swinging on a star. "); data.putExtra("returnKey2", "You could be better then you are. "); // Activity finished ok, return the data setResult(RESULT_OK, data); super.finish(); } CS440

  7. Calling Sub-Activities for result data • Once the Sub-Activity finished, the onActivityResult() method in the calling Activity will be called. @Override protectedvoidonActivityResult(intrequestCode, intresultCode, Intent data) { if(resultCode == RESULT_OK && requestCode == REQUEST_CODE) { if(data.hasExtra("returnKey1")) { Toast.makeText(this, data.getExtras().getString("returnKey1"), Toast.LENGTH_SHORT).show(); } } } CS440

  8. Intents • Intents use action strings and URIs: • Action Strings Example: • ACTION_VIEWcontent://contacts/people/1 -- Display information about the person whose identifier is "1“ • ACTION_DIALcontent://contacts/people/1 -- Display the phone dialer with the person filled in. • ACTION_DIALtel:123 -- Display the phone dialer with the given number filled in. • URI Example: • Search Google Maps: geo:0,0?q=query • Show contacts: content://contacts/people • Show a URL: http://www.google.com  CS440

  9. Intent Fun • Create a new Project: IntentFun • Create an intent that has data one of the following URIs: • http://www.google.com • content://contacts/people • geo:0,0?q=query • Do not forget to start an Activity for this intent! CS440

  10. Intent Fun • Create an intent that takes a picture: • Use: "android.media.action.IMAGE_CAPTURE“ as an argument in the intent • Start an activity that is based on the result of the intent • Do not forget: you need to override the onActivityResult method… why? CS440

  11. Intent Fun • Create a Toast with text “lalala”: • What is a Toast class? • Make the text appear on your screen • Check the open intents registry: • http://www.openintents.org/en/intentstable CS440

  12. References • http://www.damonkohler.com/2009/02/android-recipes.html • http://developer.android.com CS440

More Related