AsyncTask - 从不同类中的doInBackground访问变量

时间:2013-04-29 01:20:23

标签: android

基本上我想要访问在不同类中的doInBackground中分配的变量,但我不知道如何在此处执行此操作。以下是我的相关课程结构。这是一个Android应用程序。感谢。

public class CreateNewPlayers extends AsyncTask<String, String, String> {

public String doInBackground(String... args) {


                String playerid = playerid1.getText().toString();

1 个答案:

答案 0 :(得分:1)

您可以使用publishProgress()和onProgressUpdate()

访问内部数据
public class CreateNewPlayers extends AsyncTask<String, String, String> {
    public SomeOtherClass soc = null;

@Override
public String doInBackground(String... args) {


                String playerid = playerid1.getText().toString();
                publishProgress(playerid);

@Override
public String onProgressUpdate(String... args) {
    super.onProgressUpdate(args);
    soc.setPlayerID(args[0]);
    soc.DoSomething();
}

修改 更完整

public class SomeOtherClass {
    protected String thisPlayerID = null;

    public setPlayerID (String pid) {
        thisPlayerID = pid;
    }

    public DoSomthing () {
        // act on data
    }
}

public class mainActivity extend Activity {
    public CreateNewPlayers cnp = new CreateNewPlayers();
    public SomeOtherClass soc = new SomeOtherClass();

    @Override
    protected void onCreate(Bundle data) {
        super.onCreate(data);
        cnp.soc = soc;
        cnp.exectue("")
    }    
}