app上线后,后期维护显得尤为重要,今天给大家分享一下app上线后出现bug后的解决方法
1.继承Application类,重写onCreate方法
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.lang.Thread.UncaughtExceptionHandler;
import android.app.Application;
/**
* App上线后 bug的处理
*
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
System.out.println("application启动了....");
//捕获异常
Thread.currentThread().setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
}
private class MyUncaughtExceptionHandler implements UncaughtExceptionHandler {
//当有未捕获的异常的时候会调用
//Throwable : 其实Exception和Errorfu父类
@Override
public void uncaughtException(Thread thread, Throwable ex) {
System.out.println("哥捕获了异常");
//将异常保存到文件中
try {
//异常文件log.txt,可以判断返回给我们的服务器
ex.printStackTrace(new PrintStream(new File("/mnt/sdcard/log.txt")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//保存文件之后,自杀,myPid() : 获取自己的pid
android.os.Process.killProcess(android.os.Process.myPid());
}
}
}
2.AndroidManifest.xml中设置android:name=".MyApplication"属性
<application
android:name=".MyApplication"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<span style="white-space:pre"> </span><activity
android:name="com.demo.bug.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<!-- MAIN : 主界面,入口界面,其实java类型的main方法 -->
<action android:name="android.intent.action.MAIN" />
<!-- LAUNCHER : 在桌面上释放一个图标 -->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> ......
</application>