2

値がstrUserありKEY、この値を複数の異なるクラスに送信したいのは、この値がURLの5つのクラスで使用されるためです.次のようにのみ使用して1つのクラスに値を送信する方法を知っていますIntent.putExtra:

Intent policy= new Intent(LoginActivity.this,EpolicyListPolis.class);
        policy.putExtra("etUser",strUser);
        policy.putExtra("key",KEY);
        startActivity(policy);

この値を一度に複数の異なるクラスに送信するにはどうすればよいですか? SharedPrefences を使用できますか? クラスと宛先クラスでsharedPrefencesを書く方法は?

4

9 に答える 9

5

SharedPreferencesを使用できます。

に値を格納する場合SharedPreferences:

SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("etUser", strUser);
editor.putString("key",KEY);
editor.commit();

呼び出す前にこれらの行を貼り付けるだけですstartActivity(policy);

そして値を取得しますSharedPreferences

SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
String etUser1 = settings.getString("etUser", null);
String key1 = settings.getString("key", null);

etUser1これらの行を必要な場所に貼り付けますkey1SharedPreferencesこの値には、任意の でアクセスできますActivity。できない場合は、こちらをご覧ください。それはあなたを助けるかもしれません。

これがお役に立てば幸いです。

于 2013-05-14T05:14:19.060 に答える
0

Bundle を使用して、ある Activity クラスから他の Activity クラスにデータを渡すことができます

  Bundle bundle = new Bundle();
    bundle.putString("Id", videoChannelId);
    bundle.putString("C/V", "C");
    bundle.putString("mode", "ch");
    bundle.putString("code", "LiveTV");
    bundle.putString("urlcount", "2");
    Intent intent = new Intent(First.this,Second.class);
    intent.putExtras(bundle);
    startActivity(intent);

バンドル ID を指定して、このように 2 番目のクラスのデータを取得します

     Bundle  getBundle = this.getIntent().getExtras();

     name = getBundle.getString("Url") 
      etc......
于 2013-05-14T07:23:45.687 に答える
0

依存性注入を使用している場合は、状態を値オブジェクトに保持するモデルを作成します。次に、このモデルをアクティビティに挿入します。

または、どこからでも参照できる Singleton State オブジェクトを作成するだけです。

于 2013-05-14T05:13:11.270 に答える
0

他のアクティビティでこれらのものを取得する必要があります--

   Bundle bundle = getIntent().getExtras();
   String user = bundle.getString("etUser");
   String key = bundle.getString("key");

または、

これらの値をクラスに設定して、必要な場所から取得できます。

于 2013-05-14T05:14:16.277 に答える
0

アプリケーションオブジェクトは何らかの方法で、EventBus (Square や Greenrobot の OTTO など) に行きます。アプリケーション コンテキストを反転させると、かなり時間がたってから「GOD オブジェクト」の問題が発生する傾向があります。これは避けるべきです。SharedPreferences も良いオプションです。

于 2013-05-14T06:31:46.467 に答える