アプリアイコンを長押しして出るメニューの名称を忘れてしまうことが多いのですが「App shortcuts(アプリのショートカット)」です
本当は備忘録としてこれだけで個人的には十分なのですがそれではつまらないので実装してみました
詳しくは以下
https://developer.android.com/guide/topics/ui/shortcuts?hl=ja
とりあえず静的ショートカットを実装
最低限以下があればok
shortcutShortLabelは直接文字列を指定することはできずString.xmlに定義したものを指定します
あとはdeveloperにあるように必要に応じてandroid:enabled/android:icon/android:shortcutLongLabel
/android:shortcutDisabledMessageや<categories>/<capability-binding>の設定をしてください
<categories android:name=”android.shortcut.conversation” />の意味合いはdeveloperには記載されてなくstackoverflowに正しかは不明ですがその件について書かれていました
https://stackoverflow.com/questions/49365976/what-android-shortcut-conversation-means-in-shortcuts-xml
Activityで呼び出されたshortcutIdを取得する方法があれば良いのですが不明だったためintentにextraでキーを追加してます
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="compose"
android:shortcutShortLabel="@string/app_name">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.testsc"
android:targetClass="com.example.testsc.MainActivity">
<extra android:name="content_key" android:value="test" />
</intent>
</shortcut>
</shortcuts>
<manifest>
<application>
<activity>
<meta-data android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
とりあえずonResumeで取得してみます
アプリショートカットから呼び出された場合content_keyが取得できます
class MainActivity : ComponentActivity() {
override fun onResume() {
super.onResume()
val shortcutExtra = intent.getStringExtra("content_key") ?: ""
Log.d("App", shortcutExtra)
}
}
Android Studio Giraffe 2022.3.1 built on June 29, 2023