Android 长按快捷方式 ShortcutManager

该博客介绍了在Android中,如何在API级别25及以上版本创建动态快捷方式。首先通过`ShortcutManager`服务初始化快捷方式管理器,然后创建针对不同Activity的`ShortcutInfo`对象,设置其标签、图标和意图。接着将创建的快捷方式添加到列表并排序,最后使用`ShortcutManager`设置动态快捷方式。
摘要由CSDN通过智能技术生成
MainActivity.java
private ShortcutManager mShortcutManager;
private ArrayList<ShortcutInfo> mShortcutInfos;

onCreate

//API Level  >= 25
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
    initDynamicShortcuts();
}

initDynamicShortcuts

//①create ShortcutManager
mShortcutManager = getSystemService(ShortcutManager.class);
//②create ShortcutInfo
Intent intent1 = new Intent();
intent1.setClass(this, Activity1.class);
intent1.setAction(Intent.ACTION_PICK_ACTIVITY);
ShortcutInfo scInfoOne  = new ShortcutInfo.Builder(this, "dynamic_one")
        .setShortLabel("Activity1")
        .setLongLabel("to open dynamic activity1")
        .setIcon(Icon.createWithResource(this, R.mipmap.icon1))
        .setIntent(intent1)
        .build();
Intent intent2 = new Intent();
intent2.setClass(this, Activity2.class);
intent2.setAction(Intent.ACTION_PICK_ACTIVITY);
ShortcutInfo scInfoTwo = new ShortcutInfo.Builder(this, "dynamic_two")
        .setShortLabel("Activity2")
        .setLongLabel("to open dynamic activity2")
        .setIcon(Icon.createWithResource(this, R.mipmap.icon2))
		.setIntent(intent2)
        .build();
mShortcutInfos = new ArrayList<ShortcutInfo>();
mShortcutInfos.add(scInfoOne);
mShortcutInfos.add(scInfoTwo);
//sort
Collections.reverse(mShortcutInfos);
//③set
mShortcutManager.setDynamicShortcuts(mShortcutInfos);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值