gpt4 book ai didi

android - 使用 Roboguice 在 Android 应用程序中进行全局错误处理

转载 作者:行者123 更新时间:2023-12-03 09:00:07 24 4
gpt4 key购买 nike

我正在开发一个使用 Roboguice 的 Android 应用程序。依赖注入(inject)框架。
所以大多数时候我们扩展 RoboActivity、RoboListActivity 和类似的。

现在我想介绍某种全局错误处理,它会在应用程序崩溃时显示一些警报或错误 Activity 。

我之前通过实现这样的基本 Activity 来做到这一点:

public class BaseActivity extends Activity
{


@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new GeneralError(this));
}

我在其中定义了默认异常处理程序和所有其他 Activity ,然后从该 Activity 派生。

现在我想知道如何使用 Roboguice 实现这一点?

最佳答案

这是一些粗略的伪代码,可以帮助您入门。它使用 roboguice events使其中一些横切关注点更容易一些。

public class GlobalErrorHandler {
// injects the current activity here
@Inject Context context;


public void onCreate(@Observes OnCreateEvent e) {
// Wires up the error handling
Thread.setDefaultUncaughtExceptionHandler(new GeneralError(context));
}
}

public class MySpecificActivity {
// required in every activity that needs error handling
@Inject GlobalErrorHandler errorHandler;

}

关于android - 使用 Roboguice 在 Android 应用程序中进行全局错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8476033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com