传统方式如下
private static final String TAG = "ShortcutUtils";
/**
* 添加桌面图标快捷方式
*
* @param activity Activity对象,设置要启动的activity,一般都是应用入口类
* @param nameId 快捷方式名称id
* @param iconId 图标资源id
*/
public static void addShortcut(Activity activity, int nameId, int iconId) {
Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), iconId, null);
addShortcut(activity, activity.getResources().getString(nameId), bitmap);
}
/**
* 添加桌面图标快捷方式
*
* @param activity Activity对象
* @param name 快捷方式名称
* @param icon 快捷方式图标
*/
public static void addShortcut(Activity activity, String name, Bitmap icon) {
Intent shortcutInfoIntent = new Intent(Intent.ACTION_MAIN);
/**
* 点击快捷方式回到应用,而不是重新启动应用,解决系统一级菜单和二级菜单进入应用不一致问题
*/
shortcutInfoIntent.setClassName(activity, activity.getClass().getName());
shortcutInfoIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shortcutInfoIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
shortcutInfoIntent.addCategory(Intent.CATEGORY_LAUNCHER);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
if (isShortCutExist(activity, name)) {
Log.w(TAG, "shortcut already exist.");
return;
}
// 创建快捷方式的intent广播
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 添加快捷名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
// 快捷图标是允许重复(不一定有效)
shortcut.putExtra("duplicate", false);
// 快捷图标
// 使用资源id方式
// Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(activity, R.mipmap.icon);
// shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
// 使用Bitmap对象模式
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
// 添加携带的下次启动要用的Intent信息
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutInfoIntent);
// 发送广播
activity.sendBroadcast(shortcut);
} else {
ShortcutManager shortcutManager = (ShortcutManager) activity.getSystemService(Context.SHORTCUT_SERVICE);
if (null == shortcutManager) {
// 创建快捷方式失败
Log.e(TAG, "Create shortcut failed.ShortcutManager is null.");
return;
}
shortcutInfoIntent.setAction(Intent.ACTION_VIEW); //action必须设置,不然报错
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(activity, name)
.setShortLabel(name)
.setIcon(Icon.createWithBitmap(icon))
.setIntent(shortcutInfoIntent)
.setLongLabel(name)
.build();
shortcutManager.requestPinShortcut(shortcutInfo, PendingIntent.getActivity(activity,
0, shortcutInfoIntent, PendingIntent.FLAG_UPDATE_CURRENT).getIntentSender());
}
}
/**
* 判断快捷方式是否存在
*
* @param context 上下文
* @param title 快捷方式标志,不能和其它应用相同
* @return
*/
public static boolean isShortCutExist(Context context, String title) {
boolean isInstallShortcut = false;
if (null == context || TextUtils.isEmpty(title))
return isInstallShortcut;
String authority = getAuthority();
final ContentResolver cr = context.getContentResolver();
if (!TextUtils.isEmpty(authority)) {
try {
final Uri CONTENT_URI = Uri.parse(authority);
Cursor c = cr.query(CONTENT_URI, new String[]{"title", "iconResource"}, "title=?", new String[]{title.trim()},
null);
// XXX表示应用名称。
if (c != null && c.getCount() > 0) {
isInstallShortcut = true;
}
if (null != c && !c.isClosed())
c.close();
} catch (Exception e) {
Log.e(TAG, "isShortCutExist:" + e.getMessage());
}
}
return isInstallShortcut;
}
public static String getAuthority() {
String authority;
int sdkInt = android.os.Build.VERSION.SDK_INT;
if (sdkInt < 8) { // Android 2.1.x(API 7)以及以下的
authority = "com.android.launcher.settings";
} else if (sdkInt <= 19) {// Android 4.4及以下
authority = "com.android.launcher2.settings";
} else {// 4.4以上
authority = "com.android.launcher3.settings";
}
return "content://" + authority + "/favorites?notify=true";
}
从Android 7.1(API 25)开始,新增了ShortcutManager,可以对桌面久按应用图标弹出的快捷方式进行管理。
但是,Android 7.1上直接往桌面上添加快捷方式依然是使用上面说到的这种旧方式,但是Android O上,Google应该是想通过比较统一的接口来管理桌面快捷方式了,所以摒弃了这种形式,转而使用ShortcutManager进行管理。所以API 26上,ShortcutManager进行管理。所以API 26上,ShortcutManager新增了对Pinned Shortcuts(固定快捷方式)的管理。
//8.0及以上 @RequiresApi(api = Build.VERSION_CODES.O) public static void addShortCut(Context context,String name) { ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE); if (shortcutManager.isRequestPinShortcutSupported()) { Intent shortcutInfoIntent = new Intent(context, WoXinActivity.class); shortcutInfoIntent.setAction(Intent.ACTION_VIEW); //action必须设置,不然报错 ShortcutInfo info = new ShortcutInfo.Builder(context,name) .setIcon(Icon.createWithResource(context, R.drawable.woxin_icon)) .setShortLabel("我信") .setIntent(shortcutInfoIntent) .build(); //当添加快捷方式的确认弹框弹出来时,将被回调 PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, WoXinActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender()); } }
//8.0以下 public void addShortcut(Activity cx, String name) { Intent addShortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); // 不允许重复创建,不是根据快捷方式的名字判断重复的 addShortcutIntent.putExtra("duplicate", false); addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name); //图标 addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.woxin_icon)); // 设置关联程序 Intent launcherIntent = new Intent(); launcherIntent.setClass(this, WoXinActivity.class); addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent); // 发送广播 sendBroadcast(addShortcutIntent); }
使用方法
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { addShortCut(WoXinActivity.this,"我信" ); }else { addShortcut(WoXinActivity.this,"我信" ); }
最后主要的是权限不能忘记添加
这个功能折腾了一上午,希望能帮助以后用到的人