Skip to content

Commit 5167001

Browse files
author
371718330@qq.com
committed
增加创建删除快捷图标工具类
1 parent 863fb09 commit 5167001

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.jingewenku.abrahamcaijin.commonutil;
2+
3+
import android.app.Activity;
4+
import android.content.ComponentName;
5+
import android.content.ContentResolver;
6+
import android.content.Intent;
7+
import android.database.Cursor;
8+
import android.net.Uri;
9+
import android.content.Intent.ShortcutIconResource;
10+
11+
/**
12+
* @Description:主要功能:创建删除快捷图标工具类
13+
* @Prject: CommonUtilLibrary
14+
* @Package: com.jingewenku.abrahamcaijin.commonutil
15+
* @author: AbrahamCaiJin
16+
* @date: 2017年05月24日 18:18
17+
* @Copyright: 个人版权所有
18+
* @Company:
19+
* @version: 1.0.0
20+
*/
21+
22+
/**
23+
* 需要权限: com.android.launcher.permission.INSTALL_SHORTCUT
24+
* com.android.launcher.permission.UNINSTALL_SHORTCUT
25+
*/
26+
public final class ShortCutUtils {
27+
28+
/**
29+
* Don't let anyone instantiate this class.
30+
*/
31+
private ShortCutUtils() {
32+
throw new Error("Do not need instantiate!");
33+
}
34+
35+
/**
36+
* 检测是否存在快捷键
37+
*
38+
* @param activity Activity
39+
* @return 是否存在桌面图标
40+
*/
41+
public static boolean hasShortcut(Activity activity) {
42+
boolean isInstallShortcut = false;
43+
final ContentResolver cr = activity.getContentResolver();
44+
final String AUTHORITY = "com.android.launcher.settings";
45+
final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
46+
+ "/favorites?notify=true");
47+
Cursor c = cr.query(CONTENT_URI,
48+
new String[]{"title", "iconResource"}, "title=?",
49+
new String[]{activity.getString(R.string.app_name).trim()},
50+
null);
51+
if (c != null && c.getCount() > 0) {
52+
isInstallShortcut = true;
53+
}
54+
return isInstallShortcut;
55+
}
56+
57+
/**
58+
* 为程序创建桌面快捷方式
59+
*
60+
* @param activity Activity
61+
* @param res res
62+
*/
63+
public static void addShortcut(Activity activity,int res) {
64+
65+
Intent shortcut = new Intent(
66+
"com.android.launcher.action.INSTALL_SHORTCUT");
67+
// 快捷方式的名称
68+
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
69+
activity.getString(R.string.app_name));
70+
shortcut.putExtra("duplicate", false); // 不允许重复创建
71+
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
72+
shortcutIntent.setClassName(activity, activity.getClass().getName());
73+
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
74+
// 快捷方式的图标
75+
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
76+
activity, res);
77+
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
78+
79+
activity.sendBroadcast(shortcut);
80+
}
81+
82+
/**
83+
* 删除程序的快捷方式
84+
*
85+
* @param activity Activity
86+
*/
87+
public static void delShortcut(Activity activity) {
88+
89+
Intent shortcut = new Intent(
90+
"com.android.launcher.action.UNINSTALL_SHORTCUT");
91+
// 快捷方式的名称
92+
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
93+
activity.getString(R.string.app_name));
94+
String appClass = activity.getPackageName() + "."
95+
+ activity.getLocalClassName();
96+
ComponentName comp = new ComponentName(activity.getPackageName(),
97+
appClass);
98+
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
99+
Intent.ACTION_MAIN).setComponent(comp));
100+
activity.sendBroadcast(shortcut);
101+
}
102+
}

0 commit comments

Comments
 (0)