Repository navigation
Intent
Intents are asynchronous messages which allow application components to request functionality from other Android components. Intents allow you to interact with components from the same applications as well as with components contributed by other applications. For example, an activity can start an external activity for taking a picture or can start another activity in the same app.
An intent can contain data via a Bundle. This data can be used by the receiving component.
Strating a sample activity: (Data is optional)
var i = Intent(this, ActivityTwo::class.java)
i.putExtra("KEY_1", "This value one for ActivityTwo ")
i.putExtra("KEY_2", "This value two ActivityTwo")
startActivity(i)Get data on other activity:
var extras = intent.getExtras()
var value1: String = extras.getString("KEY_1")Implicit Intents do not directly specify the Android components which should be called, it only specifies the action to be performed.A Uri can be used with the implicit intent to specify the data type. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.
Explicit intents are used in the application itself wherein one activity can switch to other activity. You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start.
The primary purpose is to grant permission to a foreign app to use the contained intent as if it's executed from app's own process. It's a wrapper around an Intent object. Use cases:
- Intent executed to handle Notification or AppWidget Actions
- Intent executed at a specified future time, like system's Alarm manager
Sometimes you want to determine if a component has registered for an intent. For example, you want to check if a certain intent receiver is available and in case a component is available, you enable a functionality in your application.
fun isIntentAvailable(ctx:Context, intent:Intent):Boolean {
val mgr = ctx.getPackageManager()
val list = mgr.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY)
return list.size() > 0
}Intents can be used to send broadcast messages into the Android system. A broadcast receiver can register to an event and is notified if such an event is sent. Your application can register to system events, e.g., a new email has arrived, system boot is complete or a phone call is received and react accordingly.
For this start activity as:
startActivityForResult(i, REQUEST_CODE)If the sub-activity is finished, it can send data back to its caller via an Intent. This is done in the finish() method.
override fun finish() {
// Prepare data intent
val data = Intent()
data.putExtra("returnKey1", "Swinging on a star. ")
data.putExtra("returnKey2", "You could be better then you are. ")
// Activity finished ok, return the data
setResult(RESULT_OK, data)
super.finish()
}Once the sub-activity finishes, the onActivityResult() method in the calling activity is called.
override protected fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE)
{
if (data.hasExtra("returnKey1"))
{
val value1 = data.getExtras().getString("returnKey1")
}
}
}If a component does not define an intent filter, it can only be called by explicit intents.
<activity android:name=".BrowserActivitiy"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"/>
</intent-filter>
</activity><activity
android:name=".ActivityTest"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>Some Actions and How to achieve that:
| Action | How to |
|---|---|
| Open Browser | Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")) |
| Dial | Intent(Intent.ACTION_DIAL, Uri.parse("tel:(+65)12345789")) |
| Show Map | Intent(Intent.ACTION_VIEW, Uri.parse("geo:11.123,7.000?z=2")) |
| Search on Map | Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=query")) |
| Take Picture | Intent("android.media.action.IMAGE_CAPTURE") |
| Show Contacts | Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/")) |
| Edit First Contact | Intent(Intent.ACTION_EDIT, Uri.parse("content://contacts/people/1")) |