1、创建应用的快捷方式
所以在我们的manifest.xml文件中,添加权限 uses-permission android:name=com.android.launcher.permission.INSTALL_SHORTCUT
private void addShortcut(){
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
// 不允许重复创建
shortcut.putExtra("duplicate", false);
// 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
// 这里必须为Intent设置一个action,可以任意(但安装和卸载时该参数必须一致)
String action = "com.android.action.test";
Intent respondIntent = new Intent(this, this.getClass());
respondIntent.setAction(action);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent);
// 下面的方法与上面的效果是一样的,另一种构建形式而已
// 注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序
// String appClass = this.getPackageName() + "." + this.getLocalClassName();
// ComponentName comp = new ComponentName(this.getPackageName(), appClass);
// shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(action).setComponent(comp));
// 快捷方式的图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcut);
}
2、右上角有数字的快捷方式