效果如圖,構建快捷方式菜單
//①、創建動態快捷方式的第一步,創建ShortcutManager ShortcutManager scManager = (ShortcutManager) getApplicationContext().getSystemService( Context.SHORTCUT_SERVICE); //②、構建動態快捷方式的詳細信息 ShortcutInfo scInfoOne = new ShortcutInfo.Builder(getApplicationContext(), "one111") .setShortLabel("Dynamic net") .setLongLabel("go to baidu") .setIcon(Icon.createWithResource(getApplicationContext(), R.drawable.heart)) .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"))) .build(); ShortcutInfo scInfoTwo = new ShortcutInfo.Builder(getApplicationContext(), "one222") .setShortLabel("Dynamic Activity") .setLongLabel("to open dynamic one activity") .setIcon(Icon.createWithResource(getApplicationContext(), R.drawable.heart)) .setIntents(new Intent[]{ new Intent(Intent.ACTION_MAIN, Uri.EMPTY, getApplicationContext(), MainActivity.class). setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) //加該FLAG的目的是讓MainActivity作爲根activity,清空已有的任務 // new Intent(DynamicASOneActivity.ACTION) }) .build(); //③、爲ShortcutManager設置動態快捷方式集合 scManager.setDynamicShortcuts( Arrays.asList(scInfoOne, scInfoTwo)); //如果想爲兩個動態快捷方式進行排序,可執行下面的代碼 ShortcutInfo dynamicWebShortcut = new ShortcutInfo.Builder(getApplicationContext(), "one111") .setRank(0) .build(); ShortcutInfo dynamicActivityShortcut = new ShortcutInfo.Builder(getApplicationContext(), "one222") .setRank(1) .build(); //④、更新快捷方式集合 // scManager.setDynamicShortcuts( Arrays.asList(dynamicWebShortcut, dynamicActivityShortcut) ); scManager.updateShortcuts(Arrays.asList(dynamicWebShortcut, dynamicActivityShortcut));