Issue Description
Currently it is not possible to create a launcher shortcut programmatically using the Titanium platform. Titanium allows for the creation of an Android intent using Ti.Android.createIntent but the implementation of the putExtra method is incomplete. It currently only supports Java primitives. Creating a shortcut requires an intent and an image to be set. Ideally the following example should work:
var currentIntent = Ti.Android.currentActivity.getIntent();
currentIntent.setAction(Ti.Android.ACTION_MAIN);
// Add custom metadata to read when your App is launched from the shortcut.
currentIntent.putExtra("shortcut", "gallery");
// Create an Intent
var shortcutIntent = Ti.Android.createIntent({
action: "com.android.launcher.action.INSTALL_SHORTCUT",
});
// Title that will appear with the shortcut
shortcutIntent.putExtra(Ti.Android.EXTRA_SHORTCUT_NAME, "Gallery");
shortcutIntent.putExtra(Ti.Android.EXTRA_SHORTCUT_INTENT, currentIntent); // Not currently possible
shortcutIntent.putExtra("duplicate", false);
// Set the icon for the shortcut
var iconImage = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, "appicon.png").read();
shortcutIntent.putExtra(Ti.Android.EXTRA_SHORTCUT_ICON, iconImage); // Not currently possible
// Adds the shortcut to the home screen.
Ti.Android.currentActivity.sendBroadcast(shortcutIntent);
I have created a pull request that adds the missing input types.
https://github.com/appcelerator/titanium_mobile/pull/7073
This PR https://github.com/appcelerator/titanium_mobile/pull/7073 introduces a new feature to pass in an Intent and an Image.
PR reviewed, tested and merged.
Verified the implementation. Now "putExtra" supports images as well. Closing. Environment: Appc Studio : 4.5.0.201601262138 Ti SDK : 5.2.0.v20160131224748 Ti CLI : 5.0.6 Alloy : 1.7.33 MAC Yosemite : 10.10.5 Appc NPM : 4.2.3-1 Appc CLI : 5.2.0-238 Node: 4.2.2 Nexus 5 - Android 5.1.1
[~msamah] [~lchoudhary] I try to add this to the 5.2 sample but the code in the description doesn't add an additional launcher shortcut for me (Android 6.0 device, 5.2.0.GA).
[~fokkezb], I had to use this permission in tiapp.xml for the launcher shortcut to get added :
Thanks [~lchoudhary], I got it to work. I also found it's better to use
Ti.App.Android.launchIntent
thenTi.Android.currentActivity.getIntent()
since the last one will not be useful if the primary window is no longer the current one. When the app starts I can useTi.App.Android.launchIntent.getStringExtra()
to see if the shortcut was used to launch it. However, if the app is already running thenewintent
event on eitherTi.Android.currentActivity
or the current Window/TabGroup's activity doesn't fire. Any idea?@fokkezb I actually had to abandon the launcher shortcut because I could not get
newintent
working. I figured it was related to the launcher shortcut but maybe this event is broken?It looks like it. I'll open a ticket.
Here it is: TIMOB-20459