Is there any way to put extras to Intent from preferences?
Asked Answered
J

8

59

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.

Jezabelle answered 17/1, 2010 at 21:6 Comment(1)
#6948979Improbity
P
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 );
Peterec answered 17/1, 2010 at 22:52 Comment(3)
I know that, but i need to pass a value when selecting a preference defined in xml.Jezabelle
hmm but you can retrieve a xml defined preference in the code also.Peterec
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.Jezabelle
M
114

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>
Marquesan answered 5/3, 2010 at 20:57 Comment(7)
What do you use for android:name? I tried to send a mail and added android.intent.extra.EMAIL but it does not work.Knepper
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 nameTailback
@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.Foredeck
Android Studio (v2.2.2) Problem (just in Design Mode): Exception raised during rendering: <extra> 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?Ubana
If you get "Element extra is not allowed here" in Android Studio, just ignore it, everything is working fine with extra tag :)Undine
@MichaF. there seems to be a bug report open for that code.google.com/p/android/issues/detail?id=230209Tbar
When retrieving these extras in code, value is always of type String?Groomsman
L
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?

Linoel answered 20/1, 2010 at 23:46 Comment(0)
S
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;
    }
});
Snide answered 10/11, 2013 at 7:56 Comment(0)
F
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>
Funky answered 24/10, 2016 at 10:4 Comment(1)
Did you receive extra value in Activity oncreate like this Bundle bundle = getIntent().getExtras();Lentil
P
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 );
Peterec answered 17/1, 2010 at 22:52 Comment(3)
I know that, but i need to pass a value when selecting a preference defined in xml.Jezabelle
hmm but you can retrieve a xml defined preference in the code also.Peterec
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.Jezabelle
A
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()
Arrowwood answered 20/10, 2016 at 4:55 Comment(0)
N
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:[email protected]" />

</Preference>
Nikolas answered 5/11, 2013 at 12:30 Comment(0)
I
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();
    }
}
Incognito answered 18/11, 2012 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.