Download presentation
Presentation is loading. Please wait.
Published byEsmond Robbins Modified over 9 years ago
1
Intents 1 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 2
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 3
4
Example: Share Intent Sender Intent 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 4
5
Calling Sub-Activities for result data If you need some information from the called Activity use the startActivityForResult() method. public void onClick(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 5
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 public void 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 6
7
Calling Sub-Activities for result data Once the Sub-Activity finished, the onActivityResult() method in the calling Activity will be called. @Override protected void onActivityResult(int requestCode, int resultCode, 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 7
8
Intents Intents use action strings and URIs: Action Strings Example: ACTION_VIEW content://contacts/people/1 -- Display information about the person whose identifier is "1“ ACTION_VIEW ACTION_DIAL content://contacts/people/1 -- Display the phone dialer with the person filled in. ACTION_DIAL ACTION_DIAL tel:123 -- Display the phone dialer with the given number filled in. ACTION_DIAL URI Example: Search Google Maps: geo:0,0?q=query Show contacts: content://contacts/people Show a URL: http://www.google.com CS440 8
9
Intent Fun Create a new Project: IntentFun Create an intent that has data one of the following URIs: http://www.google.com http://www.google.com content://contacts/people geo:0,0?q=query Do not forget to start an Activity for this intent! CS440 9
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 10
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 http://www.openintents.org/en/intentstable CS440 11
12
References http://www.damonkohler.com/2009/02/android- recipes.html http://www.damonkohler.com/2009/02/android- recipes.html http://developer.android.com http://developer.android.com CS440 12
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.