LoginSignup
28
17

More than 5 years have passed since last update.

Android 7.1 の新機能 App Shortcuts を使ってみる

Last updated at Posted at 2016-12-08

こんばんは、SNS mixiのAndroidアプリを書いているAKKUMAです。
今年はAndroid 7.1で追加された、ショートカットメニューを表示するAPIであるApp Shortcutsを使ってみた話について書きます。

App Shortcuts

Android 7.1(SDK 25)で追加された、ランチャー上のアプリアイコンを長押しすると出るショートカットメニューの機能です。
タップすることで特定のIntentを起動できるようです。メニュー右端の=アイコンをドラッグすることでショートカットをアイコン化をすることもできみたいです。

使ってみる

ショートカットの種類が2つあり、Static ShortcutsDynamic Shortcuts に分かれているようです。
Static Short は xml 上で定義する静的なショートカットメニューを定義します。
Dynamic Shortcuts は ShortcutManager を利用し、動的にショートカットメニューを設定します。

SDK Level

まずは compileSdkVersion25 に設定する必要があります。

build.gradle

android {
    compileSdkVersion 25
}

Static Shortcuts

xmlを利用し静的なショートカットメニューを定義してみます。
res/xml/shortcuts.xmlに配置します。valuesではないので注意。

android:icon にアイコンを指定、android:shortcutShortLabel等に文字列を指定できます。
文字列はこのxml内には直接書けないのでres/values/strings.xmlに定義しましょう。

intentタグ内でタップ時に発動するIntentを定義できます。

res/xml/shortcuts.xml
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="hoge"
        android:enabled="true"
        android:icon="@drawable/ic_motorcycle_black_24dp"
        android:shortcutShortLabel="@string/shortcut_short_label"
        android:shortcutLongLabel="@string/shortcut_long_label"
        android:shortcutDisabledMessage="@string/shortcut_disabled_message">

        <intent
            android:action="info.akkuma.appshortcutssample.action.HOGE"
            android:targetPackage="info.akkuma.appshortcutssample"
            android:targetClass="info.akkuma.appshortcutssample.MainActivity" />

    </shortcut>
</shortcuts>

次に AndroidManifest.xml 上に先程のshortcuts.xmlを登録します。
ランチャーに表示できないActivityに入れてもしょうがないので
<category android:name="android.intent.category.LAUNCHER" /> が付いているActivityに入れましょう。

AndroidManifest.xml
<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>

これだけでショートカットメニューができます。

Dynamic Shortcuts

次はShortcutManagerを利用し動的にメニューを追加してみます。

ShortcutManagerを利用してショートカットメニューを設定したり追加したりします。
ショートカット内容はShortcutInfoを使い作成しますが、IntentにはActionを指定しないと例外がthrowされるので要注意です。
また、最大数を超えるショートカットメニューを追加しようとした時もthrowされます。ショートカットは合計4つ以下でなければならないようです。
既にある同一IDのショートカットメニューを追加した場合は、無視されるようです。


ShortcutManager manager = (ShortcutManager) context.getSystemService(SHORTCUT_SERVICE);

int shortcutCount = manager.getDynamicShortcuts().size();

Intent intent = new Intent(Intent.ACTION_VIEW, null, this, MainActivity.class);

ShortcutInfo info = new ShortcutInfo.Builder(context, "fuga" + shortcutCount)
        .setShortLabel("LGTM " + shortcutCount)
        .setIcon(Icon.createWithResource(context, R.drawable.ic_thumb_up_black_24dp))
        .setIntent(intent)
        .build();

manager.addDynamicShortcuts(Collections.singletonList(info));

28
17
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
28
17