/*
* 在桌面添加快捷方式
* @param icon 快捷方式图标
* @param name 快捷方式名称
* @param uri 快捷方式的intent Uri
*/
public void addShortcut(Parcelable icon, String name, Uri uri){
Intent intentAddShortcut = new Intent(ACTION_ADD_SHORTCUT);
//添加名称
intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
//添加图标
intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//设置Launcher的Uri数据
Intent intentLauncher = new Intent();
intentLauncher.setData(uri);
//添加快捷方式的启动方法
intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intentLauncher);
sendBroadcast(intentAddShortcut);
}
/*
* 在桌面添加快捷方式
* @param icon 快捷方式图标
* @param name 快捷方式名称
* @param uri 快捷方式的intent Uri
*/
public void addShortcut(Parcelable icon, String name, Uri uri){
Intent intentAddShortcut = new Intent(ACTION_ADD_SHORTCUT);
//添加名称
intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
//添加图标
intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//设置Launcher的Uri数据
Intent intentLauncher = new Intent();
intentLauncher.setData(uri);
//添加快捷方式的启动方法
intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intentLauncher);
sendBroadcast(intentAddShortcut);
}
2.权限设置
这里第一个参数Parcelable类型的icon如何获得呢,以下举个例子从Drawable文件夹中获取图片。
Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);