Android程序运行时可以以编程方式动态创建一个程序快捷方式在home screen上,但并不是每种类型的程序都适合在桌面上创建快捷方式。
比如记帐类程序就比较适合自动创建一个快捷方式在home screen上,方便每天对消费进行记录,游戏类型的游戏不是那么的适合。
创建步骤为:
1.在AndroidManifest.xml中添加权限声明
<!-- AndroidManifest.xml -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<!-- 新增创建快捷方式权限声明 -->
2.调用下面类进行添加
package com.since2006.helper;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
public class ShortcutHelper {
 private static final String TAG = "ShortcutHelper";
 private static final String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
 /**
  * 添加一个程序快捷方式到桌面
  *
  * 调用此方法,程序需要有"com.android.launcher.permission.INSTALL_SHORTCUT"权限
  *
  * className: 点击快捷方式要执行的类
  */
 public static void addShortcutToHomeScreen(Context context, String packageName, String className, int shortcutIcon, int shortcutName) {
  
  Parcelable iconRes = Intent.ShortcutIconResource.fromContext(context, shortcutIcon);
  Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
  shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  shortcutIntent.setClassName(packageName, className);
  Intent intent = new Intent(ACTION_ADD_SHORTCUT);
  intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
  intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getText(shortcutName));
  intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
  context.sendBroadcast(intent);
 }
}
调用示例:
/**
 * context
 * packageName
 * className: 点击快捷方式调用的类
 * shortcutIcon: 快捷方式图标
 * shortcutName: 快捷方式名称
 */
ShortcutHelper.addShortcutToHomeScreen(context, getPackageName(), ".ui.Login", R.drawable.logo, R.string.app_name);