Android添加快捷方式

权限

1
2
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>

配置

  • 快捷方式要打开一个界面,需要一个Intent配置这些信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 获取Intent
*
* @param thisActivity 当前activity
* @param goActivity 点击打开的activity
* @return 创建的Intent
*/
private static Intent getShortCutIntent(Activity thisActivity, Class goActivity) {
// 使用MAIN,可以避免部分手机(比如华为、HTC部分机型)删除应用时无法删除快捷方式的问题
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setClass(thisActivity, goActivity);
return intent;
}

Action

1
2
3
4
// Action 添加Shortcut
public static final String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
// Action 移除Shortcut
public static final String ACTION_REMOVE_SHORTCUT = "com.android.launcher.action.UNINSTALL_SHORTCUT";

添加快捷方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 添加快捷方式
*
* @param activity context
* @param name name
* @param goActivity 要启动的界面
* @param allowRepeat 是否允许重复,建议false
* @param iconBitmap 快捷方式图标
*/
public static void addShortcut(Activity activity, Class goActivity, String name,boolean allowRepeat, Bitmap iconBitmap) {
Intent addShortcutIntent = new Intent(ACTION_ADD_SHORTCUT);
// 是否允许重复创建
addShortcutIntent.putExtra("duplicate", allowRepeat);
// 快捷方式的标题
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
// 快捷方式的图标
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconBitmap);
// 快捷方式的动作
Intent shortCutIntent = getShortCutIntent(activity, goActivity);
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent);
activity.sendBroadcast(addShortcutIntent);
}


public static void addShortcut(Activity activity, Class goActivity, String name,
boolean allowRepeat, int res) {
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), res);
addShortcut(activity, goActivity, name, allowRepeat, bitmap);
}

移除快捷方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 移除快捷方式
*
* @param activity context
* @param goActivity 要启动的Activity
* @param name name
*/
public static void removeShortcut(Activity activity, Class goActivity, String name) {
Intent intent = new Intent(ACTION_REMOVE_SHORTCUT);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
//intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.putExtra("duplicate", false);
Intent shortCutIntent = getShortCutIntent(activity, goActivity);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent);
activity.sendBroadcast(intent);
}

附全部代码,亲测可用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* com.march.libs.helper
* CdLibsTest
* Created by chendong on 16/7/29.
* Copyright © 2016年 chendong. All rights reserved.
* Desc :
*/
public class ShortCutHelper {

// Action 添加Shortcut
public static final String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
// Action 移除Shortcut
public static final String ACTION_REMOVE_SHORTCUT = "com.android.launcher.action.UNINSTALL_SHORTCUT";


/**
* 获取Intent
*
* @param thisActivity 当前activity
* @param goActivity 点击打开的activity
* @return 创建的Intent
*/
private static Intent getShortCutIntent(Activity thisActivity, Class goActivity) {
// 使用MAIN,可以避免部分手机(比如华为、HTC部分机型)删除应用时无法删除快捷方式的问题
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setClass(thisActivity, goActivity);
return intent;
}

/**
* 添加快捷方式
*
* @param activity context
* @param name name
* @param goActivity 要启动的界面
* @param allowRepeat 是否允许重复
* @param iconBitmap 快捷方式图标
*/
public static void addShortcut(Activity activity, Class goActivity, String name,
boolean allowRepeat, Bitmap iconBitmap) {
Intent addShortcutIntent = new Intent(ACTION_ADD_SHORTCUT);
// 是否允许重复创建
addShortcutIntent.putExtra("duplicate", allowRepeat);
// 快捷方式的标题
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
// 快捷方式的图标
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconBitmap);
// 快捷方式的动作
Intent shortCutIntent = getShortCutIntent(activity, goActivity);
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent);
activity.sendBroadcast(addShortcutIntent);
}

public static void addShortcut(Activity activity, Class goActivity, String name,
boolean allowRepeat, int res) {
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), res);
addShortcut(activity, goActivity, name, allowRepeat, bitmap);
}


/**
* 移除快捷方式
*
* @param activity context
* @param goActivity 要启动的Activity
* @param name name
*/
public static void removeShortcut(Activity activity, Class goActivity, String name) {
Intent intent = new Intent(ACTION_REMOVE_SHORTCUT);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
//intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.putExtra("duplicate", false);
Intent shortCutIntent = getShortCutIntent(activity, goActivity);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent);
activity.sendBroadcast(intent);
}
}
------ 本文结束 🎉🎉 谢谢观看  ------