Closed3

Androidのショートカットが起動しない

ゆーちゃんゆーちゃん
AndroidManifest.xml
<! -- 省略 -->
<activity
    android:name=".MainActivity"
    android:exported="true">
    <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>
<! -- 省略 -->
shortcuts.xml
<?xml version ="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher_round"
        android:shortcutId="shortcut_sample"
        android:shortcutShortLabel="@string/sample_shortcut">

        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.sample.MainActivity"
            android:targetPackage="com.sample" />
    </shortcut>
</shortcuts>

ショートカットのポップアップは出るけど「このアプリはインストールされていません」と表示されてしまう

ゆーちゃんゆーちゃん

どうもandroid:targetPackageはアプリケーションIDを指しているようで、BuildFlavorでdebug時はapplicationIdSuffix設定していたのが原因だった様子。
で実際のアプリIDが↓になっていたので

com.sample.debug

debug/以下にshortcuts.xmlを作成して

shortcuts.xml
<?xml version ="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher_round"
        android:shortcutId="shortcut_sample"
        android:shortcutShortLabel="@string/sample_shortcut">

        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.sample.MainActivity"
            android:targetPackage="com.sample.debug" />
    </shortcut>
</shortcuts>

で起動できました

https://stackoverflow.com/a/43040341

このスクラップは5ヶ月前にクローズされました