2017-02-17 125 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