创建桌面快捷方式,老的代码就不贴了,网上一大堆,基本是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);
}
ShortcutManagerCompat中我没有找到删除的快捷方式的方法,所以删除还是用老的方式给Intent设置action,但是删除的方法在很多设备上目前都不起作用。。
调用:
添加快捷方式
String className = "com.test.activity.MemoActivity";
String id = className;
addShortCutCompat(getContext(), className, id, R.drawable.ic_memo_shortcut, R.string.memo);
删除快捷方式(不起作用)
String className = "com.test.activity.MemoActivity";
deleteShortCut(getContext(), getString(R.string.memo), className);
需要添加权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
如果点击快捷方式启动的是一个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>
如果不加intent-filter中的内容的话,点击快捷方式的时候会报错,提示你要么加一个action为MAIN的intent-filter,要么设置exported为true.