Android動態創建快捷方式

一、 摘要

以Android O爲分界,介紹兩種動態創建快捷方式的途徑:廣播和ShortcutManager。


二、 Android O以前

在Android O(8.0)以前,動態創建快捷方式是通過發送廣播實現的:

// 由該action可知,我們的創建快捷方式廣播會由launcher,也就是系統桌面來接收
public static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";

public void createShortcutBelowO(Context ctx, String name, Bitmap icon) {
    Intent shortcutIntent = new Intent(ACTION_INSTALL_SHORTCUT);
    // 快捷方式的名字
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    // 快捷方式的bitmap儘可能小,因爲廣播內容超過2MB會拋出異常
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
    // 設置是否允許重複創建快捷方式,該選項非必填,默認是允許
    shortcutIntent.putExtra("duplicate", false);

    // 快捷方式執行的intent,比如啓動應用在AndroidManifest中配置的入口Activity
    Intent launchIntent = new Intent();
    launchIntent.setClass(ctx, "com.zengyu.shortcutdemo");

    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
    ctx.sendBroadcast(shortcutIntent);
}

三、 Android O以後

Android O(8.0)新增了一個叫ShortcutManager的類:

/**
 * The ShortcutManager manages an app's <em>shortcuts</em>. Shortcuts provide users
 * with quick access to activities other than an app's main activity in the currently-active
 * launcher.  For example,
 * an email app may publish the "compose new email" action, which will directly open the
 * compose activity.  The {@link ShortcutInfo} class contains information about each of the
 * shortcuts themselves.
 */

ShortcutManager管理應用的快捷方式。快捷方式爲用戶提供了一個訪問應用的快速渠道。ShortcutInfo類包含了每個快捷方式的信息。

在ShortcutManager的API文檔中,其實已經有詳細的靜態創建和動態創建的介紹,以及創建的具體步驟,但在此我還是以一個簡單的例子,直觀地展示使用ShortcutManager動態創建:

public void createShortcutAboveO(Context ctx, String name, Bitmap icon) {
    ShortcutManager shortcutManager = (ShortcutManager) ctx.getSystemService(Context.SHORTCUT_SERVICE);
    /**
     * 判斷是否支持該方式動態創建
     */
    if (shortcutManager.isRequestPinShortcutSupported()) {
        // 快捷方式執行的intent,比如啓動應用在AndroidManifest中配置的入口Activity
        Intent launchIntent = new Intent();
        launchIntent.setClass(ctx, "com.zengyu.shortcutdemo");

        ShortcutInfo.Builder builder = new ShortcutInfo.Builder(context, pkg)
                    .setShortLabel(name)
                    .setIcon(Icon.createWithBitmap(icon))
                    .setIntent(launchIntent);
        /**
         * 第二個參數爲彈出創建快捷方式確認框時的回調PendingIntent,此例不關注該回調,因此爲null,
         * 如果需要監聽該回調,需要自定義一個BroadcastReceiver,可參考參考文獻中的例子
         */
        shortcutManager.requestPinShortcut(builder.build(), null);
    }
}

其中isRequestPinShortcutSupported方法:

/**
 * Return {@code TRUE} if the app is running on a device whose default launcher supports
 * {@link #requestPinShortcut(ShortcutInfo, IntentSender)}.
 *
 * <p>The return value may change in subsequent calls if the user changes the default launcher
 * app.
 *
 * <p><b>Note:</b> See also the support library counterpart
 * {@link android.support.v4.content.pm.ShortcutManagerCompat#isRequestPinShortcutSupported(
 * Context)}, which supports Android versions lower than {@link VERSION_CODES#O} using the
 * legacy private intent {@code com.android.launcher.action.INSTALL_SHORTCUT}.
 *
 * @see #requestPinShortcut(ShortcutInfo, IntentSender)
 */
public boolean isRequestPinShortcutSupported() {
    try {
        return mService.isRequestPinItemSupported(injectMyUserId(),
                LauncherApps.PinItemRequest.REQUEST_TYPE_SHORTCUT);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}

判斷設備是否支持ShortcutManager的這種方式動態創建快捷方式,如果Android版本低於Android O,使用“com.android.launcher.action.INSTALL_SHORTCUT”這個intent,即我們在上一節中使用的action。因此,如果這個方法返回false,我們應該仍使用廣播的方式來動態創建。


四、 參考文獻

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章