App ShortcutsのStatic Shortcutsを実装してみる
App Shortcutsとは
Android 7.1(SDK 25)で追加されたショートカットを作成する機能です.
アプリを長押しすると,ショートカットの機能が出てきます.
吹き出しをタップすることで指定したintentを起動できます.
図1: ショートカット(左上)
ショートカットは先ほどの吹き出しを長押しすることで,ホーム画面にピン留めできます.
図2: ピン留めされたショートカット(左上)
App Shortcutsには二種類あります.
- Static Shortcuts:xmlで定義する静的なショートカット
- Dynamic Shortcuts:Shortcut Managerを利用する動的なショートカット
です.
実装
今回はStatic Shortcutsを作ります.
①AndroidManifest.xmlを定義する
ルートActivityにmeta-dataを追加します.@xml/shortcut
はショートカットを設定したxmlです.コードは②に載せています.
(略) <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=".ComposeActivity" /> </application> (略)
②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" <!-- falseにするとこのショートカットは無効化される --> android:icon="@drawable/icon" android:shortcutShortLabel="@string/compose_shortcut_short_label1" <!-- アプリ長押し時に出るラベル名 --> android:shortcutLongLabel="@string/compose_shortcut_long_label1" <!-- ピン留めしたショートカットのラベル名 --> android:shortcutDisabledMessage="@string/compose_disabled_message1"> <!-- 無効化されたショートカットをタップした時に出る文言 --> <intent android:action="android.intent.action.VIEW" android:targetPackage="com.example.a01.appshortcut" android:targetClass="com.example.a01.appshortcut.ComposeActivity" /> <categories android:name="android.shortcut.conversation" /> </shortcut> </shortcuts>