Android Intents
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
None specified
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)
}
Fixing
This failed to work so rather than following the instructor I googled my way so I might reuse this approach next time
- Googled NoteIntents
This led me to
#1 https://developers.google.com/android/reference/com/google/android/gms/actions/NoteIntents
No mention of play services
#2 Was https://stackoverflow.com/questions/50145470/how-do-i-use-noteintents
Strong indication of having to include play services.
But eventually I decided to read the code. So pressing F12 lead me to the NoteIntents.class which gives
public class NoteIntents {
@RecentlyNonNull
public static final String ACTION_CREATE_NOTE = "com.google.android.gms.actions.CREATE_NOTE";
...
So then I googled the domain and got https://developers.google.com/android/reference/com/google/android/gms/actions/package-summary Now I know I need actions I add the only library with actions from https://developers.google.com/android/guides/setup#add_google_play_services_to_your_project.
But still no joy.