什么是App Shortcuts
中文名,应用快捷方式
通过长按图标,可以弹出应用的快捷方式,点击可以直接跳转至应用的某个界面
创建应用快捷方式分为静态和动态创建两种
静态创建是在AndoridManifiest.xml中添加快捷方式
动态创建是在代码中通过ShortcutManager来动态增加和删除快捷方式
本文只说明动态创建这种方式,静态创建方式可百度
动态创建
1.基本用法
先贴代码
public class ShortCutInitMoudle {
public void init(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
Intent intent1 = new Intent(Intent.ACTION_VIEW);
intent1.setClass(context, PlayActivity.class);
Intent intent2 = new Intent(Intent.ACTION_VIEW);
intent2.setClass(context, SkinActivity.class);
Intent intent3 = new Intent(Intent.ACTION_VIEW);
intent3.setClass(context, ColorActivity.class);
ShortcutInfo playCut = new ShortcutInfo.Builder(context, "play")
.setShortLabel("播放短标签")
.setLongLabel("播放长标签")
.setRank(3)// 弹出的排序,越大离应用图标越远,静态的比动态的离应用图标近
.setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
.setIntent(intent1)// 设置一个intent就是点击启动的页面
.build();
ShortcutInfo skinCut = new ShortcutInfo.Builder(context, "skin")
.setShortLabel("皮肤短标签")
.setLongLabel("皮肤长标签")
.setRank(1)// 弹出的排序,越大离应用图标越远,静态的比动态的离应用图标近
.setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
.setIntent(intent2)// 设置一个intent就是点击启动的页面
.build();
ShortcutInfo colorCut = new ShortcutInfo.Builder(context, "color")
.setShortLabel("颜色短标签")
.setLongLabel("颜色长标签")
.setRank(2)// 弹出的排序,越大离应用图标越远,静态的比动态的离应用图标近
.setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
.setIntent(intent3)// 设置一个intent就是点击启动的页面
.build();
shortcutManager.setDynamicShortcuts(Arrays.asList(playCut, colorCut));
}
}
}
调用方式
// 增加shortcuts
ShortCutInitMoudle shortCut=new ShortCutInitMoudle();
shortCut.init(this);
2.效果
通过贴出的效果,来说明代码中一些属性的作用
![](https://img.haomeiwen.com/i5352666/34239f4fc270e81d.jpg)
划重点
版本要求至少为7.1(API>=25)
长按图标,会弹出3个快捷标签,并且可以将快捷标签拖拽至桌面单独存在,如上图左边的3个图标
代码中构造函数中的play,skin和color分别是这三个标签的唯一id
一些方法的作用
setShortLabel 桌面快捷方式的名字
setLongLabel 长按快捷方式的名字
setRank 距离图标的位置,数字越大,距离越远
setIcon 长按快捷方式左边的小图标
setIntent 点击快捷方式的跳转
3.其它用法
删除某个快捷方式
我们修改上述代码,直接删除掉某一个shortcut
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
Intent intent1 = new Intent(Intent.ACTION_VIEW);
intent1.setClass(context, PlayActivity.class);
Intent intent2 = new Intent(Intent.ACTION_VIEW);
intent2.setClass(context, SkinActivity.class);
Intent intent3 = new Intent(Intent.ACTION_VIEW);
intent3.setClass(context, ColorActivity.class);
ShortcutInfo playCut = new ShortcutInfo.Builder(context, "play")
.setShortLabel("播放短标签")
.setLongLabel("播放长标签")
.setRank(3)// 弹出的排序,越大离应用图标越远,静态的比动态的离应用图标近
.setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
.setIntent(intent1)// 设置一个intent就是点击启动的页面
.build();
ShortcutInfo skinCut = new ShortcutInfo.Builder(context, "skin")
.setShortLabel("皮肤短标签")
.setLongLabel("皮肤长标签")
.setRank(1)// 弹出的排序,越大离应用图标越远,静态的比动态的离应用图标近
.setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
.setIntent(intent2)// 设置一个intent就是点击启动的页面
.build();
shortcutManager.addDynamicShortcuts(Arrays.asList(playCut, skinCut));
运行app
发现虽然我们删除了其中的colorCut,但无论是桌面快捷和长按的快捷,它依然存在
结论:一旦添加上,无需要在执行这个方法,快捷标签就已经存在了
那么如何删除了,当然有办法
// 删除快捷方式
public void removeShortCuts(@ShortcutId String... shortcutId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
mShortcutManager.removeDynamicShortcuts(Arrays.asList(shortcutId));
}
}
调用removeShortCuts方法,重新运行app
shortCut.removeShortCuts(ShortCutInitMoudle.ShortcutId.COLOR);
![](https://img.haomeiwen.com/i5352666/a6117cfa5cca7f21.png)
如上图,长按的颜色标签已被删除,但是桌面的图标仍然存在,并且点击仍能跳转
在删除中增加代码
public void removeShortCuts(@ShortcutId String... shortcutId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
mShortcutManager.removeDynamicShortcuts(Arrays.asList(shortcutId));
mShortcutManager.disableShortcuts(Arrays.asList(shortcutId));
}
}
![](https://img.haomeiwen.com/i5352666/119715b08ec7816b.png)
如上图,颜色段标签已置灰且不可点击
添加快捷方式
public void addShortCuts(@ShortcutId String... shortcutId) {
if (isCanUse()) {
List<ShortcutInfo> list = new ArrayList<>();
for (String id : shortcutId) {
list.add(getShortcutInfoById(id));
}
mShortcutManager.addDynamicShortcuts(list);
}
}
调用addShortCuts,重新运行app
shortCut.addShortCuts(ShortCutInitMoudle.ShortcutId.COLOR);
![](https://img.haomeiwen.com/i5352666/9671da357d6bd5e1.png)
如图,刚才删除掉的color标签已经被重新添加到快捷列表了,并且桌面的短标签恢复成了可点击的状态
重置快捷列表
初始阶段,我们有3个快捷标签
现在调用如下代码
//代码中,我们没有添加皮肤快捷方式
public void resetShortCuts() {
if (isCanUse()) {
ShortcutInfo playCut = getPlayShortCut();
ShortcutInfo colorCut = getColorShortCut();
mShortcutManager.setDynamicShortcuts(Arrays.asList(playCut, colorCut));
}
}
在代码中调用resetShortCuts方法
shortCut.resetShortCuts();
![](https://img.haomeiwen.com/i5352666/c1cf596218987e3d.png)
从上图可以看出,当调用setDynamicShortcuts方法时,长按的快捷列表被重置了,但是短标签不受影响
就介绍这么多了
完整代码
public class ShortCutInitMoudle {
private Context mContext;
private ShortcutManager mShortcutManager;
public void init(Context context) {
if (isCanUse()) {
mContext = context;
mShortcutManager = context.getSystemService(ShortcutManager.class);
}
}
private boolean isCanUse() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1;
}
// 重置快捷方式
public void resetShortCuts() {
if (isCanUse()) {
ShortcutInfo playCut = getPlayShortCut();
ShortcutInfo skinCut = getSkinShortCut();
ShortcutInfo colorCut = getColorShortCut();
mShortcutManager.setDynamicShortcuts(Arrays.asList(playCut,skinCut, colorCut));
}
}
// 添加快捷方式
public void addShortCuts(@ShortcutId String... shortcutId) {
if (isCanUse()) {
List<ShortcutInfo> list = new ArrayList<>();
for (String id : shortcutId) {
list.add(getShortcutInfoById(id));
}
mShortcutManager.addDynamicShortcuts(list);
}
}
// 删除快捷方式
public void removeShortCuts(@ShortcutId String... shortcutId) {
if (isCanUse()) {
mShortcutManager.removeDynamicShortcuts(Arrays.asList(shortcutId));
mShortcutManager.disableShortcuts(Arrays.asList(shortcutId));
}
}
private ShortcutInfo getShortcutInfoById(@ShortcutId String shortcutId) {
ShortcutInfo shortcutInfo = null;
if (isCanUse()) {
switch (shortcutId) {
case ShortcutId.PLAY:
shortcutInfo = getPlayShortCut();
break;
case ShortcutId.SKIN:
shortcutInfo = getSkinShortCut();
break;
case ShortcutId.COLOR:
shortcutInfo = getColorShortCut();
break;
default:
break;
}
}
return shortcutInfo;
}
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private ShortcutInfo getPlayShortCut() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClass(mContext, PlayActivity.class);
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(mContext, ShortcutId.PLAY)
.setShortLabel("播放短标签")
.setLongLabel("播放长标签")
.setRank(3)// 弹出的排序,越大离应用图标越远,静态的比动态的离应用图标近
.setIcon(Icon.createWithResource(mContext, R.mipmap.ic_launcher))
.setIntent(intent)// 设置一个intent就是点击启动的页面
.build();
return shortcutInfo;
}
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private ShortcutInfo getSkinShortCut() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClass(mContext, SkinActivity.class);
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(mContext, ShortcutId.SKIN)
.setShortLabel("皮肤短标签")
.setLongLabel("皮肤长标签")
.setRank(1)
.setIcon(Icon.createWithResource(mContext, R.mipmap.ic_launcher))
.setIntent(intent)// 设置一个intent就是点击启动的页面
.build();
return shortcutInfo;
}
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private ShortcutInfo getColorShortCut() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClass(mContext, ColorActivity.class);
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(mContext, ShortcutId.COLOR)
.setShortLabel("颜色短标签")
.setLongLabel("颜色长标签")
.setRank(2)// 弹出的排序,越大离应用图标越远,静态的比动态的离应用图标近
.setIcon(Icon.createWithResource(mContext, R.mipmap.ic_launcher))
.setIntent(intent)// 设置一个intent就是点击启动的页面
.build();
return shortcutInfo;
}
@StringDef({ShortcutId.PLAY, ShortcutId.SKIN, ShortcutId.COLOR})
@Retention(RetentionPolicy.SOURCE)
public @interface ShortcutId {
String PLAY = "play"; // 播放界面
String SKIN = "skin"; // 皮肤界面
String COLOR = "color"; // 颜色界面
}
}
网友评论