在向现有应用添加静态应用快捷方式时遇到了一些问题。我按照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>
这是shortcuts.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
就可以了。
提前致谢!
答案 0 :(得分:15)
如果您在build.gradle中设置了不同的productFlavors
,则应确保android:targetPackage
为application id
,android: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'
...
}
}
在此处为您的内部版本,targetPackage应为com.example.app.internal
,targetClass应为com.example.app.MainActivity
答案 1 :(得分:2)
将您的android:targetPackage="com.mypackage"
更改为app / gradle中的应用ID。希望这会对你有所帮助。
答案 2 :(得分:0)
您注册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"
这是shortcuts.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>
答案 3 :(得分:0)
只需在您的AndroidManifest.xml下的目标活动标签中添加android:exported="true"
,并添加INSTALL_SHORTCUT
权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
...
<activity android:name=".activity.MainActivity"
android:exported="true" />