Ответ 1
Поскольку ваши дополнительные функции не являются константами, вы должны передать их в java-коде вместо xml.
Intent intent = new Intent( this, YourTargetActivity.class );
intent.putExtra( EXTRAS_KEY, extras );
yourPref.setIntent( intent );
Привет, я запускаю активность с экрана настроек. Активность распределяется между тремя предпочтениями. Интересно, могу ли я установить дополнительные функции для этой операции в 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>
Все, что мне нужно сделать, чтобы передать целое число. Я могу выполнять разные действия и проверять действие вместо дополнительных функций.
Поскольку ваши дополнительные функции не являются константами, вы должны передать их в java-коде вместо xml.
Intent intent = new Intent( this, YourTargetActivity.class );
intent.putExtra( EXTRAS_KEY, extras );
yourPref.setIntent( intent );
Я получил ответ, вы можете использовать его вот так:
<Preference
android:key="xxx"
android:title="xxx"
android:summary="xxx">
<intent android:action="xxx" >
<extra android:name="xxx" android:value="xxx" />
</intent>
</Preference>
Существует поле данных для намерений, описанных в документации здесь.
Он используется в демонстрационном приложении API для предпочтений XML, чтобы запустить намерение в примере настроек Intent.
Связанный пример 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>
Возможно, этот подход может сработать для вас?
Добавьте предпочтение в файл 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;
}
});
работает для меня.
<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>
Чтобы отправить электронную почту или курс на рынке, вам нужно использовать что-то вроде
<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>
Вы можете использовать
<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()
Не совсем ответ на ваш вопрос, но очень близкий. Может быть, кто-то найдет это полезным. Для более нового API ( > 11) у вас есть файл заголовков предпочтений, и вы можете определить пользовательские намерения для одного из заголовков. Я пытался добавить пользовательский Экстра в один из заголовков, и найденное мной решение выглядит следующим образом:
В ваших предпочтениях-headers.xml:
<header
android:fragment="com.mypackage.MyPreference$Prefs1Fragment"
android:title="Intent"
android:summary="Launches an Intent.">
</header>
В вашем классе "MyPreference" (extends 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();
}
}