Intents 18 December 2018 Lecture 4 18 Dec 2018 SE 435: Development in the Android Environment 1
Topics for Today Building an Intent Explicit Implicit Other features: Data Result Sources: developer.android.com 18 Dec 2018 SE 435: Development in the Android Environment 2
Building an Intent Can add fields such as: Action: what do you want to happen Ex. ACTION_VIEW, ACTION_SEND URI: Data URI to examine or operate on Ex. content://com.android.email.attachmentprovider/1/62/raw Class: Which class should receive the intent Explicit intents: Have a Class Implicit intents: No Class 18 Dec 2018 SE 435: Development in the Android Environment 3
Sending an Explicit Intent public void sendexplicitnodata(view view) { // create an explicit intent to send Intent intent = new Intent(getBaseContext(), ExplicitActivityOne.class); startactivity(intent); } 1. The intent is sent to Android OS 2. The sender loses control of the screen 3. The intent is sent to the ExplicitActivityOne 4. ExplicitActivityOne starts to run with its lifecycle 18 Dec 2018 SE 435: Development in the Android Environment 4
Sending an Implicit Intent public void sendimplicitnodata(view view) { // create an explicit intent to send Intent intent = new Intent(Intent.ACTION_DIAL); startactivity(intent); } 1. Intent is sent with the desired action (dial) 2. The sender loses control of the screen 3. Android OS searches which apps can handle ACTION_DIAL 4. The chosen app starts to run with its lifecycle 18 Dec 2018 SE 435: Development in the Android Environment 5
Result 18 Dec 2018 SE 435: Development in the Android Environment 6
So Far Building an Intent Explicit Implicit Other features: Data Result 18 Dec 2018 SE 435: Development in the Android Environment 7
Adding Data public void sendimplicitdata (View view) { // create an implicit intent to send with data Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:97246653793")); startactivity(intent); } Added a data field to the Intent The scheme (tel:) indicates it s a telephone number 18 Dec 2018 SE 435: Development in the Android Environment 8
Result 18 Dec 2018 SE 435: Development in the Android Environment 9
Adding Extras public void sendimplicitextras (View view) { } // create an implicit intent to send with data Intent intent = new Intent(Intent.ACTION_SEND); intent.settype("text/rfc822"); intent.putextra(intent.extra_email, new String[]{"mjmay@kinneret.ac.il"}); intent.putextra(intent.extra_cc, new String[]{"yaely@kinneret.ac.il"}); intent.putextra(intent.extra_subject, "An implicit intent with extras"); intent.putextra(intent.extra_text, "This is the body of my message!"); startactivity(intent); 18 Dec 2018 SE 435: Development in the Android Environment 10
Sending and Routing MyApp Intent Action ACTION.SEND MIME type Rfc822 EXTRA EMAIL String[]{"mjmay@kinneret.ac.il"} EXTRA CC String[]{ yaely@kinneret.ac.il"} EXTRA SUBJECT "An implicit intent with extras" EXTRA TEXT "This is the body of my message!" GMail Binder Intent App Action MIME Type Category SEND text/rfc822 DEFAULT GMail SEND text/plain DEFAULT Reader VIEW application/pdf DEFAULT 18 Dec 2018 SE 435: Development in the Android Environment 11
Result 18 Dec 2018 SE 435: Development in the Android Environment 12
Receiving an Intent (1/2) public void sendexplicitdata (View view) { // create an explicit intent to send with // data Intent intent = new Intent(getBaseContext(), ExplicitActivityTwo.class); intent.putextra( ExplicitActivityTwo.EXTRA_NUMBERS, new int[]{25,72,43}); } startactivity(intent); 18 Dec 2018 SE 435: Development in the Android Environment 13
Receiving an Intent (2/2) public class ExplicitActivityTwo extends Activity { } public static final String EXTRA_NUMBERS = "il.ac.kinneret.mjmay.extra_numbers"; @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview( R.layout.activity_explicit_activity_two); Intent intent = getintent(); TextView tv = (TextView) findviewbyid(r.id.messageview); int[] values = intent.getextras().getintarray(extra_numbers); tv.settext(""); for (int i : values) { } tv.append(string.valueof(i) + "\n"); 18 Dec 2018 SE 435: Development in the Android Environment 14
Result 18 Dec 2018 SE 435: Development in the Android Environment 15
Sending an Intent for Result startactivityforresult (intent, mycode) 18 Dec 2018 SE 435: Development in the Android Environment 16
Sending an Intent for Result startactivityforresult (intent, mycode) OK? setresult(result_ok, returnintent) BAD? setresult(result_canceled, returnintent) finish() 18 Dec 2018 SE 435: Development in the Android Environment 17
Sending an Intent for Result startactivityforresult (intent, mycode) OK? setresult(result_ok, returnintent) BAD? setresult(result_canceled, returnintent) onactivityresult() with: mycode RESULT_OK/RESULT_CANCELED returnintent finish() 18 Dec 2018 SE 435: Development in the Android Environment 18
Conclusion Building an Intent Explicit Implicit Other features: Data Result 18 Dec 2018 SE 435: Development in the Android Environment 19