Android 有什么办法可以从偏好中为 Intent 添加额外内容吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2082640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any way to put extras to Intent from preferences?
提问by Alex Volovoy
Hi i'm launching activity from preferences screen. Activity is shared among three preferences. I wonder if i can set extras for this activity in xml
嗨,我正在从首选项屏幕启动活动。活动在三个偏好之间共享。我想知道我是否可以在 xml 中为此活动设置额外内容
<Preference
android:key="action_1"
android:title="@string/action_1_title"
>
<intent
android:action="com.package.SHAREDACTION"
>
</intent>
</Preference>
i wonder if i can do something like
我想知道我是否可以做类似的事情
<extras>
<item
android:name=""
android:value=""/>
</extras>
All i need to do to pass an integer really. I can different actions and check action instead of extras.
我需要做的就是真正传递一个整数。我可以采取不同的行动并检查行动而不是额外的行动。
采纳答案by tbruyelle
As your extras are not constants, you should pass them in the java code instead of xml.
由于您的 extras 不是常量,您应该在 java 代码而不是 xml 中传递它们。
Intent intent = new Intent( this, YourTargetActivity.class );
intent.putExtra( EXTRAS_KEY, extras );
yourPref.setIntent( intent );
回答by ludwigm
I got an answer, you can use it like this:
我得到了一个答案,你可以这样使用它:
<Preference
android:key="xxx"
android:title="xxx"
android:summary="xxx">
<intent android:action="xxx" >
<extra android:name="xxx" android:value="xxx" />
</intent>
</Preference>
回答by Paul Fernhout
There is a data field for intents described in the documentation here.
此处的文档中有一个用于意图的数据字段。
It is used in the API demo application for the XML preferences to launch an intent in the Intent Preferences example.
它在 API 演示应用程序中用于 XML 首选项以启动意图首选项示例中的意图。
Related example xml from that demo in preferences.xml:
来自该演示的相关示例 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>
Maybe this approach could work for you?
也许这种方法对你有用?
回答by patrick-fitzgerald
Add the preference to the preference.xml file:
将首选项添加到首选项.xml 文件中:
<Preference android:title="user" android:key="user"/>
And then you can use a setOnPreferenceClickListener to launch an Intent with extras.
然后您可以使用 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;
}
});
回答by Gautam
working for me.
为我工作。
<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>
回答by kylarsturn
You can use
您可以使用
<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>
to send the intent data. Then in your activity simply use:
发送意图数据。然后在您的活动中只需使用:
getIntent().getDataString()
回答by Viktor Yakunin
To send email or rate on market you need to use something like
要在市场上发送电子邮件或费率,您需要使用类似
<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:[email protected]" />
</Preference>
回答by Andy
Not really an answer to your question, but very much related. Maybe someone will find it useful. For newer API (>11) you have a preference-headers file and you can define custom intents for one of the headers. I was trying to add a custom Extra to one of the headers and the solution I found goes like this:
不是你问题的真正答案,但非常相关。也许有人会发现它很有用。对于较新的 API (>11),您有一个首选项头文件,您可以为头文件之一定义自定义意图。我试图向其中一个标题添加自定义 Extra,我找到的解决方案如下所示:
In your preference-headers.xml:
在您的首选项 headers.xml 中:
<header
android:fragment="com.mypackage.MyPreference$Prefs1Fragment"
android:title="Intent"
android:summary="Launches an Intent.">
</header>
In your "MyPreference" class (extends PreferenceActivity) you have:
在您的“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();
}
}