在 AsyncTask 中捕获未处理异常的设计模式
各位,
我通过 onCreate 顶部的这样的代码片段捕获未处理的 Android 异常:
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 中发生的未处理的异常是抓住&已记录。
问题是,因为 AsyncTask 采用任意类型初始值设定项,所以我不确定如何声明一个超类(我的所有 AsyncTask 都继承自该超类)来设置此未处理的异常处理程序。
任何人都可以推荐一个好的设计模式来处理 AsyncTask 的 doInBackground 方法中未处理的异常,该模式不涉及为每个新的 AsyncTask 定义复制和粘贴上面的代码吗?
谢谢!
更新
这是我在仔细查看来源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<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
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<String, Object>> {
protected HashMap<String, Object> doInBackground(Void... args) {
this.setupUnhandledExceptionLogging(MyActivity.this.mContext);
// do work
return myHashMap;
}
}
显然,您的任务可以采用此模式所需的任何参数。您可以定义 RemoteUploadExceptionHandler 来执行必要的日志记录/上传。
Folks,
I catch unhandled Android exceptions via a code snippet like this, at the top of onCreate:
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());
}
I'd like to come up with something similar for about two dozen AsyncTasks I use in my android application, so unhandled exceptions that occur in doInBackground are caught & logged.
Problem is, because AsyncTask takes arbitrary type initializers, I am not sure how to declare a superclass, from which all my AsyncTasks inherit, that sets up this unhandled exception handler.
Can anyone recommend a good design pattern for handling unhandled exceptions in the doInBackground method of AsyncTask that does not involve copy-and-paste of code like that above for every new AsyncTask definition?
Thanks!
UPDATE
Here is the design pattern I used, after looking more closely at the source of 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<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
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());
}
}
}
Then I define my tasks as follows:
private class MyTask extends LoggingAsyncTask<Void, Void, HashMap<String, Object>> {
protected HashMap<String, Object> doInBackground(Void... args) {
this.setupUnhandledExceptionLogging(MyActivity.this.mContext);
// do work
return myHashMap;
}
}
Obviously your task can take whatever params necessary with this pattern. It's up to you to define RemoteUploadExceptionHandler to do the necessary logging/uploading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不会将其称为设计模式,而只是包装
doInBackground()
并根据需要初始化和/或捕获异常。I wouldn't go as far as to call it a design pattern, but just wrap
doInBackground()
and initialize and/or catch exceptions as necessary.esilver 我捕获异常并返回一个 BoolString 对象,一个无抛出模式
用法:
esilver I trap for exceptions and return a BoolString object, a no throws pattern
USAGE: