the intent class · using intents for operations to be performed i.e., using intents to start a...
TRANSCRIPT
![Page 1: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/1.jpg)
![Page 2: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/2.jpg)
![Page 3: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/3.jpg)
The Intent Class
Starting Activities with IntentsExplicit Activation
Implicit Activation via Intent resolution
![Page 4: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/4.jpg)
A data structure that representsAn operation to be performed, or
An event that has occurred
![Page 5: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/5.jpg)
Using Intents for operations to be performedi.e., using Intents to start a single activity
We’ll cover using Intents for event notification when we talk about BroadcastReceivers
![Page 6: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/6.jpg)
Intents provide a flexible “language” for specifying operations to be performed
e.g., I want to pick a contact, take a photo, dial a phone number, etc.
![Page 7: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/7.jpg)
An Intent is constructed by one component that wants some work done
It is delivered to another component that offers to perform that work
![Page 8: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/8.jpg)
Action
Data
Category
Type
Component
Extras
Flags
![Page 9: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/9.jpg)
String representing the desired operation
![Page 10: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/10.jpg)
ACTION_DIAL – Dial a number
ACTION_EDIT – Display data to edit
ACTION_SYNC – Synchronize device data with a server
ACTION_MAIN – Start as initial activity of app
![Page 11: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/11.jpg)
val newIntent = Intent(Intent.ACTION_DIAL)
Or
val newIntent = Intent()newIntent.action = Intent.ACTION_DIAL
![Page 12: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/12.jpg)
Data associated with the Intent
Formatted as a Uniform Resource Identifier (URI)
![Page 13: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/13.jpg)
Data to view on a mapUri.parse(“geo:0,0?q=1600+Pennsylvania
+Ave+Washington+DC”)
Number to dial in the phone dialerUri.parse(“tel:+15555555555”)
![Page 14: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/14.jpg)
val intent= Intent (Intent.ACTION_DIAL, Uri.parse("tel:+15555555555"))
Or
val intent = Intent(Intent.ACTION_DIAL)intent.data = Uri.parse("tel:+15555555555")
![Page 15: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/15.jpg)
Additional information about the components that are allowed to handle the Intent
![Page 16: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/16.jpg)
CATEGORY_BROWSABLE – Activity can be invoked to display data ref’s by a URI
CATEGORY_LAUNCHER – can be the initial Activity of a task and is listed in top-level app launcher
![Page 17: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/17.jpg)
Specifies an explicit MIME type of the Intent data
Examplesimage/*, image/png, image/jpeg
text/html, text/plain
If unspecified, Android will infer the type
![Page 18: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/18.jpg)
The component that should receive this Intent
Use this when there’s exactly one named component that should receive the intent
![Page 19: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/19.jpg)
val intent = Intent(packageContext: Context!, cls: Class<*>!
![Page 20: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/20.jpg)
Or
Intent intent = new Intent ();
and one of:
setComponent(), setClass(), or setClassName()
![Page 21: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/21.jpg)
Additional information associated with Intent
Treated as a map (key-value pairs)
![Page 22: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/22.jpg)
val intent = Intent(Intent.ACTION_SEND)intent.putExtra(Intent.EXTRA_EMAIL,
arrayOf(“[email protected]”, “[email protected]”,“[email protected]”, “[email protected]”))
![Page 23: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/23.jpg)
Several forms depending on data typeputExtra(name: String!, value: String?);
putExtra(name: String!, value: FloatArray?);
…
![Page 24: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/24.jpg)
Specify how Intent should be handled
![Page 25: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/25.jpg)
FLAG_ACTIVITY_NO_HISTORYDon’t put this Activity in the History stack
FLAG_DEBUG_LOG_RESOLUTIONPrint extra logging information when this Intent is processed
![Page 26: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/26.jpg)
val intent = Intent(Intent.ACTION_SEND)intent.flags = Intent.FLAG_ACTIVITY_NO_HISTORY
![Page 27: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/27.jpg)
fun startActivity(intent: Intent!): Unit
fun startActivityForResult(intent: Intent!, requestCode: Int): Unit
![Page 28: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/28.jpg)
Can be named explicitly by setting the Intent’s component
Otherwise, it is determined implicitly
![Page 29: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/29.jpg)
HelloWorldWithLogin
Two ActivitiesLoginActivity checks username & password and then starts HelloAndroidActivity
HelloAndroidActivity shows “Hello Android!” message
![Page 30: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/30.jpg)
HelloAndroidWithLogin
![Page 31: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/31.jpg)
fun onClick(v: View?) {if (/* authorized */) {// Create an explicit Intent for starting the // HelloAndroid Activity
val helloAndroidIntent = Intent(this@LoginScreen,HelloAndroid::class.java)
// Use the Intent to start the HelloAndroid ActivitystartActivity(helloAndroidIntent)
…
![Page 32: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/32.jpg)
When the Activity to be started is not explicitly named, Android tries to find Activities that match the Intent
This process is called Intent Resolution
![Page 33: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/33.jpg)
Intents describe desired operations
IntentFilters describe which operations a given Activity can handle
IntentFilters specified in AndroidManifest.xml or programmatically
![Page 34: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/34.jpg)
Action
Data (both URI & Type)
Category
![Page 35: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/35.jpg)
<activity …> …<intent-filter …>
…<action android:name=”actionName" />…
</intent-filter> …
</activity>
![Page 36: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/36.jpg)
<activity …> …<intent-filter …>
…<action android:name=”android.intent.action.DIAL" />…
</intent-filter> …
</activity>
![Page 37: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/37.jpg)
<intent-filter …> …<data
android:mimeType="string”android:scheme="string" android:host="string"android:port="string"android:path="string"android:pathPattern="string"android:pathPrefix="string"
/>…
</intent-filter>
![Page 38: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/38.jpg)
<intent-filter …> …
<data android:scheme=”geo" />…
</intent-filter>
![Page 39: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/39.jpg)
<intent-filter …>…
<category android:name="string" />…
</intent-filter>
![Page 40: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/40.jpg)
<intent-filter …><action android:name ="android.intent.action.VIEW" /><category android:name ="android.intent.category.DEFAULT" /><category android:name="android.intent.category.BROWSABLE”/><data android:scheme ="geo”/>
</intent-filter>
![Page 41: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/41.jpg)
Note: to receive implicit intents an Activity should specify an IntentFilter with the category
"android.intent.category.DEFAULT”
![Page 42: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/42.jpg)
android:priority – Priority given to the parent component when handling matching Intents
Causes Android to prefer one activity over another
-1000 <= priority <=1000
Higher values represent higher priorities
![Page 43: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/43.jpg)
The MapLocation app created an implicit Intent and then used it in a call to startActivity()
Should start a Maps app
What if the user has uninstalled the Maps app?
Your code should always check before attempting to start an Activity with an implicit Intent
![Page 44: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/44.jpg)
private fun processClick() {try {…// Create Intent object for starting Google Maps applicationval geoIntent = Intent(
Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=$address"))
if (packageManager.resolveActivity(geoIntent, 0) != null) {// Use the Intent to start Google Maps application //using Activity.startActivity()
startActivity(geoIntent)}…
![Page 45: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/45.jpg)
Implicit Intents can pose a security hazard
Prefer explicit Intents within your own app
Set the android:exported attribute to false in AndroidManifest.xml, if you don’t want other apps to start a given component in your app
![Page 46: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/46.jpg)
% adb shell dumpsys package
![Page 47: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/47.jpg)
![Page 48: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/48.jpg)
Permissions
![Page 49: The Intent Class · Using Intents for operations to be performed i.e., using Intents to start a single activity ... Prefer explicit Intents within your own app Set the android:exportedattribute](https://reader035.vdocuments.site/reader035/viewer/2022071103/5fdc8d6c1128ff112144ca45/html5/thumbnails/49.jpg)
HelloAndroidWithLogin