好みの意図にエキストラを入れてする方法はありますか?
-
21-09-2019 - |
質問
こんにちは、私は好みの画面から活動を開始しています。活動は3人の好みの間で共有されています。 私は、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>
私がやらなければならないことは、実際に整数を渡します。 I異なるアクションやチェックアクションの代わりに、エクストラことができます。
解決
あなたのエキストラは定数ではないので、あなたの代わりにXMLをJavaコードに渡す必要があります。
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>
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;
}
});
ドキュメントに記述の意図<のhref =「http://developer.android.com/reference/android/content/Intent.html#getData%28%29」のrel = "nofollowをnoreferrerのためのデータフィールドがあります「>ここを。
テント環境設定例では意図を起動するためにXMLの好みのためのAPIのデモアプリケーションで使用されています。
ある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>
たぶん、このアプローチは、あなたのために働くだろうか?
私のために働いています。
<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:your_email@gmail.com" />
</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)のためには、好み-ヘッダファイルを持っていて、ヘッダーの1つにカスタムインテントを定義することができます。私は、ヘッダーの1つにカスタムエクストラを追加しようとしていたと私が見つけた解決策は、このように書きます:
あなたの好み-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();
}
}