Android Intents: Difference between revisions
Line 47: | Line 47: | ||
*Sample Code | *Sample Code | ||
*Pass the appropriate Parameter | *Pass the appropriate Parameter | ||
==Working Example== | ==Working Example 1 SET_ALARM== | ||
This creates an alarm for Mon-Fri at 17:00. | This creates an alarm for Mon-Fri at 17:00. | ||
*Set Permissions | *Set Permissions | ||
Line 77: | Line 77: | ||
startActivity(intent) | startActivity(intent) | ||
} | } | ||
</syntaxhighlight> | |||
==Working Example 2 CREATE_NOTE== | |||
It seems that the documentation is a bit poor around the intents. So I thought it wise just to see how it worked for this for me | |||
*Set Permissions | |||
*Implementation | |||
===Set Permissions=== | |||
<syntaxhighlight lang="kotlin"> | |||
</syntaxhighlight> | |||
===Implementation=== | |||
This is the documentation at the time | |||
<syntaxhighlight lang="kotlin"> | |||
val intent = Intent(NoteIntents.ACTION_CREATE_NOTE).apply { | |||
putExtra(NoteIntents.EXTRA_NAME, "test subject") | |||
putExtra(NoteIntents.EXTRA_TEXT, "text") | |||
} | |||
if (intent.resolveActivity(packageManager) != null) { | |||
startActivity(intent) | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 05:24, 27 January 2021
Introduction
Intents
There are two types of intents
- Explicit
- Implicit
Explicit
We can start an explicit intent with
val intent = Intent(this.MyActivityClass::class.java_
startActivity(intent)
Implicit
No destination intent is defined. The user will be prompted for which application to use. Not the use of the apply operator.
val intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT,"Hello World")
type = "text/plain"
}
startActivity(intent)
Quite nice compared with the code without the apply.
val intent = Intent()
intent.action = Intent.ACTION_SEND
intent.putExtra(Intent.EXTRA_TEXT,"Hello World")
intent.type = "text/plain"
startActivity(intent)
Implicit With Choice
Android looks at the action, and prompts the user for all app which handle this.The user can make their choice a default however we can override this and force a choice. Notice we should always check for a valid intent or the app will crash
val chooser = Intent.createChooser)myIntent, title)
if(intent.resolveActivity(packageManager) !=null) {
startActivity(chooser)
} else {
Log.d(...)
}
Common Intents
What is Required
For common intents we need to go to https://developer.android.com/guide/components/intents-common#Clock and look at what is required this includes
- Action Type
- Permissions
- Sample Code
- Pass the appropriate Parameter
Working Example 1 SET_ALARM
This creates an alarm for Mon-Fri at 17:00.
- Set Permissions
- Implementation
Set Permissions
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
Implementation
val intent = Intent(AlarmClock.ACTION_SET_ALARM).apply {
putExtra(AlarmClock.EXTRA_MESSAGE, "My Great Alarm")
putExtra(AlarmClock.EXTRA_HOUR, 17)
putExtra(AlarmClock.EXTRA_MINUTES, 0)
putExtra(
AlarmClock.EXTRA_DAYS,
arrayOf(
java.util.Calendar.MONDAY,
java.util.Calendar.TUESDAY,
java.util.Calendar.WEDNESDAY,
java.util.Calendar.THURSDAY,
java.util.Calendar.FRIDAY
)
)
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
Working Example 2 CREATE_NOTE
It seems that the documentation is a bit poor around the intents. So I thought it wise just to see how it worked for this for me
- Set Permissions
- Implementation
Set Permissions
Implementation
This is the documentation at the time
val intent = Intent(NoteIntents.ACTION_CREATE_NOTE).apply {
putExtra(NoteIntents.EXTRA_NAME, "test subject")
putExtra(NoteIntents.EXTRA_TEXT, "text")
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}