2017-04-10 15 views
1

AndroidアプリにNougatショートカットを追加しましたが、すべてのショートカットは同じアクティビティを開始したいが、パラメータを変更したい(アクティビティは追加するフラグメントを決定する)マニフェスト宣言されたインテントにエキストラを追加する方法

ので、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>ともう一度試してみましたが、それは忘れましたが、onCreateonNewIntentintentは常に空になります。

あなたの活動に意図を受け取るときに、あなたがそれによってフィルタリングすることができshorcutあなたはそれぞれのために1 余分を追加する必要があります
<?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

:例えば:あなたの活動の中

<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

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

、その後:私は自分のカスタム定義されたアクションを使って、私の問題を解決することができましたショートカットはアイコンを長押ししても表示されません。これが本当に事実であることを確認するために、私は「余分な」部分をコメントアウトし、再度ビルドして、ショートカットを表示しました。どういうわけかアンドロイドは私にエキストラを渡したくない...カスタムカテゴリで試してみる –

+0

あなたのコードをすべて投稿できますか? – venimania

+0

私は自分のアプリでそのコードを使用しており、うまくいきます! – venimania

0

@venimaniaの答えが私を助けなかった理由は分かりませんが、多分何か間違っていました。 、私はショートカットでの意図に `extra`を追加するとき、onCreate残念ながら

String action = getIntent().getAction(); 
関連する問題