Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

19
Androi d Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

Transcript of Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Page 1: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

AndroidIntent

Erick Pranata

© Sekolah Tinggi Teknik Surabaya

1

Page 2: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Intent» 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

Page 3: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Explicit Intent© Sekolah Tinggi Teknik Surabaya

3

Page 4: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Instantiate IntentIntent 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

Page 5: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Bundle» 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

Page 6: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Bundle» Send more data as an object using

bundle

Bundle myBundle = new Bundle();myBundle.putInt("val1", 123);intent.putExtras(myBundle);

6

© Sekolah Tinggi Teknik Surabaya

Page 7: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Starting ActivitystartActivity(intent);

» Based on Intent object that was already instantiated

7

© Sekolah Tinggi Teknik Surabaya

Page 8: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Receive the Intent» 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

Page 9: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Receive the Intent - Bundle

Bundle myBundle = intent.getExtras();

int x = myBundle.getint("val1");

9

© Sekolah Tinggi Teknik Surabaya

Page 10: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Implicit Intent© Sekolah Tinggi Teknik Surabaya

10

Page 11: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Implicit Intent» 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

Page 12: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Caution!» 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

Page 14: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Instantiate Common Intents

Intent common = new Intent(action, data);

14

© Sekolah Tinggi Teknik Surabaya

Page 15: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Some Common IntentsAction Data

ACTION_VIEW Uri.parse("http://www.google.com")

ACTION_WEB_SEARCH Uri.parse("http://www.google.com")

ACTION_DIAL Uri.parse("tel:555-555-5555")

ACTION_CALL Uri.parse("tel:555-555-5555")

ACTION_VIEW Uri.parse("geo:0,0?z=4&q=business+near+city“)

ACTION_SENDTO Uri.parse("sms:5551234")

15

© Sekolah Tinggi Teknik Surabaya

Some actions require permissions. Refer to http://developer.android.com/reference/android/Manifest.permission.html

Page 16: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Starting Activity and Getting Result© Sekolah Tinggi Teknik Surabaya

16

Page 17: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

© Sekolah Tinggi Teknik Surabaya

17

Page 18: Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

From Recipient» 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