Android添加桌面快捷方式的简单实现

核心代码如下:

Button bn = (Button) findViewById(R.id.bn);
        // 为按钮的单击事件添加监听器
        bn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View source) {
                // 创建添加快捷方式的Intent
                Intent addIntent = new Intent(
                        "com.android.launcher.action.INSTALL_SHORTCUT"); //
                String title = getResources().getString(R.string.title);
                // 设置快捷方式的标题
                  addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); //// 加载快捷方式的图标
                  Parcelable icon = Intent.ShortcutIconResource.fromContext(
                        AddShortcut.this, R.drawable.ic_launcher);
                // 设置快捷方式的图标
                  addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); //// 创建点击快捷方式后操作Intent,该处当点击创建的快捷方式后,再次启动该程序
                  Intent myIntent = new Intent(AddShortcut.this,
                        AddShortcut.class);
                // 设置快捷方式对应的Intent
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent); //// 发送广播添加快捷方式
                sendBroadcast(addIntent); //
            }
        });
原文地址:https://www.cnblogs.com/AndyGe/p/3442590.html