1. Home > programming >

Talking about adding ShortCut shortcuts in Android

As we all know, there are 4 ways to start an application:

  • Click the app to start
  • Shortcut
  • Notification jump
  • Enter commands (adb commands, etc.)

Today, I will give you a brief introduction to the usage of shortcut startup~

Introduction of shortcuts

Google official Android 7.1 (API 25) adds a new desktop long-press pop-up menu, and after 8.0 (API 26), shortcuts can be fixed to the desktop. There are also many needs around desktop shortcuts. For example, WeChat can add contacts and applets to the desktop; Jianshu adds "writing articles" to the desktop; AutoNavi adds "coordinate information" to the desktop.

Reappearance of shortcut scenarios

Add an application to the desktop

Long press the application to open a certain function

Use shortcuts

Add an application to the desktop

Look at the code first, and later I will write these codes as tool classes for everyone to use:

 /** * @param context current content * @param targetClass shortcut icon to open the interface* @param backClass opens and press the return key to return to the interface* @param shortCutId shortCut unique id * @param shortCutIcon desktop  The icon displayed on the */ public void AddShortCut(Context context, Class targetClass, Class backClass, int shortCutId, int shortCutIcon) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {ShortcutManager shortcutManager = (ShortcutManager) context.  getSystemService(Context.SHORTCUT_SERVICE); if (shortcutManager != null && shortcutManager.isRequestPinShortcutSupported()) {Intent shortcutInfoIntent = new Intent(context, targetClass); shor  tcutInfoIntent.setAction(Intent.ACTION_VIEW); ShortcutInfo info = new ShortcutInfo.Builder(context, "id" + shortCutId) .setIcon(Icon.createWithResource(context, shortCutIcon)). setShortLabel(titles[shortCutId]).setIntent(shortcutInfoIntent)  .build(); PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, backClass), PendingIntent.FLAG_UPDATE_CURRENT); shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender());} }( Toast.makeText  context, "The device does not support creating shortcut icons on the desktop!  ", Toast.LENGTH_LONG).show();} }

This article is published by www.internetweblist.com and does not represent the position of www.internetweblist.com/:http://www.internetweblist.com/programming/82400.html

Contact Us

Online consultation:click here to give a message