代码之家  ›  专栏  ›  技术社区  ›  Alex Volovoy

有没有办法从偏好中增加额外的意图?

  •  54
  • Alex Volovoy  · 技术社区  · 15 年前

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

    我需要做的就是传递一个整数。我可以选择不同的动作,检查动作而不是额外的。

    8 回复  |  直到 8 年前
        1
  •  8
  •   tbruyelle    15 年前

    由于您的临时变量不是常量,应该将其传递到Java代码中而不是XML。

    Intent intent = new Intent( this, YourTargetActivity.class );
    intent.putExtra( EXTRAS_KEY, extras );
    yourPref.setIntent( intent );
    
        2
  •  107
  •   ludwigm    14 年前

    我有一个答案,你可以这样使用它:

    <Preference
        android:key="xxx"
        android:title="xxx"
        android:summary="xxx">
       <intent android:action="xxx" >
             <extra android:name="xxx" android:value="xxx" />
        </intent>        
    
    </Preference>
    
        3
  •  13
  •   patrick-fitzgerald    11 年前

    将首选项添加到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;
        }
    });
    
        4
  •  13
  •   schnatterer RealHowTo    10 年前

    文档中描述的意图有一个数据字段 here .

    它在API演示应用程序中用于XML首选项,以在意向首选项示例中启动意向。

    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>
    

    也许这种方法对你有用?

        5
  •  9
  •   Gautam    8 年前

    为我工作。

    <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>
    
        6
  •  1
  •   Viktor Yakunin    11 年前

    要在市场上发送电子邮件或费率,您需要使用

    <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>
    
        7
  •  1
  •   kylarsturn    8 年前

    你可以使用

    <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()
    
        8
  •  0
  •   Andy    12 年前

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

    在preference-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();
        }
    }
    
    推荐文章