AsyncTask - 如何在onPostExecute之后将值传递回活动类?

时间:2017-08-03 19:53:46

标签: java android

这是我的问题。我创建了一个asyncTask来链接到我的数据库,并使用JSON发送和接收信息。

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);

    progressDialog.dismiss();
    returnValues = dataParsed.split("\\s+");

    mainActivity.getValue(this is the function that calls anotherfunction in 
    asyncTask)

    Log.v("ARRAY LENGTH", String.valueOf(returnValues.length));
}

public String[] returnmyString(){
    //return mySaveData;
    Log.v("ARRAY LENGTH 2", String.valueOf(returnValues.length));
    return returnValues;
}

我在基于活动的类中创建了asyncTask对象,然后调用该object.execute。我的问题是我的代码将在调用object.execute后继续运行,其中一行在执行所有代码之前调用asyncTask类中的函数。

process.activitySave(1); //<---Process is the object for the asyncTask class
process.ContextSave(this,ServerURLSource,myParameters);
process.execute()
changedData = process.returnmyString(); //<-- this is the line of code that gets implemented that returns a null value

我已经尝试在asyncTask类中创建一个Mainactivity对象,然后调用一个函数然后检索该值,但是当我这样做时我的应用程序崩溃了。任何帮助,将不胜感激。我想在mainactivity类中加入某种监听器,因为我似乎无法在asyncTask类中引用mainactivity类中的任何函数。

这是asyncTask中返回值的函数:

public String[] returnmyString(){
    //return mySaveData;
    Log.v("ARRAY LENGTH", String.valueOf(returnValues.length));
    return returnValues;
}

1 个答案:

答案 0 :(得分:1)

方法1是基本的匿名内部类实现。由于内部AsyncTask类不是静态类,因此您可以从该实现中访问CustomActivity的属性。

在方法2中,AsyncClass单独实现。如果您将活动提供给此课程,则可以在执行后回调您所需的方法。对于我们的示例,此方法是#setChangedData方法。 CustomAsyncTask回调#onPostExecute中的#setChangedData。

public class CustomActivity extends AppCompatActivity {

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

        // Method 1 - change data into the anonymously implemented AsyncTask class
        new AsyncTask<Integer, Void, Void>() {

            @Override
            protected Void doInBackground(Integer... params) {
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                CustomActivity.this.mChangedData = "foo"; // this changes mChangedData as "foo"
            }
        }.execute(1);

        // Method 2 - change data into the custom AsyncTask class
        new CustomAsyncTask(this).execute(2);
    }

    public void setChangedData(String changedData){
        this.mChangedData = changedData;
    }

    static class CustomAsyncTask extends AsyncTask<Integer, Void, Void> {
        CustomActivity mActivity;
        public CustomAsyncTask(CustomActivity activity) {
            this.mActivity = activity;
        }
        @Override
        protected Void doInBackground(Integer... params) {
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            mActivity.setChangedData("bar");
        }
    }
}

而且,作为方法3,如果你想更松散地分离Activity和AsyncTask,这就是处理程序方法:

public class CustomActivity extends AppCompatActivity {

    private String mChangedData;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        CustomAsyncTask task = new CustomAsyncTask();
        task.setOnDataChangedListener(new CustomAsyncTask.OnDataChangedListener() {
            @Override
            public void onDataChanged(String data) {
                mChangedData = data;
            }
        });
        task.execute(1);
    }

    private static class CustomAsyncTask extends AsyncTask<Integer, Void, Void> {

        private OnDataChangedListener onDataChangedListener;
        @Override
        protected Void doInBackground(Integer... params) {
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            if(onDataChangedListener != null) {
                onDataChangedListener.onDataChanged("foo");
            }
        }

        void setOnDataChangedListener(OnDataChangedListener onDataChangedListener) {
            this.onDataChangedListener = onDataChangedListener;
        }

        interface OnDataChangedListener {
            void onDataChanged(String data);
        }
    }
}