实现Asynctask

时间:2016-03-17 03:18:37

标签: android interface android-asynctask

我不想直接在asynctask类中实现接口,如下所示:

new AsyncTask<Void, Void, Void>() {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                swipeRefreshLayout.setRefreshing(true);
            }

            @Override
            protected Void doInBackground(Void... params) {
                retrofitCallBackUtil.getLastTenMessageCallBack(AppConfig.USER_ID, userId, offsetNumber, service, new RetrofitResponseCallBack() {
                    @Override
                    public void onSuccess(ArrayList<Message> messages) {
                        messageAdapter.insertToTheFirst(messages);
                    }

                    @Override
                    public void onFailure() {

                    }
                });
                offsetNumber += 5;
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                messageAdapter.notifyDataSetChanged();
                swipeRefreshLayout.setRefreshing(false);
            }
        }.execute();

我只是通过主类实现它并将 this 放入此参数中,如下所示:

retrofitCallBackUtil.getLastTenMessageCallBack(AppConfig.USER_ID, userId, offsetNumber, service, this);

但我不能在asynctask方法中做到这一点。所以任何人都可以给我一些解决方案吗?

2 个答案:

答案 0 :(得分:1)

我假设你在MainActivity(或类似的东西)中有这些代码。

让我们通过getLastTenMessageCallBack

实现您在此活动中传递到implements RetrofitResponseCallBack {...}所需的界面

现在,在您的Asyntask doInBackground方法中,调用

retrofitCallBackUtil.getLastTenMessageCallBack(AppConfig.USER_ID, userId, offsetNumber, service, MainActivity.this)

Voila,您不必再在Asyntask中创建匿名类了。 请注意,你必须正确地移动和放置fields/params,只需要正确的地方,没什么大不了的:D

答案 1 :(得分:1)

AsyncTask分别创建一个类。
AsyncTask将处理数据并发布实现interface内定义的AsyncTask的数据。
在这种情况下,定义的接口是DataDownloadListener

public class GetFoldersAsync extends AsyncTask<Integer,Boolean,Boolean> {
    Context context;
    ProgressDialog dialog;
    ArrayList mFolderDataLevel;

    public GetFoldersAsync(Context context){
        this.context=context;

    }
    public static interface DataDownloadListener {
        public void dataDownloadedSuccessfully(ArrayList data);
        public void dataDownloadFailed();
    }
    DataDownloadListener dataDownloadListener;
    public void setDataDownloadListener(DataDownloadListener dataDownloadListener) {
        this.dataDownloadListener = dataDownloadListener;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = ProgressDialog.show(context, "Please wait", "Processing.........");
    }
    @Override
    protected Boolean doInBackground(Integer... params) {
        Log.v(Constants.BLL_LOG, "ExplorerDBOperation doInBackground  ");

        mFolderDataLevel=new ArrayList();

        // Process to populate mFolderDataLevel

        return true;
    }
    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        if(dialog!=null && dialog.isShowing())
        {dialog.dismiss();}
        Log.v(Constants.BLL_LOG, "ExplorerDBOperation onPostExecute  ");
        if(mFolderDataLevel!=null && mFolderDataLevel.size()>0){
            dataDownloadListener.dataDownloadedSuccessfully(mFolderDataLevel);
        }else{
            dataDownloadListener.dataDownloadFailed();
        }
    }
}

现在在调用者Activity中,当AsyncTask需要处理并将数据返回到同一个活动时调用此方法。

public class HomeActivity extends AppCompatActivity {

    //Other necessary methods goes here .....

    //call this method for processing AsyncTask and to get back data 
    public void getFolderData(int parentID, int callerID){
            Log.v(Constants.BLL_LOG,"parentID="+parentID+" , callerID="+callerID);
            mCallerID=callerID;
            GetFoldersAsync getFolders = new GetFoldersAsync(this);
            getFolders.setDataDownloadListener(new GetFoldersAsync.DataDownloadListener()
            {
                @SuppressWarnings("unchecked")
                @Override
                public void dataDownloadedSuccessfully(ArrayList data) {
                    Log.v(Constants.BLL_LOG,"getFolderData dataProcessSuccessfully");

                    // Success data with populated ArrayList to process further

                }
                @Override
                public void dataDownloadFailed() {
                    //Failure
                    Log.v(Constants.BLL_LOG,"getFolderData dataProcessFailed()");
                }
            });
        getFolders.execute(callerID);
    }
}