用于在AsyncTask中捕获未处理的exception的设计模式

伙计们,

我在onCreate的顶部通过这样的代码片段捕获未处理的Androidexception

try { File crashLogDirectory = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + Constants.CrashLogDirectory); crashLogDirectory.mkdirs(); Thread.setDefaultUncaughtExceptionHandler(new RemoteUploadExceptionHandler( this, crashLogDirectory.getCanonicalPath())); } catch (Exception e) { if (MyActivity.WARN) Log.e(ScruffActivity.TAG, "Exception setting up exception handler! " + e.toString()); } 

我想在我的android应用程序中使用大约二十几个AsyncTasks,因此会捕获并记录在doInBackground中发生的未处理的exception。

问题是,因为AsyncTask采用任意types的初始化器,我不知道如何声明一个超类,我的所有AsyncTasks都inheritance了它,它设置了这个未处理的exception处理程序。

任何人都可以推荐一个好的设计模式来处理AsyncTask的doInBackground方法中的未处理exception,它不涉及每个新的AsyncTask定义的上述代码的复制和粘贴?

谢谢!

UPDATE

这是我使用的设计模式,在仔细查看AsyncTask的源代码之后

 import java.io.File; import android.content.Context; import android.os.AsyncTask; import android.os.Environment; import android.util.Log; public abstract class LoggingAsyncTask extends AsyncTask { protected void setupUnhandledExceptionLogging(Context context) { try { File crashLogDirectory = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + Constants.CrashLogDirectory); crashLogDirectory.mkdirs(); Thread.setDefaultUncaughtExceptionHandler(new RemoteUploadExceptionHandler( context, crashLogDirectory.getCanonicalPath())); } catch (Exception e) { if (MyActivity.WARN) Log.e(ScruffActivity.TAG, "Exception setting up exception handler! " + e.toString()); } } } 

然后我定义我的任务如下:

 private class MyTask extends LoggingAsyncTask<Void, Void, HashMap> { protected HashMap doInBackground(Void... args) { this.setupUnhandledExceptionLogging(MyActivity.this.mContext); // do work return myHashMap; } } 

显然,你的任务可以采用这种模式所需的任何参数。 您可以定义RemoteUploadExceptionHandler来执行必要的日志记录/上传。

    我不会把它称为设计模式,只是包装doInBackground()并根据需要初始化和/或捕获exception。

     public abstract class AsyncTaskWrapper extends AsyncTask { protected Exception error; protected Result doInBackground(Params... params) { try { init(); return doRealWork(params); } catch (Exception e) { error = e; Log.e("TAG", e.getMessage(), e); return null; } } protected abstract void init(); protected abstract Result doRealWork(Params... params); } 

    esilver我陷阱exception并返回一个BoolString对象,一个无抛出模式

      //a utility class to signal success or failure, return an error message, and return a useful String value //see Try Out in C# public final class BoolString { public final boolean success; public final String err; public final String value; public BoolString(boolean success, String err, String value){ this.success= success; this.err= err; this.value= value; } } 

    用法:

     private class MyAsynch extends AsyncTask{ protected BoolString doInBackground(String...strings) { // <== DO NOT TOUCH THE UI VIEW HERE return model.tryMyMethod(...); // <== return value BoolString result is sent to onPostExecute } protected void onPostExecute(BoolString result){ progress.dismiss(); if (result.success){ ... continue with result.value } else { ..log result.err } } // NO THROWS VERSION Helper method public BoolString tryMyMethod(...) { try { String value= MyMethod(...); return new BoolString(true,"",value); } catch (Exception e){ return new BoolString(false,e.getMessage(),""); } }