在牛轧糖 7.1.1 中点击应用程序快捷方式时出现应用程序未安装错误



我在向现有应用程序添加静态应用程序快捷方式时遇到了一些问题。我按照 https://developer.android.com/guide/topics/ui/shortcuts.html 中的步骤操作,快捷方式出现了,但是当我点击它时,它不会启动活动,而是显示一条 Toast 消息:">未安装应用程序"。

以下是清单的相关部分:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activities.SplashActivity"
android:noHistory="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>
<activity
android:name=".activities.MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".activities.ListActivity"
android:label="@string/title_activity_list"
android:parentActivityName=".activities.MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mypackage.activities.MainActivity" />
</activity>
<activity
android:name=".activities.NewActivity"
android:label="@string/title_activity_new"
android:parentActivityName=".activities.ListActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mypackage.activities.ListActivity" />
</activity>
<application/>
</manifest>

以下是快捷方式.xml文件:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="shortcut_new_alarm"
android:enabled="true"
android:icon="@mipmap/ic_launcher"
android:shortcutShortLabel="short label"
android:shortcutLongLabel="long label"
android:shortcutDisabledMessage="message">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.mypackage"
android:targetClass="com.mypackage.activities.NewActivity" />
<!-- If your shortcut is associated with multiple intents, include them
here. The last intent in the list determines what the user sees when
they launch this shortcut. -->
<categories android:name="android.shortcut.conversation" />
</shortcut>
<!-- Specify more shortcuts here. -->
</shortcuts>

我已经仔细检查了,目标活动完全限定名称com.mypackage.activities.NewActivity没问题。

提前感谢!

如果在build.gradle中设置了不同的productFlavors,则应确保android:targetPackageapplication idandroid:targetClass应包含包名称。

例如:

<shortcut
android:shortcutId="shortcut_id_xxx"
android:enabled="true"
android:icon="@drawable/shortcut_xxx"
android:shortcutShortLabel="@string/shortcut_xxx">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.app.internal"
android:targetClass="com.example.app.MainActivity" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
productFlavors {
internal {
applicationId 'com.example.app.internal'
...
}
}

在这里对于您的内部版本,目标包应该是com.example.app.internal,目标类应该是com.example.app.MainActivity

android:targetPackage="com.mypackage"更改为应用/gradle 中的应用 ID。希望这对您有所帮助。

只需将android:exported="true"添加到AndroidManifest下的目标活动标签中.xml并包含INSTALL_SHORTCUT权限

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
...
<activity android:name=".activity.MainActivity"
android:exported="true" />

您正在注册您的NewActivity,而 Splash 是主要的启动器活动。

<activity
android:name="com.mypackage.SplashActivity"
android:label="@string/app_name"
android:exported="true"
android:screenOrientation="portrait" >
<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>

所以只需要删除noHistoty并添加android:exported="true"

以下是快捷方式.xml文件:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="shortcut_new_alarm"
android:enabled="true"
android:icon="@mipmap/ic_launcher"
android:shortcutShortLabel="short label"
android:shortcutLongLabel="long label"
android:shortcutDisabledMessage="message">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.mypackage"
android:targetClass="com.mypackage.activities.NewActivity"/>
<!-- If your shortcut is associated with multiple intents, include them
here. The last intent in the list determines what the user sees when
they launch this shortcut. -->
<categories android:name="android.shortcut.conversation" />
</shortcut>
<!-- Specify more shortcuts here. -->
</shortcuts>

相关内容

最新更新