2017-02-17 140 views
0

我试图将应用程序快捷方式实现到我的应用程序,但我无法让它们工作。 我shortcut.xml:ActivityNotFoundException:无法启动快捷方式

<Shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> 
<shortcut 
    android:shortcutId="shortcut" 
    android:enabled="true" 
    android:icon="@drawable/ic_shortcut" 
    android:shortcutShortLabel="@string/shortcut_label"> 
    <intent 
     android:action="android.intent.action.VIEW" 
     android:targetPackage="com.example" 
     android:targetClass="com.example.package.Activity"/> 
</shortcut> 

的manifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
package="com.example"> 

<application 
    android:name=".Application" 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:resizeableActivity="false" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <activity 
     android:name=".package.Activity" 
     android:label="@string/app_name" 
     android:launchMode="singleTask" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
      <action android:name="android.intent.action.SEARCH" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.app.searchable" 
      android:resource="@xml/voice_search_params" /> 

     <meta-data android:name="android.app.shortcuts" 
      android:resource="@xml/shortcuts" /> 
    </activity> 

这样,我的快捷方式相同谷歌的例子。我可以看到并点击快捷方式,但没有任何反应。我尝试过不同的活动,改变活动地点,活动的行动,则params,如“出口=真”,但没有任何变化 - 在日志中我可以看到只有

ActivityNotFoundException:快捷方式无法启动

谷歌拍照快捷登录我的快捷日志之间的唯一差别,是路径的活动:

I/ActivityManager:START U0 {行动= android.intent.action.VIEW FLG = 0x1000c000 CMP = com。示例/.package.Activity}

VS

I/ActivityManager:START U0 {ACT = android.media.action.STILL_IMAGE_CAMERA FLG = 0x1000c000 CMP = com.google.android.GoogleCamera/com.android.camera.activity.CameraImageActivity }

Google cam具有完整的活动路径,但程序包路径不同。

PS:今天尝试谷歌示例,静态应用程序快捷方式也不适用于示例。动态应用快捷方式运作良好。

回答

0

更改您的活动,以包名称:

android:name=".Activity" 

更改您的快捷方式目标类:

android:targetClass="com.example.Activity" 
+0

已经尝试过。没有帮助。 – Anton

+0

您将得到ActivityNotFoundException的原因是,如果Activity未位于它在清单中指向的包中。在Activity java文件的顶部看到的包名称是什么? –

+0

com.example.package,就像在路径中一样。我试图写完整的活动路径,没有任何变化。 – Anton

0

我也遇到了这个问题。然后我发现android:targetClass是不正确的路径。 android:targetClass =“com.example.camille.testappshortcuts.Main” Main是应用程序的访问权限。 也许你应该改变活动到另一个名字,然后添加它。

1

android:targetPackage必须包含您的应用程序ID。所以,如果你已经在build.gradle定义您的应用程序ID作为applicationId "com.my.app.id"你必须使用android:targetPackage="com.my.app.id"

1

除了马丁的回答,如果你有改变你的应用程序ID多种构建类型,它不会工作。

在我的情况下,我有.debug.qa后缀为应用程序ID。您不能引用字符串引用或使用intent元素中的变量。

我发现了两个解决方案:

1)您将创建不同的shortcuts.xml文件建立不同类型和更新targetPackage

例如,

<intent 
     android:action="com.example.ACTION" 
     android:targetClass="com.example.MainActivity" 
     android:targetPackage="com.example.debug"/> 

2)有一个插件,它允许您使用applicationId变量在XML文件中。

https://github.com/timfreiheit/ResourcePlaceholdersPlugin

相关问题