Android创建/删除桌面快捷方式
如何创建桌面快捷方式:选中目标程序,右键点击创建快捷方式 #生活常识# #日常生活技巧# #基础电脑操作#
创建桌面快捷方式,老的代码就不贴了,网上一大堆,基本是intent设置各种action。从Android 7.1(API 25)开始,Android新增了ShortcutManager,可以对桌面久按应用图标弹出的快捷方式进行管理,但是API 25上仍然可以使用老的方式添加快捷方式,从API 26开始支持通过ShortcutManager添加快捷方式了。
为了兼容低版本我们可以使用support提供的方法,support-v4中有一个ShortcutManagerCompat类可以兼容处理,我直接封装了工具类方法:
/** * 添加桌面快捷方式 * @param context * @param className 快捷方式的目标类(全包名的路径) * @param id 快捷方式对应的id * @param iconResId 快捷方式的图标资源 * @param labelResId 快捷方式的名称资源 */ public static void addShortCutCompat(Context context, String className, String id, int iconResId, int labelResId) { Intent shortcutInfoIntent = new Intent(); shortcutInfoIntent.setClassName(context, className); shortcutInfoIntent.setAction(Intent.ACTION_VIEW); addShortCutCompat(context, shortcutInfoIntent, id, iconResId, labelResId); } /** * 添加桌面快捷方式 * @param context * @param shortcutInfoIntent 点击快捷方式时的启动目标intent * @param id 快捷方式对应的id * @param iconResId 快捷方式的图标资源 * @param labelResId 快捷方式的名称资源 */ public static void addShortCutCompat(Context context, Intent shortcutInfoIntent, String id, int iconResId, int labelResId) { if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) { ShortcutInfoCompat info = new ShortcutInfoCompat.Builder(context, id) .setIcon(IconCompat.createWithResource(context, iconResId)) .setShortLabel(context.getResources().getString(labelResId)) .setIntent(shortcutInfoIntent) .build(); //这里第二个参数可以传一个回调,用来接收当快捷方式被创建时的响应 ShortcutManagerCompat.requestPinShortcut(context, info, null); } } /** * 删除快捷方式 * @param context * @param shortCutTitle 快捷方式的名称 * @param className 快捷方式的目标类(全包名的路径) */ public static void deleteShortCut(Context context, String shortCutTitle, String className) { Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); shortcutIntent.setComponent(new ComponentName(context.getPackageName(), className)); Intent intent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT"); intent.putExtra("android.intent.extra.shortcut.NAME", shortCutTitle); intent.putExtra("android.intent.extra.shortcut.INTENT", shortcutIntent); intent.putExtra("duplicate", true); context.sendBroadcast(intent); }
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950ShortcutManagerCompat中我没有找到删除的快捷方式的方法,所以删除还是用老的方式给Intent设置action,但是删除的方法在很多设备上目前都不起作用。。
调用:
添加快捷方式
String className = "com.test.activity.MemoActivity"; String id = className; addShortCutCompat(getContext(), className, id, R.drawable.ic_memo_shortcut, R.string.memo); 123
删除快捷方式(不起作用)
String className = "com.test.activity.MemoActivity"; deleteShortCut(getContext(), getString(R.string.memo), className); 12
需要添加权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/> 12
如果点击快捷方式启动的是一个Activity, 那么该Activity需要在AndroidManifest.xml中稍加配置:
<application> <activity android:name=".activity.MemoActivity" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> </application> 12345678910
如果不加intent-filter中的内容的话,点击快捷方式的时候会报错,提示你要么加一个action为MAIN的intent-filter,要么设置exported为true.
参考:https://www.jianshu.com/p/18be986553db
网址:Android创建/删除桌面快捷方式 https://www.yuejiaxmz.com/news/view/498756
相关内容
win10系统:虚拟桌面的创建、切换、删除快捷键创建快捷方式app官方版
高效清理桌面:彻底删除文件和快捷方式的方法指南
桌面快捷方式在哪找,桌面快捷服务哪里去了,探寻消失的桌面快捷方式,找回便捷生活的秘籍
超级小爱翻译,如何创建桌面快捷方式?
快速整理桌面:有效删除图标的全攻略
快捷方式删除不掉是什么原因?文档如何取消快捷方式?
快捷方式
在 Mac 电脑/笔记本电脑上删除快捷方式的完整教程
快速清理:在 Android 上删除文件的 3 种超简单方法