add Self Shortcut - Android android.content

Android examples for android.content:Intent

Description

add Self Shortcut

Demo Code

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;

public class Main {

  public static void addSelfShortcut(Context ctx, String name, Parcelable icon, boolean duplicate, Intent intent) {
    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    shortcut.putExtra("duplicate", duplicate);
    intent.setAction(Intent.ACTION_MAIN);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
    ctx.sendBroadcast(shortcut);/*from   ww  w  .j av a 2 s .  c  o  m*/
  }

  public static void addSelfShortcut(Context ctx, int nameId, int iconId, boolean duplicate, Intent intent) {
    String name = ctx.getString(nameId);
    Parcelable icon = Intent.ShortcutIconResource.fromContext(ctx, iconId);
    addSelfShortcut(ctx, name, icon, duplicate, intent);
  }
}

Related Tutorials