2017-04-10 55 views
1

我已將Nougat快捷方式添加到我的Android應用程序,但所有快捷方式都希望啓動相同的活動,但使用不同的參數(然後,活動決定要添加哪個碎片)如何向清單聲明的意圖添加附加內容

所以,在res\xml-v25\shortcuts.xml文件,我已經4種shortcut元素描述,它們都具有相同的目的

<intent 
    android:action="android.intent.action.VIEW" 
    android:targetClass="com.example.MyActivity" 
    android:targetPackage="com.example" 
/> 

那麼,如何通過捆綁與演員該活動? 我試過<meta-data><data>,還有一件事,我忘了它是什麼,但onCreateonNewIntent中的intent總是空的。

<?xml version="1.0" encoding="utf-8"?> 
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> 
    <shortcut 
     android:enabled="true" 
     android:icon="@drawable/feature_1" 
     android:shortcutId="feature_one" 
     android:shortcuDsaibledMessage="Disabled" 
     android:shortcutShrotLabel="Feature 1" 
     <intent 
      android:action="android.intent.action.VIEW" 
      android:targetClass="com.example.MyActivity" 
      android:targetPackage="com.example" 
     <!-- <extra --> 
       <!-- android:name="action" --> 
       <!-- android:value="feature_1"/> --> 
     </intent> 
    <shortcut> 
    // then 3 more shortcuts like this with feature 2, 3 and 4 

回答

1

您應該添加一個額外每個shorcut所以,當你收到你的活動的意圖,你可以通過過濾:例如:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> 
    <shortcut 
    android:shortcutId="action1" 
    android:enabled="true" 
    android:icon="@drawable/compose_icon" 
    android:shortcutShortLabel="@string/compose_shortcut_short_label1" 
    android:shortcutLongLabel="@string/compose_shortcut_long_label1" 
    android:shortcutDisabledMessage="@string/compose_disabled_message1"> 
    <intent 
    android:action="android.intent.action.VIEW" 
    android:targetClass="com.example.MyActivity" 
    android:targetPackage="com.example"/> 
    <!-- 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" /> 
    <extra 
      android:name="accion" 
      android:value="action_1_fragment1" /> 
    </shortcut> 
    <!-- Specify more shortcuts here. --> 
</shortcuts> 

然後你的活動範圍內:

String action = getIntent().getStringExtra("accion"); 
if(action.equals(ACTION_1)) 
    //Do action 1 
else 
    //do action 2 
+0

不幸的是,當我在添加快捷'extra'的意圖時,當我長按圖標時,甚至不顯示快捷鍵。爲了證實這確實如此,我評論了'extra'部分並重新構建,並顯示出快捷方式。不知何故,android不想讓我通過額外的...我會嘗試與自定義類別 –

+0

你可以發佈你的所有代碼? – venimania

+0

我在我的應用程序中使用該代碼並且工作正常! – venimania

0

不知道爲什麼,但@venimania的回答沒有幫助我,也許我做錯了什麼。我可以用我自己的自定義的行動來解決我的問題:

<intent 
     android:action="com.example.FEATURE_ONE" 

,然後在onCreate

String action = getIntent().getAction();