Skip to content

Instantly share code, notes, and snippets.

@Limuyang1013
Created January 4, 2016 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Limuyang1013/b868c974b0cf4a779b73 to your computer and use it in GitHub Desktop.
Save Limuyang1013/b868c974b0cf4a779b73 to your computer and use it in GitHub Desktop.
1、创建快捷方式 2、删除快捷方式 3、判断快捷方式的存在与否
//权限
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
public static void deleteShortcut(Context context , String shortcutName ,
Intent actionIntent , boolean isDuplicate) {
Intent shortcutIntent = new Intent ("com.android.launcher.action.UNINSTALL_SHORTCUT");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME ,shortcutName);
shortcutIntent.putExtra("duplicate" , isDuplicate);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT , actionIntent);
context.sendBroadcast(shortcutIntent);
}
public String getLauncherPackageName(Context context) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo res = context.getPackageManager()
.resolveActivity(intent, 0);
if (res.activityInfo == null) {
return "";
}
if (res.activityInfo.packageName.equals("android")) {
return "";
} else {
return res.activityInfo.packageName;
}
}
public boolean hasShortcut(String name) {
String url;
String packageName = getLauncherPackageName(mContext);
if (packageName == null || packageName.equals("")
|| packageName.equals("com.android.launcher")) {
int sdkInt = android.os.Build.VERSION.SDK_INT;
if (sdkInt < 8) { // Android 2.1.x(API 7)以及以下的
packageName = "com.android.launcher.settings";
} else if (sdkInt < 19) {// Android 4.4以下
packageName = "com.android.launcher2.settings";
} else {// 4.4以及以上
packageName = "com.android.launcher3.settings";
}
}
url = "content://" + packageName + ".settings/favorites?notify=true";
try {
ContentResolver resolver = mContext.getContentResolver();
Cursor cursor = resolver.query(Uri.parse(url), new String[] {
"title", "iconResource" }, "title=?",
new String[] { name }, null);
if (cursor != null && cursor.getCount() > 0) {
return true;
}
} catch (Exception e) {
if (e != null) {
e.printStackTrace();
}
}
return false;
}
//添加权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
public void installShortCut(String name, int icon, Intent intent) {
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
shortcut.putExtra("duplicate", false); // 不允许重复创建
// 快捷方式的图标
ShortcutIconResource iconRes = ShortcutIconResource.fromContext(mContext, icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
intent.setAction("android.intent.action.MAIN");// 桌面图标和应用绑定,卸载应用后系统会同时自动删除图标
intent.addCategory("android.intent.category.LAUNCHER");// 桌面图标和应用绑定,卸载应用后系统会同时自动删除图标
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
mContext.sendBroadcast(shortcut);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment