Intents 1 CS440
Intents Message passing mechanism Most common uses: starting an Activity (open an , contact, etc.) starting an Activity for a result (scan a barcode, take a picture to attach to an , etc.) CS440 2
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(" startActivity(i); CS440 3
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
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
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() 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
Calling Sub-Activities for result data Once the Sub-Activity finished, the onActivityResult() method in the calling Activity will be 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
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: 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: CS440 8
Intent Fun Create a new Project: IntentFun Create an intent that has data one of the following URIs: content://contacts/people geo:0,0?q=query Do not forget to start an Activity for this intent! CS440 9
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
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: CS440 11
References recipes.html recipes.html CS440 12