Android 有没有办法从偏好中添加额外的内容?
嗨,我正在从首选项屏幕启动活动。活动在三个首选项之间共享。 我想知道是否可以用xml为这个活动设置额外的内容Android 有没有办法从偏好中添加额外的内容?,android,android-intent,intentfilter,extras,Android,Android Intent,Intentfilter,Extras,嗨,我正在从首选项屏幕启动活动。活动在三个首选项之间共享。 我想知道是否可以用xml为这个活动设置额外的内容 <Preference android:key="action_1" android:title="@string/action_1_title" > <intent android:action="com.package.SHAREDACTION" > </intent> </Prefe
<Preference
android:key="action_1"
android:title="@string/action_1_title"
>
<intent
android:action="com.package.SHAREDACTION"
>
</intent>
</Preference>
我想知道我是否能做像这样的事情
<extras>
<item
android:name=""
android:value=""/>
</extras>
<Preference
android:title="@string/title_intent_preference"
android:summary="@string/summary_intent_preference">
<intent android:action="android.intent.action.VIEW"
android:data="market://details?id=com.your_package" />
</Preference>
<Preference
android:title="@string/title_intent_preference"
android:summary="@string/summary_intent_preference">
<intent android:action="android.intent.action.VIEW"
android:data="mailto:your_email@gmail.com" />
</Preference>
我需要做的就是传递一个整数。我可以选择不同的动作,并检查动作而不是附加动作。因为附加动作不是常量,所以应该在java代码而不是xml中传递它们
Intent intent = new Intent( this, YourTargetActivity.class );
intent.putExtra( EXTRAS_KEY, extras );
yourPref.setIntent( intent );
文档中描述的意图有一个数据字段 在XML首选项的API演示应用程序中,它用于启动intent首选项示例中的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>
也许这种方法对你有用?我得到了一个答案,你可以这样使用它:
<Preference
android:key="xxx"
android:title="xxx"
android:summary="xxx">
<intent android:action="xxx" >
<extra android:name="xxx" android:value="xxx" />
</intent>
</Preference>
并不是对你问题的真正回答,而是非常相关的。也许有人会发现它很有用。 对于较新的API(>11),您有一个首选项头文件,可以为其中一个头定义自定义意图。我试图在其中一个标题中添加一个自定义额外标题,我发现的解决方案如下所示:
<Preference
android:key="xxx"
android:title="xxx"
android:summary="xxx">
<intent android:action="xxx" >
<extra android:name="xxx" android:value="xxx" />
</intent>
</Preference>
在preference-headers.xml中:
<header
android:fragment="com.mypackage.MyPreference$Prefs1Fragment"
android:title="Intent"
android:summary="Launches an Intent.">
</header>
要发送电子邮件或在市场上定价,您需要使用
<extras>
<item
android:name=""
android:value=""/>
</extras>
<Preference
android:title="@string/title_intent_preference"
android:summary="@string/summary_intent_preference">
<intent android:action="android.intent.action.VIEW"
android:data="market://details?id=com.your_package" />
</Preference>
<Preference
android:title="@string/title_intent_preference"
android:summary="@string/summary_intent_preference">
<intent android:action="android.intent.action.VIEW"
android:data="mailto:your_email@gmail.com" />
</Preference>
将首选项添加到preference.xml文件:
<Preference android:title="user" android:key="user"/>
你可以用
<PreferenceScreen
android:title="@string/title_intent_preference"
android:summary="@string/summary_intent_preference">
<intent android:action="android.intent.action.VIEW"
android:data="hello world" />
</PreferenceScreen>
为我工作
<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>
我知道这一点,但我需要在选择xml.hmm中定义的首选项时传递一个值,但您也可以在代码中检索xml定义的首选项。Thomas-我将接受您的答案-必须实现OnPreferenceClickListener,从首选项中获取意图,然后添加额外的内容。我有点希望它可以通过xml来实现——哦,好吧。你用什么来android:name
?我尝试发送邮件并添加了android.intent.extra.EMAIL
,但它不起作用。名称是您自己的额外密钥名称,然后您可以使用activity.getIntent().getStringExtra(xxx)检索此额外密钥的值,其中xxx是您的name@EthanLeroy迟交的答复,但是,android.intent.extra.EMAIL
extra在XML中不起作用,因为它需要一个String[]
,并且不支持在XML中使用数组作为extra。这必须在代码中完成。如果你在Android Studio中得到“这里不允许使用额外元素”,请忽略它,使用额外的标记时一切正常:)@MichaF。这里似乎有一个bug报告,非常感谢!正如我所期待的,您是否在Activity oncreate中获得了额外的价值,如Bundle=getIntent().getExtras();