Android 长按快捷方式 ShortcutManager
程序员文章站
2022-04-15 18:04:55
MainActivity.javaprivate ShortcutManager mShortcutManager;private ArrayList mShortcutInfos;onCreate//API Level >= 25if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { initDynamicShortcuts();}initDynamicShortcu...
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);
本文地址:https://blog.csdn.net/m0_46728513/article/details/108848004