Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Similar presentations


Presentation on theme: "Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1."— Presentation transcript:

1 Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

2 » An object that provides runtime binding between separate components (such as two activities) » The Intent represents an app’s "intent to do something.“ ˃You can use intents for a wide variety of tasks, but most often they’re used to start another activity. » Types of Intent ˃Explicit ˃Implicit 2 © Sekolah Tinggi Teknik Surabaya

3 3

4 Intent intent = new Intent( theContext, theActivityClass); » theContext : Who owns the Intent » TheActivityClass : to which the system should deliver the Intent (in this case, the activity that should be started) 4 © Sekolah Tinggi Teknik Surabaya

5 » Intent may contain data to be passed to other Activity. intent.putExtra( "KEY_STRING", message ); » Best practice: use static constant to define "KEY_STRING" 5 © Sekolah Tinggi Teknik Surabaya

6 » Send more data as an object using bundle Bundle myBundle = new Bundle(); myBundle.putInt("val1", 123); intent.putExtras(myBundle); 6 © Sekolah Tinggi Teknik Surabaya

7 startActivity(intent); » Based on Intent object that was already instantiated 7 © Sekolah Tinggi Teknik Surabaya

8 » Every Activity is invoked by an Intent, regardless of how the user navigated there. » You can get the Intent that started your activity by calling getIntent() and retrieve the data contained within it. Intent intent = getIntent(); String message = intent.getStringExtra( MainActivity.CONSTANT_NAME ); 8 © Sekolah Tinggi Teknik Surabaya

9 Bundle myBundle = intent.getExtras(); int x = myBundle.getint("val1"); 9 © Sekolah Tinggi Teknik Surabaya

10 10

11 » It does not specify the app component to start, but instead specifies an action and provides some data with which to perform the action. » The system resolves the intent to an app that can handle the intent and starts its corresponding Activity. » If there's more than one app that can handle the intent, the system presents the user with a dialog to pick which app to use. 11 © Sekolah Tinggi Teknik Surabaya

12 » If there are no apps on the device that can receive the implicit intent, your app will crash when it calls startActivity(). ˃ Use resolveActivity() +If the result is non-null, there is at least one app that can handle the intent 12 © Sekolah Tinggi Teknik Surabaya

13 » Alarm Clock Alarm Clock » Calendar Calendar » Camera Camera » Contacts/People App Contacts/People App » Email Email » File Storage File Storage » Maps Maps » Music or Video Music or Video » Phone Phone » Settings Settings » Text Messaging Text Messaging » Web Browser Web Browser 13 © Sekolah Tinggi Teknik Surabaya

14 Intent common = new Intent(action, data); 14 © Sekolah Tinggi Teknik Surabaya

15 ActionData ACTION_VIEWUri.parse("http://www.google.com") ACTION_WEB_SEARCHUri.parse("http://www.google.com") ACTION_DIALUri.parse("tel:555-555-5555") ACTION_CALLUri.parse("tel:555-555-5555") ACTION_VIEWUri.parse("geo:0,0?z=4&q=business+near+city“) ACTION_SENDTOUri.parse("sms:5551234") 15 © Sekolah Tinggi Teknik Surabaya Some actions require permissions. Refer to http://developer.android.com/reference/android/Manifest.permission.html

16 © Sekolah Tinggi Teknik Surabaya 16

17 © Sekolah Tinggi Teknik Surabaya 17

18 » Before an activity exits, it can call setResult(resultCode) to return a termination signal back to its parent. » Always supply a result code, which can be the standard results Activity.RESULT_CANCELED, Activity.RESULT_OK, or any custom values. » If a child activity fails for any reason (such as crashing), the parent activity will receive a result with the code RESULT_CANCELED. 18 © Sekolah Tinggi Teknik Surabaya

19 » Starting Another Activity, https://developer.android.com/training /basics/firstapp/starting-activity.html https://developer.android.com/training /basics/firstapp/starting-activity.html » Common Intents, http://developer.android.com/guide/co mponents/intents-common.html http://developer.android.com/guide/co mponents/intents-common.html 19 © Sekolah Tinggi Teknik Surabaya


Download ppt "Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1."

Similar presentations


Ads by Google