核心代码以下: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); // ③ } });