使用android中不同Java类中声明的变量

使用android中不同Java类中声明的变量,java,android,Java,Android,我在安卓工作室工作 我在MainActivity.java文件中声明了一个变量,并希望在另一个类中使用相同的变量,例如Records.java文件 在MainActivity.java中: public void parse(String response) { //some code String token = //some code //use code in Records class Intent intent = new Intent(M

我在安卓工作室工作

我在MainActivity.java文件中声明了一个变量,并希望在另一个类中使用相同的变量,例如Records.java文件

在MainActivity.java中:

public void parse(String response)
{

           //some code
     String token = //some code //use code in Records class

     Intent intent = new Intent(MainActivity.this,Records.class);
     startActivity(intent);
}
public class Records extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //use token variable here
}
在RecordsActivity.java中:

public void parse(String response)
{

           //some code
     String token = //some code //use code in Records class

     Intent intent = new Intent(MainActivity.this,Records.class);
     startActivity(intent);
}
public class Records extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //use token variable here
}
因此,根据上面的代码,我想使用MainActivity中声明的token变量,并在Records.java中使用它
甚至允许这样做吗?

按以下方式进行:

String-token=//一些代码//在记录类中使用代码
意向意向=新意向(MainActivity.this,Records.class);
intent.putExtra(“令牌”,令牌)
星触觉(意向);
要在onCreate()中获得预期用途,请执行以下操作:

Intent-Intent=getIntent();
String sToken=intent.getStringExtra(“令牌”);

如果您想使用值存储在令牌中,则使用bundle将数据从一个活动传递到另一个活动。或者将此变量声明为静态。的重复项