Android Intents: Difference between revisions
Jump to navigation
Jump to search
Line 38: | Line 38: | ||
Log.d(...) | Log.d(...) | ||
} | } | ||
</syntaxhighlight> | |||
=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== | |||
This creates an alarm for Mon-Fri at 17:00. | |||
*Set Permissions | |||
*Implementation | |||
===Set Permissions=== | |||
<syntaxhighlight lang="kotlin"> | |||
</syntaxhighlight> | |||
===Implementation=== | |||
<syntaxhighlight lang="kotlin"> | |||
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, arrayListOf( | |||
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) | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 03:45, 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
This creates an alarm for Mon-Fri at 17:00.
- Set Permissions
- Implementation
Set Permissions
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, arrayListOf(
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)
}