主屏幕上的快捷方式,用于我的应用中的操作

时间:2010-10-02 18:53:22

标签: android homescreen

在我的应用程序中,当用户单击某个项目时,我会有一个ListView。 现在我想为列表项创建一个上下文菜单,可以将此操作添加到主屏幕作为快捷方式。 有人可以给我一些链接或提示如何做到这一点吗?

1 个答案:

答案 0 :(得分:1)

private void createShortcut(Command command){
    Intent shortcutIntent = new Intent(this, FormActivity.class);
    // As binary because OS does not know type Command
    command.putToIntentAsXml(shortcutIntent);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, command.getTitle());
    Parcelable iconResource = Intent
        .ShortcutIconResource
        .fromContext(this,  R.drawable.ic_launcher);
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");

    sendBroadcast(intent);
}