Create an application shortcut in Android NativeScript

When installing an app, many times there is a need to show the shortcut Icon on the Android home screen. You can do so in Android by adding the following:

AndroidManifest:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

And in your page loaded event for the main view:

var androidApp = applicationModule.android;
var context = utils.ad.getApplicationContext();
var pm = context.getPackageManager();
var i = new android.content.Intent();
i.setClassName(androidApp.packageName, androidApp.startActivity.getClass().getName());
i.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
var shortcutintent = new android.content.Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(android.content.Intent.EXTRA_SHORTCUT_NAME,"Name of the App");

var ri = pm.resolveActivity(i, 0);
var iconId = ri.activityInfo.applicationInfo.icon;
var icon = android.content.Intent.ShortcutIconResource.fromContext(context, iconId);

shortcutintent.putExtra(android.content.Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(android.content.Intent.EXTRA_SHORTCUT_INTENT, i);
context.sendBroadcast(shortcutintent);