58

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.

Alex Volovoy
  • 67,778
  • 13
  • 73
  • 54
  • http://stackoverflow.com/questions/6948479/extras-on-email-intent-preferences-xml – gor Nov 19 '13 at 09:56

8 Answers8

113

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>
ludwigm
  • 3,363
  • 3
  • 28
  • 36
  • What do you use for `android:name`? I tried to send a mail and added `android.intent.extra.EMAIL` but it does not work. – Ethan Leroy Aug 28 '12 at 22:30
  • 1
    the name is your own extra key name, then you can retrieve the value of this extra using activity.getIntent().getStringExtra(xxx) where xxx is your *name* – pleerock Nov 13 '12 at 06:29
  • 3
    @EthanLeroy Late response, but the `android.intent.extra.EMAIL` extra won't work in XML because it is expecting a `String[]`, and using arrays as extras in XML isn't supported. It has to be done in code. – Jason Robinson Jan 29 '13 at 21:17
  • Android Studio (v2.2.2) Problem (just in Design Mode): **Exception raised during rendering: requires an android:value or android:resource attribute at Binary XML file line #-1**. Though I used `android:value="xxx"`. Does anyone know the solution? – Mir-Ismaili Jan 04 '17 at 19:52
  • 3
    If you get "Element extra is not allowed here" in Android Studio, just ignore it, everything is working fine with extra tag :) – Micha F. Jan 05 '17 at 22:49
  • 1
    @MichaF. there seems to be a bug report open for that https://code.google.com/p/android/issues/detail?id=230209 – gbhall Jan 29 '17 at 08:53
13

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;
    }
});
patrick-fitzgerald
  • 2,561
  • 3
  • 35
  • 49
13

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?

schnatterer
  • 7,525
  • 7
  • 61
  • 80
13

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>
Gautam
  • 1,345
  • 12
  • 30
9

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 );
tbruyelle
  • 12,895
  • 9
  • 60
  • 74
  • 2
    I know that, but i need to pass a value when selecting a preference defined in xml. – Alex Volovoy Jan 17 '10 at 23:11
  • hmm but you can retrieve a xml defined preference in the code also. – tbruyelle Jan 17 '10 at 23:20
  • 1
    Thomas - i'm going accept your answer - have to implement OnPreferenceClickListener, get the intent from preference and then add extra. Was kind of hoping that it can be done from xml - oh well. – Alex Volovoy Jan 18 '10 at 05:43
2

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()
kylarsturn
  • 125
  • 1
  • 8
1

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:your_email@gmail.com" />

</Preference>
Viktor Yakunin
  • 2,927
  • 3
  • 24
  • 39
0

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();
    }
}
Andy
  • 6,869
  • 2
  • 31
  • 24