Just the other day, I was driving home with a colleague and he was using the iOS Version of Autonavi. Every time I started driving, I pressed the Autonavi Icon for Shortcuts and shortcuts would pop up, making it easy to access the shortcuts and shortcuts for getting home with the iOS 3D Touch feature. Here’s a screenshot from the 647 iPhone X.
In Android 7.1(API 25), we added a new shortcut to the App, which is managed by the ShortcutManager class, so that developers can quickly enter a specific Activity or open a specific web page. There are many apps that already have this feature, as shown below:
Let’s explore this feature in more detail.
Implement both forms of Shortcuts
Static Shortcuts
Static is configured in the project and written to death using XML. Include a resource file in APK to describe Shortcut, directory res/ XML /shortcuts. This method can not do hot update, you need to release the App again.
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="static shortcut"
android:enabled="true"
android:icon="@mipmap/ic_launcher"
android:shortcutShortLabel="@string/shortcut_short_name"
android:shortcutLongLabel="@string/shortcut_long_name"
android:shortcutDisabledMessage="@string/shortcut_disable_msg">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="d.shortcuts"
android:targetClass="d.shortcuts.MainActivity" />
<categories android:name="android.shortcut.conversation"/>
</shortcut>
</shortcuts>
Copy the code
<application .
.>
<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>
</application>
Copy the code
This method is suitable for a hundred years of the same scene, otherwise it is really not flexible.
Dynamic Shortcuts
Dynamic Shortcuts are registered at runtime through the ShortcutManager API. This way you can publish, update, and delete shortcuts dynamically at runtime. Officials give a few examples of shortcut scenarios, such as:
- In the map app, guide users to specific locations;
- In social apps, sending a message to a friend;
- In the media app, play the next clip of the video;
- In a game app, download the last saved bullet points;
Dynamic installation
Build Shortcut for the Activity page
private void setupShortcutsForActivity(a) {
mShortcutManager = getSystemService(ShortcutManager.class);
List<ShortcutInfo> infos = new ArrayList<>();
for (int i = 0; i < mShortcutManager.getMaxShortcutCountPerActivity(); i++) {
Intent intent = new Intent(this, Main2Activity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra("info"."this is info!");
ShortcutInfo info = new ShortcutInfo.Builder(this."ID:" + i)
.setShortLabel("short label")
.setLongLabel("long label")
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
.setIntent(intent)
.build();
infos.add(info);
}
mShortcutManager.setDynamicShortcuts(infos);
}
Copy the code
Build Shortcut using the URL and open the default browser as shown below
private ShortcutInfo createShortcutForUrl(String urlAsString) {
final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mContext, urlAsString);
final Uri uri = Uri.parse(urlAsString);
b.setIntent(new Intent(Intent.ACTION_VIEW, uri));
setSiteInformation(b, uri)
setExtras(b);
return b.build();
}
private ShortcutInfo.Builder setSiteInformation(ShortcutInfo.Builder b, Uri uri) {
b.setShortLabel(uri.getHost());
b.setLongLabel(uri.toString());
Bitmap bmp = fetchFavicon(uri);
if(bmp ! =null) {
b.setIcon(Icon.createWithBitmap(bmp));
} else {
b.setIcon(Icon.createWithResource(mContext, R.drawable.link));
}
return b;
}
private ShortcutInfo.Builder setExtras(ShortcutInfo.Builder b) {
final PersistableBundle extras = new PersistableBundle();
extras.putLong(EXTRA_LAST_REFRESH, System.currentTimeMillis());
b.setExtras(extras);
return b;
}
// Make sure the Task is executed asynchronously
private Bitmap fetchFavicon(Uri uri) {
final Uri iconUri = uri.buildUpon().path("favicon.ico").build();
InputStream is = null;
BufferedInputStream bis = null;
try
{
URLConnection conn = new URL(iconUri.toString()).openConnection();
conn.connect();
is = conn.getInputStream();
bis = new BufferedInputStream(is, 8192);
return BitmapFactory.decodeStream(bis);
} catch (IOException e) {
return null; }}Copy the code
Dynamic delete
public void removeShortcut(ShortcutInfo shortcut) {
mShortcutManager.removeDynamicShortcuts(Arrays.asList(shortcut.getId()));
}
Copy the code
Dynamic outage
public void disableShortcut(ShortcutInfo shortcut) {
mShortcutManager.disableShortcuts(Arrays.asList(shortcut.getId()));
}
Copy the code
Dynamic open
public void enableShortcut(ShortcutInfo shortcut) {
mShortcutManager.enableShortcuts(Arrays.asList(shortcut.getId()));
}
Copy the code
Pinning Shortcut
There’s also the concept of Pinning Shortcuts within the app, which is just another shortcut that allows you to add and remove it.
Use isRequestPinShortcutSupported PinShort () to judge whether the current equipment support. Create pinned Shortcuts is supported on Android 8.0 (API Level 26) and above.
ShortcutManager mShortcutManager = context.getSystemService(ShortcutManager.class);
if (mShortcutManager.isRequestPinShortcutSupported()) {
// Assumes there's already a shortcut with the ID "my-shortcut".
// The shortcut must be enabled.
ShortcutInfo pinShortcutInfo =
new ShortcutInfo.Builder(context, "my-shortcut").build();
// Create the PendingIntent object only if your app needs to be notified
// that the user allowed the shortcut to be pinned. Note that, if the
// pinning operation fails, your app isn't notified. We assume here that the
// app has implemented a method called createShortcutResultIntent() that
// returns a broadcast intent.
Intent pinnedShortcutCallbackIntent =
mShortcutManager.createShortcutResultIntent(pinShortcutInfo);
// Configure the intent so that your app's broadcast receiver gets
// the callback successfully.For details, see PendingIntent.getBroadcast().
PendingIntent successCallback = PendingIntent.getBroadcast(context, /* request code */ 0,
pinnedShortcutCallbackIntent, /* flags */ 0);
mShortcutManager.requestPinShortcut(pinShortcutInfo,
successCallback.getIntentSender());
}
Copy the code
Shortcuts Best Practices
1. Each app can register up to 4 Shortcuts, whether static or dynamic.
2. Limit “short description” to 10 characters
3. Limit “Long Description” to 25 characters
4, change dynamic and pinned Shortcuts, call updateShortcuts()
5. Duplicate create shortcut
- Dynamic Shortcuts usage methods:
addDynamicShortcuts()
或setDynamicShortcuts()
. - Pinned Shortcuts use methods:
requestPinShortcut()
.
6. It is recommended to check the number of returns from the method getDynamicShortcuts() every time you start up and need to republish dynamic Shortcut.
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if (shortcutManager.getDynamicShortcuts().size() == 0) {
// Application restored. Need to re-publish dynamic shortcuts.
if (shortcutManager.getPinnedShortcuts().size() > 0) {
// Pinned shortcuts have been restored. Use
// updateShortcuts() to make sure they contain
// up-to-date information.}}}// ...
}
Copy the code
Demo address: github.com/donald99/sh…
Recommend Google Demo github.com/googlesampl…