How to implement Desktop Shortcut with Android
This article mainly introduces Android how to achieve desktop shortcuts, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Android shortcut
Mode of use
ShortcutUtils.getInstance () .addShortcut (this, MainActivity2.class, liveBundle, "live_Id", "watching Live" "watch live", R.drawable.live) .addShortcut (this, MainActivity2.class, vodBundle, "vod_Id" "watch playback", "watch playback", R.drawable.vod) .build ()
Add permission
ShortcutUtils utility class (used directly)
/ * Android Shortcut tool Class * Build.VERSION.SDK_INT > = 25 * API25 and above are available * setShortLabel setting short title * setLongLabel setting long title * setIcon setting icon * setIntent setting Intent * @ author renquan * @ date 27 December 2021 * / public class ShortcutUtils {private static ShortcutUtils shortcutUtils; private List shortcutInfos; private Context mContext Public static ShortcutUtils getInstance () {if (shortcutUtils = = null) {synchronized (ShortcutUtils.class) {if (shortcutUtils = = null) {shortcutUtils = new ShortcutUtils ();} return shortcutUtils;} private ShortcutUtils () {shortcutInfos = new ArrayList () } / * set Class object * all parameters cannot be empty * @ param context * @ param cls * @ param bundle * @ param shortcutId * @ param shortLabel * @ param longLabel * @ return * / public ShortcutUtils addShortcut (Context context, Class cls, Bundle bundle, String shortcutId, String shortLabel, String longLabel @ DrawableRes int resId) {if (shortcutUtils! = null & & shortcutInfos! = null) {if (Build.VERSION.SDK_INT > = 25) {mContext = context Intent intent = new Intent (context, cls); intent.putExtra ("shortcutArgument", bundle); intent.setAction (Intent.ACTION_VIEW); intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK) ShortcutInfo shortcutInfo = new ShortcutInfo.Builder (context, shortcutId) .setShortLabel (shortLabel) .setLongLabel (longLabel) .setIcon (Icon.createWithResource (context, resId)) .setIntent (intent) .build (); shortcutInfos.add (shortcutInfo) }} return shortcutUtils } / * set Intent object * all parameters cannot be empty * @ param context * @ param intent * @ param shortcutId * @ param shortLabel * @ param longLabel * @ param resId * @ return * / public ShortcutUtils addShortcut (Context context, Intent intent, String shortcutId, String shortLabel, String longLabel @ DrawableRes int resId) {if (Build.VERSION.SDK_INT > = 25) {mContext = context If (shortcutUtils! = null & & shortcutInfos! = null) {ShortcutInfo shortcutInfo = new ShortcutInfo.Builder (context, shortcutId) .setShortLabel (shortLabel) .setLongLabel (longLabel) .setIcon (Icon.createWithResource (context) ResId) .setIntent (intent) .build () ShortcutInfos.add (shortcutInfo);}} return shortcutUtils;} / * build Shortcuts * / public void build () {if (shortcutInfos! = null & & shortcutInfos.size () > 0 & & mContext! = null) {ShortcutManager systemService = mContext.getSystemService (ShortcutManager.class); systemService.setDynamicShortcuts (shortcutInfos) } Thank you for reading this article carefully. I hope the article "how to achieve Desktop Shortcut" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!