如何将值从一个类发送到不同的类(多个类)

时间:2013-05-14 05:09:44

标签: java android bundle

我有值strUserKEY,我想将这个值发送到多个不同的类,因为这个值将在5个类中用于url,我知道将值发送到一个类的方法仅使用Intent.putExtra如下:

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

如何一次将此值发送到多个不同的类?我可以使用SharedPrefences ..?我在类和目标类中编写sharedPrefences的方式如何?

9 个答案:

答案 0 :(得分: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);

将这些行粘贴到您想要的位置etUser1key1。您可以在任何SharedPreferences中访问此Activity值。如果你不能请看看here。它可能对你有帮助。

我希望这会对你有所帮助。

答案 1 :(得分:2)

试试这个......

使用意图 ....

从此类发送值
Intent policy= new Intent(LoginActivity.this,EpolicyListPolis.class);
    policy.putExtra("key",strUser);
    startActivity(policy);

这就像: KEY - “ key ”和 VALUE - strUser 。 并且您使用KEY 从另一个类中获取此值。

获得这样的价值。

String user = getIntent().getExtras().getString("key",null);

用户中,您获得 strUser 值。如果strUser没有传递任何值而不是默认 null (“键”的右侧)为用户设置....

你在所需的所有课程中使用相同...但请记住所有与KEY一起工作 ......

因为 KEY (一种ID)只适用于 Put 获取特定值.....

答案 2 :(得分:0)

如果您正在使用依赖注入,请创建一个模型,用于保存值对象中的状态。 然后在您的活动中注入此模型。

或者只是创建一个可以从任何地方引用的Singleton State对象。

答案 3 :(得分:0)

你必须在其他活动中获得这些东西 -

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

或者,

您可以在课程中设置这些值,并随时从中获取。

答案 4 :(得分:0)

我不确定你想做什么,但IMO,如果我将使用从一个类到另一个类的变量,我将使变量静态并从类中“获取”该变量的值。

例如在一个类中我有一个变量:

public class Login extends Activity {

static String username;

    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
       this.requestWindowFeature(Window.FEATURE_NO_TITLE);
       setContentView(R.layout.login_layout);

       username = "Your Name";
    }

在另一个班级,我会得到这样的价值;

   String var;
   var = Login.username;

但正如我所读到的,这不是最好的做法。有时它就足够了。

答案 5 :(得分:0)

首先,使用getIntent()方法获取已启动活动的意图:

Intent intent = getIntent();

如果您的额外数据表示为字符串,则可以使用intent.getStringExtra()方法。

String etUser = intent.getStringExtra("etUser");
String key = intent.getStringExtra("key");

有关详细信息,请参阅this

答案 6 :(得分:0)

我想告诉你使用Application类。从以下链接获取参考的好主意

maintain the state of all the variables from the Application class

Bettter去寻找这个应用程序b'coz你可以访问每个类中的所有变量而不从意图发送。

这会对你有帮助。

答案 7 :(得分:0)

应用程序对象是某种方式,我会选择EventBus(如来自Square或Greenrobot的OTTO)。 在应用程序上下文之后,在相当长的一段时间后,往往会产生“GOD对象”问题。你应该避免这种情况。 SharedPreferences也是一个不错的选择。

答案 8 :(得分: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

来获取第二类中的数据
     Bundle  getBundle = this.getIntent().getExtras();

     name = getBundle.getString("Url") 
      etc......