Android App Shortcuts

Android App Shortcuts

Explanation

  1. Api of version 25 (Android 7.1), requires min
  2. Each reference to one or more shortcuts are Intent, a corresponding start a task or action
  3. Shortcuts shortcut divided into static (static Shortcuts), dynamic shortcuts (Dynamic Shortcuts), Pointer shortcut (Pinned ShortCuts)
  4. APP released once a maximum five static or dynamic shortcuts, but not limited to the number of shortcut pointer.
  5. APP pointer can not remove shortcuts, but you can turn off the pointer shortcut.
  6. Shortcut icons cannot include tints.
  7. All the shortcuts information is stored in an encrypted credential storage, so your application only after unlocking the device to access a shortcut to the user.

The display order of shortcuts

  1. Static shortcut: isDeclaredInManifest () method returns a shortcut to true.
  2. Dynamic Shortcuts: isDynamic () method returns a shortcut to true.

(Static and dynamic) in accordance getRank (), the shortcut order to sort in ascending order in each type shortcut.

Static Shortcuts

  1. Static shortcuts can not have a custom logo intent. The first intention static shortcut is always set FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK. This means that when the application is already running, when you start a static shortcut, all existing activities in the application will be destroyed. Note: If this behavior is not desirable, you can use the trampoline activity, or start another activity in onCreate (Bundle) activity is not visible, then call finish ():

In AndroidManifest.xml file, trampoline activities should include property distribution android: taskAffinity = "".
Shortcuts in the resource file, static shortcut intentions should reference trampoline activities.

Create a shortcut to a static -Static Shortcuts

  1. Add <meta-data> element, defined shortcut to point to the location of the resource file in the manifest file in the startup activity
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.myapplication">
  <application ... >
    <activity android:name="Main">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      <meta-data android:name="android.app.shortcuts"
                 android:resource="@xml/shortcuts" />
    </activity>
  </application>
</manifest>
    
  1. Create a shortcut to the resource file res / xml / shortcuts.xml:
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
  <shortcut
    android:shortcutId="compose"
    android:enabled="true"
    android:icon="@drawable/compose_icon"
    android:shortcutShortLabel="@string/compose_shortcut_short_label1"
    android:shortcutLongLabel="@string/compose_shortcut_long_label1"
    android:shortcutDisabledMessage="@string/compose_disabled_message1">
    <intent
      android:action="android.intent.action.VIEW"
      android:targetPackage="com.example.myapplication"
      android:targetClass="com.example.myapplication.ComposeActivity" />
    <!-- If your shortcut is associated with multiple intents, include them
         here. The last intent in the list determines what the user sees when
         they launch this shortcut. -->
    <categories android:name="android.shortcut.conversation" />
  </shortcut>
  <!-- Specify more shortcuts here. -->
</shortcuts>
    

Note: android: targetClass refers to the target activity, android: targetPackage refers to the goal of APP package name, which is the application id. Here it has been tested and does not need to request permission.

Note: The two properties android static shortcut must be provided: shortcutId and android: shortcutShortLabel

Dynamic shortcuts (Dynamic Shortcuts)

Dynamically create a shortcut

1. Dynamic shortcuts can publish any intent flag together, but usually specify FLAG_ACTIVITY_CLEAR_TASK, otherwise, if the application is already running, the application will be simply placed in the foreground, and the target activity may not occur.

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
    .setShortLabel("Web site")
    .setLongLabel("Open the web site")
    .setIcon(Icon.createWithResource(context, R.drawable.icon_website))
    .setIntent(new Intent(Intent.ACTION_VIEW,
                   Uri.parse("https://www.mysite.example.com/")))
    .build();

shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));


Pinned Shortcuts- fixed shortcuts

  1. User confirmation is required before you can add a shortcut to the device screen
  2. Users can also copy the application of static and dynamic shortcuts starter up to create a fixed shortcuts.

In the Android O (Api26) and later add shortcuts may be fixed

mShortCutId = "my-pinned-shortcuts";
        ShortcutInfo shortcutInfo;
        Intent mainIntent=new Intent(this,MainActivity.class);
        mainIntent.setAction(Intent.ACTION_MAIN);
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            shortcutInfo = new ShortcutInfo.Builder(this, mShortCutId)
                    .setIntent(mainIntent)
                    .setIcon(Icon.createWithResource(this,R.drawable.ic_person_gray_36dp))
                    .setShortLabel("my-shortcut-label1")
                    .build();
            ShortcutManager mShortcutManager =
                    this.getSystemService(ShortcutManager.class);
            if (mShortcutManager.isRequestPinShortcutSupported()) {
                Intent resultIntent = mShortcutManager.createShortcutResultIntent(shortcutInfo);
                PendingIntent successPendingIntent = PendingIntent.getBroadcast(this, 0, resultIntent, 0);
                mShortcutManager.requestPinShortcut(shortcutInfo, successPendingIntent.getIntentSender());
            }
        }

Fixed shortcut will be removed in the following three cases

  1. User manual removal
  2. APP and shortcuts associated with being unloaded
  3. APP provided by the system, the selective erasing data stored in APP associated with the user interface.

Since the system automatically performs fixed shortcut (Pinned Shortcut) backup and recovery, and therefore these shortcuts ID should contain a stable or constant string identifier of the server, rather than on other devices may be meaningless locally generated identifiers .

Reference Documents

  1. https://developer.android.com/reference/android/content/pm/ShortcutManager.html
  2. https://developer.android.com/guide/topics/ui/shortcuts.html
4311354-42c3ff6c5daf398a.jpg
Scan code red collar support

Reproduced in: https: //www.jianshu.com/p/96d73344bca5

Guess you like

Origin blog.csdn.net/weixin_33712987/article/details/91119547