Lecture 1 Introduction Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
-
Upload
hilda-richardson -
Category
Documents
-
view
214 -
download
2
Transcript of Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
AndroidIntent
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
Explicit Intent© Sekolah Tinggi Teknik Surabaya
3
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
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
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
Starting ActivitystartActivity(intent);
» Based on Intent object that was already instantiated
7
© Sekolah Tinggi Teknik Surabaya
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
Receive the Intent - Bundle
Bundle myBundle = intent.getExtras();
int x = myBundle.getint("val1");
9
© Sekolah Tinggi Teknik Surabaya
Implicit Intent© Sekolah Tinggi Teknik Surabaya
10
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
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
Common Intents» Alarm Clock» Calendar» Camera» Contacts/People App» Email» File Storage» Maps» Music or Video» Phone» Settings» Text Messaging» Web Browser
13
© Sekolah Tinggi Teknik Surabaya
Instantiate Common Intents
Intent common = new Intent(action, data);
14
© Sekolah Tinggi Teknik Surabaya
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
Starting Activity and Getting Result© Sekolah Tinggi Teknik Surabaya
16
© Sekolah Tinggi Teknik Surabaya
17
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
References» Starting Another Activity, https://
developer.android.com/training/basics/firstapp/starting-activity.html
» Common Intents, http://developer.android.com/guide/components/intents-common.html
19
© Sekolah Tinggi Teknik Surabaya