Android应用桌面快捷方式

164 阅读1分钟

原文:viralpatel.net/blogs/andro…

清单文件

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

添加和删除快捷方式

/**
     * 添加快捷方式
     */
    private void addShortcut() {
        //Adding shortcut for MainActivity 
        //on Home screen
        Intent shortcutIntent = new Intent(getApplicationContext(),
                MainActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);

        Intent addIntent = new Intent();
        addIntent
                .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);//设置点击进入的intent
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AlerDemo实例");//快捷方式的名称  
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));//快捷方式的图标
        addIntent.putExtra("duplicate", false);//不允许重复创建  

        addIntent
                .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);
    }
    /**
     * 删除快捷方式
     */
    private void removeShortcut() {

        //Deleting shortcut for MainActivity 
        //on Home screen
        Intent shortcutIntent = new Intent(getApplicationContext(),
                MainActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);

        Intent addIntent = new Intent();
        addIntent
                .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AlerDemo实例");

        addIntent
                .setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);
    }