有什么办法可以从偏好中添加额外的意图吗?
-
21-09-2019 - |
题
您好,我正在从首选项屏幕启动活动。活动由三个偏好共享。我想知道是否可以在 xml 中为此活动设置额外内容
<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>
我需要做的就是真正传递一个整数。我可以执行不同的操作并检查操作而不是额外的操作。
解决方案
作为演员不是常数,你应该通过他们在java代码而不是XML。
Intent intent = new Intent( this, YourTargetActivity.class );
intent.putExtra( EXTRAS_KEY, extras );
yourPref.setIntent( intent );
其他提示
我得到的回答,您可以使用它是这样的:
<Preference
android:key="xxx"
android:title="xxx"
android:summary="xxx">
<intent android:action="xxx" >
<extra android:name="xxx" android:value="xxx" />
</intent>
</Preference>
偏好添加到preference.xml文件:
<Preference android:title="user" android:key="user"/>
然后你可以使用一个setOnPreferenceClickListener推出具备额外的意图。
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;
}
});
文档中有一个用于描述意图的数据字段 这里.
它在 API 演示应用程序中用于 XML 首选项,以在意图首选项示例中启动意图。
该演示中的相关示例 xml 在preferences.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>
也许这种方法对你有用?
为我工作。
<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>
要发送电子邮件或利率市场,你需要使用像
<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>
可以使用
<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>
,发送意图数据。然后在你活动只需使用:
getIntent().getDataString()
不是一个真正的回答你的问题,但有很大关系。也许有人会觉得它有用。 对于较新的API(> 11)你有一个偏好头文件,你可以自定义意图的标题之一。我试图添加自定义额外的头,我找到了解决办法之一是这样的:
在您偏好headers.xml:
<header
android:fragment="com.mypackage.MyPreference$Prefs1Fragment"
android:title="Intent"
android:summary="Launches an Intent.">
</header>
在你的 “为MyPreference” 级(扩展PreferenceActivity)您有:
public static class Prefs1Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(getActivity(), MyTargetActivity.class);
// set the desired extras, flags etc to the intent
intent.putExtra("customExtra", "Something that I used to know");
// starting our target activity
startActivity(intent);
// ending the current activity, which is just a redirector to our end goal
getActivity().finish();
}
}
不隶属于 StackOverflow