AsyncTask and error handling on Android

前端 未结 12 2187
失恋的感觉
失恋的感觉 2020-11-27 10:32

I\'m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the

12条回答
  •  忘掉有多难
    2020-11-27 11:06

    A more comprehensive solution to Cagatay Kalan's solution is shown below:

    AsyncTaskResult

    public class AsyncTaskResult 
    {
        private T result;
        private Exception error;
    
        public T getResult() 
        {
            return result;
        }
    
        public Exception getError() 
        {
            return error;
        }
    
        public AsyncTaskResult(T result) 
        {
            super();
            this.result = result;
        }
    
        public AsyncTaskResult(Exception error) {
            super();
            this.error = error;
        }
    }
    

    ExceptionHandlingAsyncTask

    public abstract class ExceptionHandlingAsyncTask extends AsyncTask>
    {
        private Context context;
    
        public ExceptionHandlingAsyncTask(Context context)
        {
            this.context = context;
        }
    
        public Context getContext()
        {
            return context;
        }
    
        @Override
        protected AsyncTaskResult doInBackground(Params... params)
        {
            try
            {
                return new AsyncTaskResult(doInBackground2(params));
            }
            catch (Exception e)
            {
                return new AsyncTaskResult(e);
            }
        }
    
        @Override
        protected void onPostExecute(AsyncTaskResult result)
        {
            if (result.getError() != null)
            {
                onPostException(result.getError());
            }
            else
            {
                onPostExecute2(result.getResult());
            }
            super.onPostExecute(result);
        }
    
        protected abstract Result doInBackground2(Params... params);
    
        protected abstract void onPostExecute2(Result result);
    
        protected void onPostException(Exception exception)
        {
                            new AlertDialog.Builder(context).setTitle(R.string.dialog_title_generic_error).setMessage(exception.getMessage())
                    .setIcon(android.R.drawable.ic_dialog_alert).setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener()
                    {
                        public void onClick(DialogInterface dialog, int which)
                        {
                            //Nothing to do
                        }
                    }).show();
        }
    }
    

    Example Task

    public class ExampleTask extends ExceptionHandlingAsyncTask
    {
        private ProgressDialog  dialog;
    
        public ExampleTask(Context ctx)
        {
            super(ctx);
            dialog = new ProgressDialog(ctx);
        }
    
        @Override
        protected void onPreExecute()
        {
            dialog.setMessage(getResources().getString(R.string.dialog_logging_in));
            dialog.show();
        }
    
        @Override
        protected Result doInBackground2(String... params)
        {
            return new Result();
        }
    
        @Override
        protected void onPostExecute2(Result result)
        {
            if (dialog.isShowing())
                dialog.dismiss();
            //handle result
        }
    
        @Override
        protected void onPostException(Exception exception)
        {
            if (dialog.isShowing())
                dialog.dismiss();
            super.onPostException(exception);
        }
    }
    

提交回复
热议问题