android异常捕获,上线前的操作

    xiaoxiao2025-01-20  1

    自定义一个application然后再清单文件中的 application 标签下添加name = MyApplication public class MyApplication extends Application { //注意必须在清单文件中application的标签name 写成我们自定义的名字!!!否则下面操作无效... @Override public void onCreate() { super.onCreate(); //捕获异常方法 Thread.currentThread().setUncaughtExceptionHandler(new MyUncaughtException()); } //异常捕获 private class MyUncaughtException implements Thread.UncaughtExceptionHandler{ @Override public void uncaughtException(Thread thread, Throwable ex) { try { ex.printStackTrace(new PrintStream(new File(getFilesDir() , "log.txt"))); } catch (FileNotFoundException e) { e.printStackTrace(); } //当有异常的时候自己吧自己杀死 闪退! Process.killProcess(Process.myPid()); } } } 注意清单文件!!! <application android:name=".MyApplication"//这个一定要添加!!!! android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
    转载请注明原文地址: https://ju.6miu.com/read-1295659.html
    最新回复(0)