有什么办法可以从偏好中添加额外的意图吗?

发布于 2024-08-18 08:12:43 字数 493 浏览 3 评论 0原文

您好,我正在从首选项屏幕启动活动。活动由三个偏好共享。 我想知道我是否可以在 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>

“我需要做的所有事情来传递一个整数”之类的事情。我可以执行不同的操作并检查操作而不是额外的操作。

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

<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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

天暗了我发光 2024-08-25 08:12:43

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

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

</Preference>

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>
能否归途做我良人 2024-08-25 08:12:43

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

<Preference android:title="user" android:key="user"/>            

然后您可以使用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;
    }
});

Add the preference to the preference.xml file:

<Preference android:title="user" android:key="user"/>            

And then you can use a setOnPreferenceClickListener to launch an Intent with extras.

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;
    }
});
浸婚纱 2024-08-25 08:12:43

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

它在 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>

也许这种方法适合您?

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.

Related example xml from that demo in 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>

Maybe this approach could work for you?

全部不再 2024-08-25 08:12:43

为我工作。

<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>

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>
不甘平庸 2024-08-25 08:12:43

由于您的 extra 不是常量,因此您应该在 java 代码而不是 xml 中传递它们。

Intent intent = new Intent( this, YourTargetActivity.class );
intent.putExtra( EXTRAS_KEY, extras );
yourPref.setIntent( intent );

As your extras are not constants, you should pass them in the java code instead of xml.

Intent intent = new Intent( this, YourTargetActivity.class );
intent.putExtra( EXTRAS_KEY, extras );
yourPref.setIntent( intent );
许你一世情深 2024-08-25 08:12:43

您可以用来

<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()

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()
仄言 2024-08-25 08:12:43

要发送电子邮件或市场价格,您需要使用类似的东西

<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>

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>
吃兔兔 2024-08-25 08:12:43

并不是你问题的真正答案,但非常相关。也许有人会发现它很有用。
对于较新的 API (>11),您有一个首选项标头文件,并且可以为其中一个标头定义自定义意图。我试图将自定义 Extra 添加到其中一个标头,我发现的解决方案如下所示:

在您的首选项-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();
    }
}

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:

In your preference-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:

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文