Android Intents: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 101: Line 101:
===Fixing===
===Fixing===
This failed to work so rather than following the instructor I googled my way so I might reuse this approach next time
This failed to work so rather than following the instructor I googled my way so I might reuse this approach next time
*Googled NoteIntents
====Initial====
This led me to
I google the documentation for NoteIntents and arrived on https://developers.google.com/android/reference/com/google/android/gms/actions/NoteIntents. There is no mention of a library required
#1 https://developers.google.com/android/reference/com/google/android/gms/actions/NoteIntents
<br>
<br>
No mention of play services
Next Stack at https://stackoverflow.com/questions/50145470/how-do-i-use-noteintents shows this did show play services was required and then a light bulb moment, I pressed F12 lead me to the NoteIntents.class which gives
#2 Was https://stackoverflow.com/questions/50145470/how-do-i-use-noteintents
<br>
Strong indication of having to include play services.
<br>
But eventually I decided to read the code. So pressing F12 lead me to the NoteIntents.class which gives
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
public class NoteIntents {
public class NoteIntents {
Line 116: Line 110:
     public static final String ACTION_CREATE_NOTE = "com.google.android.gms.actions.CREATE_NOTE";
     public static final String ACTION_CREATE_NOTE = "com.google.android.gms.actions.CREATE_NOTE";
...
...
</syntaxhighlight>
====Packager====
So with this install the error was now that the packer is null
<syntaxhighlight lang="kotlin">
if(intent.resolveActivity(packageManager) !=null)
</syntaxhighlight>
</syntaxhighlight>
<br>
<br>
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.
This led me to move off my emulator, genymotion which does not use playservices. So I installed play services on the android emulator and install '''Keep Notes''' because this is said to work with Noteintents. I had another go with no joy. I read that SDK 30 means you need to do something different https://developer.android.com/about/versions/11/privacy/package-visibility so I lowered the emulator to 29 and install play services. But no joy.
<br>
====Light Bulb #2====
But still no joy.
Well I read above
<syntaxhighlight lang="kotlin">
val chooser = Intent.createChooser(myIntent, title)
</syntaxhighlight>
So I implemented a chooser and still no prompt using
<syntaxhighlight lang="kotlin">
        val intent = Intent(NoteIntents.ACTION_CREATE_NOTE).apply {
            putExtra(NoteIntents.EXTRA_NAME, "My Subject")
            putExtra(NoteIntents.EXTRA_TEXT, "Iain you found it")
            type = "text/plain"
        }
</syntaxhighlight>
So I removed the type and finally it said '''no apps can perform this action'''. So adding the type back in showed nothing.
====Resolution====
To me this said that there are no clients which supports "text/plain" so I googled how make a client and basically implemented an app with
<syntaxhighlight lang="xml">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.TestNoteIntent.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.google.android.gms.actions.CREATE_NOTE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
            </intent-filter>
        </activity>
 
</syntaxhighlight>
And in the Activity
<syntaxhighlight lang="kotlin">
        when {
            intent?.action == NoteIntents.ACTION_CREATE_NOTE -> {
                if ("text/plain" == intent.type) {
                    intent.getStringExtra(NoteIntents.EXTRA_TEXT)?.let {
                        Toast.makeText(this,it,
                                Toast.LENGTH_LONG).show();
                    }
                }
                else {
                    Toast.makeText(this, "We go this ok 1!",
                            Toast.LENGTH_LONG).show();
                }
...
</syntaxhighlight>
This finally produced a prompt for '''both''' my Test app and Keep Notes. Using the test app all went well. I could not get Keep Notes to work

Revision as of 22:33, 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)
    }

Fixing

This failed to work so rather than following the instructor I googled my way so I might reuse this approach next time

Initial

I google the documentation for NoteIntents and arrived on https://developers.google.com/android/reference/com/google/android/gms/actions/NoteIntents. There is no mention of a library required
Next Stack at https://stackoverflow.com/questions/50145470/how-do-i-use-noteintents shows this did show play services was required and then a light bulb moment, I pressed 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";
...

Packager

So with this install the error was now that the packer is null

if(intent.resolveActivity(packageManager) !=null)


This led me to move off my emulator, genymotion which does not use playservices. So I installed play services on the android emulator and install Keep Notes because this is said to work with Noteintents. I had another go with no joy. I read that SDK 30 means you need to do something different https://developer.android.com/about/versions/11/privacy/package-visibility so I lowered the emulator to 29 and install play services. But no joy.

Light Bulb #2

Well I read above

val chooser = Intent.createChooser(myIntent, title)

So I implemented a chooser and still no prompt using

        val intent = Intent(NoteIntents.ACTION_CREATE_NOTE).apply {
            putExtra(NoteIntents.EXTRA_NAME, "My Subject")
            putExtra(NoteIntents.EXTRA_TEXT, "Iain you found it")
            type = "text/plain"
        }

So I removed the type and finally it said no apps can perform this action. So adding the type back in showed nothing.

Resolution

To me this said that there are no clients which supports "text/plain" so I googled how make a client and basically implemented an app with

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.TestNoteIntent.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.google.android.gms.actions.CREATE_NOTE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
            </intent-filter>
        </activity>

And in the Activity

        when {
            intent?.action == NoteIntents.ACTION_CREATE_NOTE -> {
                if ("text/plain" == intent.type) {
                    intent.getStringExtra(NoteIntents.EXTRA_TEXT)?.let {
                        Toast.makeText(this,it,
                                Toast.LENGTH_LONG).show();
                    }
                }
                else {
                    Toast.makeText(this, "We go this ok 1!",
                            Toast.LENGTH_LONG).show();
                }
...

This finally produced a prompt for both my Test app and Keep Notes. Using the test app all went well. I could not get Keep Notes to work