Est-il possible de mettre extras à intention des préférences?
-
21-09-2019 - |
Question
Salut je lance l'activité de l'écran des préférences. L'activité est partagée entre trois préférences. Je me demande si je peux mettre extras pour cette activité en XML
<Preference
android:key="action_1"
android:title="@string/action_1_title"
>
<intent
android:action="com.package.SHAREDACTION"
>
</intent>
</Preference>
Je me demande si je peux faire quelque chose comme
<extras>
<item
android:name=""
android:value=""/>
</extras>
Tout ce que je dois faire pour vraiment passer un entier. Je peux les différentes actions et vérifier l'action au lieu de figurants.
La solution
Comme vos extras ne sont pas constantes, vous devez les passer dans le code java au lieu de XML.
Intent intent = new Intent( this, YourTargetActivity.class );
intent.putExtra( EXTRAS_KEY, extras );
yourPref.setIntent( intent );
Autres conseils
Je suis une réponse, vous pouvez l'utiliser comme ceci:
<Preference
android:key="xxx"
android:title="xxx"
android:summary="xxx">
<intent android:action="xxx" >
<extra android:name="xxx" android:value="xxx" />
</intent>
</Preference>
Ajoutez la préférence au fichier preference.xml:
<Preference android:title="user" android:key="user"/>
Et vous pouvez utiliser un setOnPreferenceClickListener pour lancer une intention avec des figurants.
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;
}
});
Il y a un champ de données pour les intentions décrites dans la documentation
travail pour moi. <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>
Pour envoyer un courriel ou un taux sur le marché, vous devez utiliser quelque chose comme
<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>
Vous pouvez utiliser
<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>
pour envoyer les données d'intention. Ensuite, dans votre activité utilisez simplement:
getIntent().getDataString()
Pas vraiment une réponse à votre question, mais très liée. Peut-être quelqu'un trouvera utile. Pour l'API plus récente (> 11), vous disposez d'un fichier de préférence en-têtes et vous pouvez définir des intentions personnalisées pour l'un des en-têtes. Je tentais d'ajouter une mesure supplémentaire à l'un des en-têtes et la solution que je trouve va comme ceci:
Dans votre préférence headers.xml:
<header
android:fragment="com.mypackage.MyPreference$Prefs1Fragment"
android:title="Intent"
android:summary="Launches an Intent.">
</header>
Dans votre "MyPreference" classe (étend PreferenceActivity) vous avez:
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();
}
}