CrashHandler:
package itant.com.cashtest;
import android.util.Log;
/**
* Created by zhanzc on 2017/7/27.
*/
public class CrashHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
Log.e("bug", "crash");
//退出程序
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}
CrashApplication:(记得要在AndroidManifest.xml文件里注册)
package itant.com.cashtest;
import android.app.Application;
import android.util.Log;
/**
* Created by zhanzc on 2017/7/27.
*/
public class CrashApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Log.e("bug", "application oncreate");
Thread.setDefaultUncaughtExceptionHandler(new CrashHandler());
}
}
然后故意制造一个崩溃,比如:
Method method = null;
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
try {
method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class);
if (method != null) {
method.invoke(manager, getPackageName());
}
} catch (Exception e) {
Log.e("bug", e.getMessage());
}
本文介绍了一个自定义的Android应用崩溃处理机制,通过实现CrashHandler类来捕获未捕获的异常并记录日志,最后强制关闭应用程序。此外,还展示了如何在AndroidManifest.xml中配置CrashApplication以启用此崩溃处理机制。
755

被折叠的 条评论
为什么被折叠?



