Create Shortcut intent

Post your questions and help other users.

Moderator: Martin

Post Reply
bogdyro
Posts: 241
Joined: 04 Apr 2015 15:14

Create Shortcut intent

Post by bogdyro » 25 Feb 2017 18:18

Hi. Could someone pls tell me how I can use the broadcast intent action to create a new Shortcut in the default Android launcher? Thx

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Create Shortcut intent

Post by Martin » 26 Feb 2017 22:15

Hi,

Here's a link to a possible way using regular Java:
https://gist.github.com/zeuxisoo/1008973

You probably have to do it using the Java functions of action Script since adding the extra EXTRA_SHORTCUT_ICON_RESOURCE is not supported in Automagic.

Regards,
Martin

bogdyro
Posts: 241
Joined: 04 Apr 2015 15:14

Re: Create Shortcut intent

Post by bogdyro » 01 Mar 2017 06:44

Hi Martin, thx. It's getting too complicated. Could U guide me through the process pls ?
So

intent=callJavaConstructor("android.content.Intent", "Intent(java.lang.String)","com.android.launcher.action.INSTALL_SHORTCUT"); //should it not be android.intent.action.CREATE_SHORTCUT ?
callJavaMethod(intent, "android.content.Intent", "putExtra(java.lang.String, java.lang.String)", 'EXTRA_SHORTCUT_NAME', 'MyShortcut');

//this is where it gets shady; the component name should be the app to which i'm sending the broadcast to or AUTOMAGIC ?
compName=callJavaConstructor("android.content.ComponentName", "ComponentName(java.lang.String, java.lang.String)", 'com.google.android.apps.nexuslauncher', 'com.android.launcher3.InstallShortcutReceiver');

callJavaMethod(intent, "android.content.Intent", "putExtra(java.lang.String, java.lang.String)", 'EXTRA_SHORTCUT_NAME', '_bogdyro');
NewIntent = callJavaConstructor("android.content.Intent", "Intent(java.lang.String)","ACTION_MAIN");
callJavaMethod(intent, "android.content.Intent", "putExtra(java.lang.String, java.lang.String)", 'EXTRA_SHORTCUT_NAME', NewIntent);


compName=callJavaConstructor("android.content.ComponentName", "ComponentName(java.lang.String, java.lang.String)", 'com.google.android.apps.nexuslauncher', 'com.android.launcher3.InstallShortcutReceiver');

//next java method call should have two parameters, string and Intent; I can't find this method
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));


//how do I pass the path to an .ico or .png file to create the shortcut icon ?
callJavaMethod(intent, "android.content.Intent", "putExtra(java.lang.String, java.lang.String)", 'EXTRA_SHORTCUT_INTENT','/storage/emulated/0/Download/ios7/TTF.ico');

//send broadcast
callJavaMethod(getContext(), "android.content.Context", "sendBroadcast(android.content.Intent)", intent);

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Create Shortcut intent

Post by Martin » 02 Mar 2017 21:13

Hi,

Please check the following flow:
http://automagic4android.com/flow.php?i ... 4344a05a41

The bitmap is loaded using the action Init Variable Image File. Please note that the image should not be too large, otherwise it can not be added to the intent, respectively the intent can not be sent. I've used an app icon since the size should be OK to use it as a shortcut icon.

The variable intentToStart should be set to an activity intent that directly launches an activity and to which both Automagic and the Launcher have the permission to start. The launcher verifies that the intent is valid and will silently discard the shortcut when the intent is not valid for some reason.

The extra names EXTRA_SHORTCUT_NAME etc. have to be replaced by the actual String values (e.g. "android.intent.extra.shortcut.NAME") (lines 5, 6, 7 in the Script).

Regards,
Martin

bogdyro
Posts: 241
Joined: 04 Apr 2015 15:14

Re: Create Shortcut intent

Post by bogdyro » 03 Mar 2017 18:56

Sorry to bug you. Tried it but it doesn't work. Isn't there a way to debug this intent? There is an app that 8ntercepts intents but it doesn't get triggered.
Anything else I could try? Thanks

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Create Shortcut intent

Post by Martin » 05 Mar 2017 13:11

Hmmm. No error shown in the log of Automagic?
Maybe the image could not be loaded. Try to use http://automagic4android.com/logo.png as the image in action Init Variable Image File.

Android is also very picky about the intent to start. Try to change the intent to launch the system settings, which should work on most devices.
To do this, replace the first two lines of the script with:

Code: Select all

intentToStart=callJavaConstructor("android.content.Intent", "Intent(java.lang.String)", "android.settings.SETTINGS");
Also check the android system log. My device logs an error when the intent to start is not valid that reads: E/InstallShortcutReceiver: Ignoring malicious intent #Intent;action=...;end

Regards,
Martin

bogdyro
Posts: 241
Joined: 04 Apr 2015 15:14

Re: Create Shortcut intent

Post by bogdyro » 07 Mar 2017 17:33

Hi Martin. It just started working out of the blue. Only needed to change the activity name. Anywyay,no error in automagic log or catlog when it wasn't working. Strange.
Now I would like to make a shortcut to an AM trigger. Is that possible via script.
Also, as a general request, do you think it's doable to edit every java action in a script by opening that helper dialog?
Thanks

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Create Shortcut intent

Post by Martin » 07 Mar 2017 21:39

Android does not notify apps about the problem when the intent is not valid. Android just ignores the intent and writes one error log to the system log of Android, not the log of Automagic. Glad it's working now :-)

You could use the trigger Send/Share Intent Received to execute a flow. You have to change the first few lines of the script to create a SEND-intent like this:

Code: Select all

intentToStart=callJavaConstructor("android.content.Intent", "Intent(java.lang.String)", "android.intent.action.SEND");
callJavaMethod(intentToStart, "android.content.Intent", "setClassName(java.lang.String, java.lang.String)", "ch.gridvision.ppam.androidautomagic", "ch.gridvision.ppam.androidautomagic.SendIntentReceiverActivity");
Regards,
Martin

bogdyro
Posts: 241
Joined: 04 Apr 2015 15:14

Re: Create Shortcut intent

Post by bogdyro » 08 Mar 2017 05:20

Well, I was talking about logcat. Isn't that the mother of all android logs ? Couldn't find any error there either. Anyway, thx for the second snippet of code.

Post Reply