techium

このブログは何かに追われないと頑張れない人たちが週一更新をノルマに技術情報を発信するブログです。もし何か調査して欲しい内容がありましたら、@kobashinG or @muchiki0226 までいただけますと気が向いたら調査するかもしれません。

App shortcutsを使ってみる

App Shortcutとは

ランチャーアイコンを長押しすることで、ショートカットメニューを表示します。 また、ショートカットを長押ししてアイコン(Pined shortcut)としてランチャー上に配置できます。 この機能はAndroid 7.1(API level 25)以上から使用できます。

Static Shortcuts

XMLリソースファイルにショートカットを定義します。 動的に変更することはできません。

Dynamic Shortcuts

コード上からショートカットの追加、削除、更新ができます。

Static Shortcutの使い方

Static shortcutsは以下の手順で追加します。

  • ショートカットを定義したxmlファイルをxmlディレクトリに格納する(res/xml/shortcuts.xml)
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="compose"
        android:enabled="true"
        android:icon="@drawable/ic_mode_edit_black_24dp"
        android:shortcutShortLabel="@string/shortcutShortLabel"
        android:shortcutLongLabel="@string/shortcutLongLabel"
        android:shortcutDisabledMessage="@string/shortcutShortLabel">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="jp.forestsoft.appshortcutsample"
            android:targetClass="jp.forestsoft.appshortcutsample.ComposeActivity" />

        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <!-- Specify more shortcuts here. -->
</shortcuts>
  • AndroidManifestファイルのintent-filterにandroid.intent.action.MAINアクションとandroid.intent.category.LAUNCHER カテゴリがセットされたActivityにmeta-dataを追加する
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <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>

これで、ショートカットを追加することができました。

f:id:forestsoftjpdev:20170919012257p:plain:w300

Dynamic Shortcutの使い方

ShortcutManager | Android Developersを使用してDynamic Shortcutを追加します。
 

  • ショートカットの追加
    setDynamicShortcuts()を使ってdynamic shortcutsを追加する、またはaddDynamicShortcuts()にて、すでに存在するdynamic shortcutに追加します。

  • ショートカットの更新
    updateShortcuts() メソッドを使用して、ショートカットのリストを更新します。

  • ショートカットの削除
    removeDynamicShortcuts()メソッドを使い、IDが一致するshortcutを削除する。または、removeAllDynamicShortcuts()を使い、全てのdynamic shortcutを削除できます。
    private void createAddDynamicShortcuts(){
        ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, SHORTCUT_ID1)
                .setShortLabel("Open Web Site")
                .setLongLabel("techium")
                .setIcon(Icon.createWithResource(this, R.drawable.ic_web_black_24dp))
                .setIntent(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://blog.techium.jp/")))
                .build();
        mShortcutManager.setDynamicShortcuts(Arrays.asList(shortcutInfo));
    }

f:id:forestsoftjpdev:20170919012343p:plain:w300

参考

developer.android.com