我已经将牛轧糖快捷方式添加到我的Android应用程序中,但所有快捷方式都想启动相同的活动,但使用不同的参数(然后,活动决定要添加哪个片段)
因此,在resxml-v25shortcuts.xml
文件中,我描述了 4 个shortcut
元素,它们都具有相同的意图
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.example.MyActivity"
android:targetPackage="com.example"
/>
那么,如何将包含附加内容的捆绑包传递给该活动呢? 我尝试了<meta-data>
,<data>
还有一件事,我忘记了它是什么,但是onCreate
和onNewIntent
中的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
不知道为什么,但@venimania的回答对我没有帮助,也许我做错了什么。我能够通过使用我自己的自定义操作来解决我的问题:
<intent
android:action="com.example.FEATURE_ONE"
然后在onCreate
String action = getIntent().getAction();
您应该为每个 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