Use: Shortcuts? Shortcuts, like BroadcastReceiver, can be registered statically or dynamically using Java code. How to statically register Static ShortCuts

  1. First, we need to create a new XML file in the RES/XML directory,
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="settings"
        android:enabled="true"
        android:icon="@drawable/icon"
        android:shortcutShortLabel="@string/settings_short_name"
        android:shortcutLongLabel="@string/settings_long_name"
        android:shortcutDisabledMessage="@string/settings_disable_msg">

        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="org.loader.shotcutsstatic"
            android:targetClass="org.loader.shotcutsstatic.SettingsActivity" />
        <categories android:name="android.shortcut.conversation"/>
    </shortcut>
</shortcuts>
Copy the code
  1. So first there’s a Shortcuts TAB, and then there’s a Shortcut, so we can probably guess at this point that we can register multiple shortcuts, and there’s a bunch of shortcut tags, so let’s take a look at them one by one.
  • ShortcutId, needless to say, this must be a unique ID
  • Enabled: indicates whether the shortcut is available
  • ShortcutShortLabel: shortcutShortLabel: shortcutShortLabel: shortcutShortLabel: shortcutShortLabel: shortcutShortLabel: shortcutShortLabel: shortcutShortLabel
  • The shortcutLongLabel, which is the configured long name, is displayed in preference to the long name in the launcher
  • ShortcutDisabledMessage, this configuration is given to the user when we select a shortcut that is not available

There are also two familiar labels under shortcut.

  • Intent = intent intent = intent intent = intent intent = intent intent = intent intent = intent intent = intent intent

  • 2. The targetClass is the targetClass to jump to. Note here that android:action must be configured, otherwise it will crash

  • 3. The categories, the current position only to provide the official android. The shortcut. The conversation

Ok, so with the above lines we have a static Shortcuts shortcuts, how do we use that? Is used in the manifest where the activity is configured, and the activity is required.

Can configure shortcuts activity must have action is android. The intent. The action. The MAIN and the category is android. The intent. The category. The LAUNCHER!

An example of a correct configuration:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <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>

        <activity android:name=".SettingsActivity" />
    </application>
Copy the code

Take a look at the result:

Ok, that’s it for static configuration Shortcuts, isn’t it easy? What is this static configuration used for? I thought about it, the applicable scenario is generally some fixed functions, such as the setting interface of your APP, if it is some dynamic data, then static configuration is not suitable, we will introduce dynamic configuration next.

Using the Dynamic Shortcuts

After looking at Static Shortcuts, we can’t believe Google has just opened up such a limited way for us developers to use them, there must be more flexible apis out there, and yes, that’s Dynamic Shortcuts, which I’m calling Dynamic configuration. Speaking of dynamic configuration, it must be implemented in Java code, so how to implement? So the first step, we need to use our code to get the ShortcutManager

getSystemService(ShortcutManager.class)
Copy the code

After we get the ShortcutManager we can call setDynamicShortcuts(List) to set Shortcut. Let’s look at the complete code.

private void setupShortcuts() {
    mShortcutManager = getSystemService(ShortcutManager.class);

    List<ShortcutInfo> infos = new ArrayList<>();
    for (int i = 0; i < mShortcutManager.getMaxShortcutCountPerActivity(); i++) {
        Intent intent = new Intent(this, MessageActivity.class);
        intent.setAction(Intent.ACTION_VIEW);
        intent.putExtra("msg"."And I" + mAdapter.getItem(i) + "The Conversation");

        ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + i)
                .setShortLabel(mAdapter.getItem(i))
                .setLongLabel("Contact :" + mAdapter.getItem(i))
                .setIcon(Icon.createWithResource(this, R.drawable.icon))
                .setIntent(intent)
                .build();
        infos.add(info);
//            manager.addDynamicShortcuts(Arrays.asList(info));
    }

    mShortcutManager.setDynamicShortcuts(infos);
}
Copy the code

The background of this code is that we simulate the function of a contact list. In the launcher, we long press the icon and a certain number of shortcuts will appear for the contact. Clicking a shortcut will directly jump to the page related to the contact. GetSystemService (ShortCutManager.class) to get the ShortcutManager, and then a for loop, notice the number of times this for loop, Because we want to add the Shortcut is not unlimited, so here we use getMaxShortcutCountPerActivity to get maximum number. Then in the for loop, we first construct an intent. Note that here, like Static Shortcut, we have to provide an Action. Then we use ShortcutInfo. Builder to construct a ShortcutInfo and on the List, in the end we call mShortcutManager. SetDynamicShortcuts (infos) to set the Shortcuts.

Okay, the code is actually pretty simple, so let’s see what it looks like.

  1. Dynamically Update Shortcuts

The above code is only implemented in Java code and the real Dynamic will be covered next. Before that, let’s talk about the term ‘Pinning Shortcuts’. For Shortcut, Android allows you to put it directly on the desktop, making it even easier for users. Google calls it Pinning Shortcuts, just to show you what it looks like.

As for this Pinning Shortcuts, Google’s documentation says, ‘We developers don’t have the right to delete it, only users can.’ What if I delete this function? This thing is still on the desktop. Is the APP going to crash? Of course Google took this into account and allowed us to disable shortcut. So let’s go back to the code, where we’re going to simulate deletion by holding on item.

private void removeItem(int index) {
    List<ShortcutInfo> infos = mShortcutManager.getPinnedShortcuts();
    for (ShortcutInfo info : infos) {
        if (info.getId().equals("id" + index)) {
            mShortcutManager.disableShortcuts(Arrays.asList(info.getId()), "No such contact at present");
        }
    }
    mShortcutManager.removeDynamicShortcuts(Arrays.asList("id"+ index)); }. First we call mShortcutManager getPinnedShortcuts () to get all the Pinning Shortcuts, then go to traverse it, find that we remove, and then through the APIdisableShortcuts (List < > Ids)disableRemove this and finally we'll have to remove the shortcuts from the removeDynamicShortcuts(List<Ids>). Let's see what happens.Copy the code

As you can see in the effect, the Pinning Shortcut in disableShortcuts has gone grey, and when you click on it, you will be reminded that there is no such person currently. This reminder is the second parameter of disableShortcuts.

Now that we know about deleting and disabling, what about updates? If I change the name of a contact, should Shortcut change it as well? Yeah, we still have to code it.

private void updItem(int index) {
    Intent intent = new Intent(this, MessageActivity.class);
    intent.setAction(Intent.ACTION_VIEW);
    intent.putExtra("msg"."And I" + mAdapter.getItem(index) + "The Conversation");

    ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + index)
            .setShortLabel(mAdapter.getItem(index))
            .setLongLabel("Contact :" + mAdapter.getItem(index))
            .setIcon(Icon.createWithResource(this, R.drawable.icon))
            .setIntent(intent)
            .build();

    mShortcutManager.updateShortcuts(Arrays.asList(info));
}
Copy the code

Shortcutinfo. Builder to create ShortcutInfo. Finally, we use updateShortcuts(List) to update shortcut. Let’s see what happens.

  1. This article refer to the blog: https://blog.csdn.net/xiaoyu940601/article/details/78653379

If you have some meager help, you can reward to support wechat