Android Intents: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 83: Line 83:
*Implementation
*Implementation
===Set Permissions===
===Set Permissions===
None specified
<syntaxhighlight lang="kotlin">
<syntaxhighlight lang="kotlin">



Revision as of 05:25, 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

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)
    }