有没有办法从偏好中添加额外的Intent?

Ale*_*voy 55 android intentfilter extras android-intent

嗨我正在从偏好设置屏幕启动活动.活动在三个偏好中共享.我想知道我是否可以在xml中为此活动设置额外内容

<Preference
    android:key="action_1"
    android:title="@string/action_1_title"
>
    <intent
        android:action="com.package.SHAREDACTION"
    >

    </intent>
</Preference>
Run Code Online (Sandbox Code Playgroud)

我想知道我能做些什么

<extras>
     <item
      android:name=""
      android:value=""/>
</extras>
Run Code Online (Sandbox Code Playgroud)

我需要做的就是传递一个整数.我可以采取不同的行动并检查行动而不是额外的行动.

lud*_*igm 110

我得到了答案,你可以像这样使用它:

<Preference
    android:key="xxx"
    android:title="xxx"
    android:summary="xxx">
   <intent android:action="xxx" >
         <extra android:name="xxx" android:value="xxx" />
    </intent>        

</Preference>
Run Code Online (Sandbox Code Playgroud)

  • @EthanLeroy延迟响应,但是`android.intent.extra.EMAIL`额外不能在XML中工作,因为它期望`String []`,并且不支持在XML中使用数组作为额外的.它必须在代码中完成. (2认同)
  • 如果您在Android Studio中获得"此处不允许元素额外",只需忽略它,一切正常,额外标记:) (2认同)

小智 13

此处的文档中描述了一个意图数据字段.

它在API演示应用程序中用于XML首选项,以在Intent Preferences示例中启动intent.

相关示例来自preferences.xml中该演示的xml:

    <PreferenceScreen
            android:title="@string/title_intent_preference"
            android:summary="@string/summary_intent_preference">

        <intent android:action="android.intent.action.VIEW"
                android:data="http://www.android.com" />

    </PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

也许这种方法对你有用吗?


pat*_*oid 13

将首选项添加到preference.xml文件:

<Preference android:title="user" android:key="user"/>            
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用setOnPreferenceClickListener来启动带有额外内容的Intent.

Preference userButton = (Preference) findPreference("user");
userButton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
    @Override
    public boolean onPreferenceClick(Preference arg0) {
        Intent intent = new Intent(getActivity(), YourTargetActivity.class);
        intent.putExtra(EXTRA, mUser);
        startActivity(intent);
        return true;
    }
});
Run Code Online (Sandbox Code Playgroud)


Gau*_*tam 11

为我工作.

<shortcut
    android:enabled="true"
    android:icon="@mipmap/xxx"
    android:shortcutDisabledMessage="@string/xxx"
    android:shortcutId="xxxx"
    android:shortcutLongLabel="xxx"
    android:shortcutShortLabel="xxx">
    <intent
        android:action="android.intent.action.VIEW"
        android:targetClass="xxx"
        android:targetPackage="xxx">
        <extra
            android:name="intent_name"
            android:value="true" />
    </intent>
</shortcut>
Run Code Online (Sandbox Code Playgroud)


tbr*_*lle 7

由于你的额外内容不是常量,你应该在java代码而不是xml中传递它们.

Intent intent = new Intent( this, YourTargetActivity.class );
intent.putExtra( EXTRAS_KEY, extras );
yourPref.setIntent( intent );
Run Code Online (Sandbox Code Playgroud)

  • 我知道,但是选择xml中定义的首选项时需要传递一个值。 (2认同)