LoginSignup
14
16

More than 5 years have passed since last update.

App Shortcutsを実装してみた

Last updated at Posted at 2016-10-20

App Shortcutsとは?

Android7.1で追加される、アプリのショートカットです。
iOSでいうクイックアクションみたいなものです。

どのように使うのか?

iOSのクイックアクションはアプリのアイコンを強く押せば出てきますが、Androidには3D Touchがないのでタップの強弱を判別できません。
そのためAndroidではアプリのアイコンをロングタップで使えるようになっています。

device-2016-10-20-132436.png

どのように実装するのか?

(SDKの導入に関しての説明は省かせていただきます)

メインのActivityにApp Shortcutsを設定する

AndroidManifest.xml
    <application
        …(省略)…
        >
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <!-- App Shortcutsの設定 -->
            <meta-data android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts" />

        </activity>

        …(以下に別のActivityを追加)…

    </application>

intent-filterでホームにアイコンを表示するよう設定しているactivityに、App Shortcutsを表示するためmeta-dataを追加します。

App Shortcutsの内容を設定する

res内にxml/shortcuts.xmlを追加します。

xml/shortcuts.xml
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="shortcut1"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/shortcut_short_label1"
        android:shortcutLongLabel="@string/shortcut_long_label1"
        android:shortcutDisabledMessage="@string/shortcut_disable_label1">

        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.numero.appshortcutsexample"
            android:targetClass="com.numero.appshortcutsexample.SubActivity" />

        <categories android:name="android.shortcut.conversation" />

    </shortcut>

    <shortcut
        android:shortcutId="shortcut2"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/shortcut_short_label2"
        android:shortcutLongLabel="@string/shortcut_long_label2"
        android:shortcutDisabledMessage="@string/shortcut_disable_label2">

        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.numero.appshortcutsexample"
            android:targetClass="com.numero.appshortcutsexample.SubActivity" />

        <categories android:name="android.shortcut.conversation" />

    </shortcut>

    <shortcut
        android:shortcutId="shortcut3"
        android:enabled="false"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/shortcut_short_label3"
        android:shortcutLongLabel="@string/shortcut_long_label3"
        android:shortcutDisabledMessage="@string/shortcut_disable_label3">

        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.numero.appshortcutsexample"
            android:targetClass="com.numero.appshortcutsexample.SubActivity" />

        <categories android:name="android.shortcut.conversation" />

    </shortcut>

</shortcuts>

shortcutのタグでid、icon、labelを設定し、呼び出したいactivityを設定したintentタグを囲むだけです。

上記の場合はこのような感じで表示されます。
device-2016-10-20-140432.png

ショートカットはxml上で記述した順序とは逆の、下から上へ追加されていきます。
3つ目のショートカットがandroid:enabled="false"となっているので表示されていません。

shortcutタグの説明

android:enabled
ショートカットの表示、非表示

android:icon
ショートカットのアイコン

android:shortcutLongLabel
ショートカット一覧のタイトル名

android:shortcutShortLabel
App Shortcutsより作成したショートカットのタイトル名
詳しくは後述参照

android:shortcutDisabledMessage
App Shortcutsより作成したショートカットが何らかの方法でandroid:enabled="false"となった場合に出てくるメッセージ
詳しくは後述参照

App Shortcutsからショートカットの作成

ショートカット右に二重線のアイコンですが、App Shortcutsからショートカットアイコンを作成できるようになっています。
二重線のアイコンをドラッグすると、ホーム画面にショートカットを設置することができます。
LongLabel2のショートカットを作成すると以下のようになります。
device-2016-10-20-142113.png

先程のandroid:shortcutShortLabelはこのときのタイトル名となっています。
左のMapsを見てもらうとわかりますが、ここで作成されるショートカットのアイコンはandroid:iconで指定したもの+右下にアプリのアイコンになります。

このショートカットを作成した状態でandroid:enabled="false"とすれば
device-2016-10-20-174732.png

このようにアイコンが白くなり、タップするとandroid:shortcutDisabledMessageで指定した文字が表示されます。

補足

今回Android Studio2.1.3で試してみたのですが、shortcutタグのandroid:shortcutShortLabelなどで直接文字を打ち込むとビルドエラーがでました。
キャプチャ.PNG

変更があるかもしれませんが、labelの文字列はres/strings.xmlに記述しましょう。

参考

https://developer.android.com/preview/shortcuts.html

14
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
16