app上線后并不能保證程序不會崩潰,為了更好的優(yōu)化項目回俐,需要獲取異常日志逛腿,供開發(fā)人員修改bug稀并,所以希望在程序崩潰時,能捕獲異常单默,并保存異常碘举。這些需求可以用第三方的框架,比如騰訊的bugly雕凹,github上也有些開源的等殴俱,但如果不想經(jīng)過第三方的后臺,或者想自己寫個效果枚抵,可以考慮采用本文提供的方法---考慮網(wǎng)絡異常時线欲,崩潰信息不能上傳后臺,用數(shù)據(jù)庫存儲異常日志汽摹,當開啟應用或有網(wǎng)絡時李丰,上傳異常日志。同時逼泣,本文也提供了通過郵件來接收異常日志的方法趴泌。
地址:歡迎star https://github.com/comeonwyf/SteveCrashHandleDemo
一、捕獲異常部分
第一步:定義 CrashException 模型拉庶,用于異常信息對象的創(chuàng)建
該類說明:注釋代碼是greendao的方式(不熟悉嗜憔,可先去了解greendao的簡單使用方法)
@Entity
public class CrashException {
@Transient
public static final int TYPE_TOSEND = 1;
@Transient
public static final int TYPE_SENDING = 2;
@Id
private Long crashId;
private String crashTime;
private String appName;
private String appVersion;
private String OSVersion;
private String mobileBrand;
private String mobileModel;
private String crashExceptionInfor;
private int sendStat = 1;//1表示待發(fā)送,2表示正在發(fā)送
@Generated(hash = 1063750127)
public CrashException(Long crashId, String crashTime, String appName,
String appVersion, String OSVersion, String mobileBrand,
String mobileModel, String crashExceptionInfor, int sendStat) {
this.crashId = crashId;
this.crashTime = crashTime;
this.appName = appName;
this.appVersion = appVersion;
this.OSVersion = OSVersion;
this.mobileBrand = mobileBrand;
this.mobileModel = mobileModel;
this.crashExceptionInfor = crashExceptionInfor;
this.sendStat = sendStat;
}
@Generated(hash = 2138202335)
public CrashException() {
}
public void setCrashExceptionInfor(String crashExceptionInfor) {
this.crashExceptionInfor = crashExceptionInfor;
}
public static Long creatCrashId(){
return (Long)((System.currentTimeMillis()+17)/3);
}
public Long getCrashId() {
return this.crashId;
}
public void setCrashId(Long crashId) {
this.crashId = crashId;
}
public String getCrashTime() {
return this.crashTime;
}
public void setCrashTime(String crashTime) {
this.crashTime = crashTime;
}
public String getAppName() {
return this.appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getAppVersion() {
return this.appVersion;
}
public void setAppVersion(String appVersion) {
this.appVersion = appVersion;
}
public String getOSVersion() {
return this.OSVersion;
}
public void setOSVersion(String OSVersion) {
this.OSVersion = OSVersion;
}
public String getMobileBrand() {
return this.mobileBrand;
}
public void setMobileBrand(String mobileBrand) {
this.mobileBrand = mobileBrand;
}
public String getMobileModel() {
return this.mobileModel;
}
public void setMobileModel(String mobileModel) {
this.mobileModel = mobileModel;
}
public String getCrashExceptionInfor() {
return this.crashExceptionInfor;
}
public int getSendStat() {
return this.sendStat;
}
public void setSendStat(int sendStat) {
this.sendStat = sendStat;
}
}
第二步:自定義AppUncaughtExceptionHandler 類實現(xiàn)Thread.UncaughtExceptionHandler 接口 此處會獲得崩潰異常
該類主要:采用單例模式氏仗,初始化時吉捶,將該類設置為默認處理器,用于監(jiān)聽異常的回調皆尔,獲取異常呐舔。獲取異常后,存入數(shù)據(jù)庫慷蠕,并將異常信息發(fā)送給指定郵箱珊拼。
public class AppUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
//用于輸出異常信息
private PrintWriter pw;
private StringWriter sw;
//構造方法私有,防止外部構造多個實例流炕,即采用單例模式
private AppUncaughtExceptionHandler() {
}
//程序的Context對象
private Context applicationContext;
private volatile boolean crashing;
/**
* 日期格式器
*/
private DateFormat mFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 系統(tǒng)默認的UncaughtException處理類
*/
private Thread.UncaughtExceptionHandler mDefaultHandler;
/**
* 單例
*/
private static AppUncaughtExceptionHandler sAppUncaughtExceptionHandler;
public static synchronized AppUncaughtExceptionHandler getInstance() {
if (sAppUncaughtExceptionHandler == null) {
synchronized (AppUncaughtExceptionHandler.class) {
if (sAppUncaughtExceptionHandler == null) {
sAppUncaughtExceptionHandler = new AppUncaughtExceptionHandler();
}
}
}
return sAppUncaughtExceptionHandler;
}
/**
* 初始化
* @param context
*/
public void init(Context context) {
applicationContext = context.getApplicationContext();
crashing = false;
sw = new StringWriter();
pw = new PrintWriter(sw);
//獲取系統(tǒng)默認的UncaughtException處理器
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
//設置該CrashHandler為程序的默認處理器
Thread.setDefaultUncaughtExceptionHandler(this);
}
//捕獲異常
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (crashing) {
return;
}
crashing = true;
// 打印異常信息
ex.printStackTrace();
// 我們沒有處理異常 并且默認異常處理不為空 則交給系統(tǒng)處理
if (!handlelException(ex) && mDefaultHandler != null) {
// 系統(tǒng)處理
mDefaultHandler.uncaughtException(thread, ex);
}
byebye();
}
private void byebye() {
SystemClock.sleep(2000);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
private boolean handlelException(Throwable ex) {
if (ex == null) {
return false;
}
try {
//處理異常信息澎现,并存入數(shù)據(jù)庫,然后發(fā)送至服務器
handleCrashReport(ex);
// 提示對話框
showPatchDialog();
} catch (Exception e) {
return false;
}
return true;
}
/**
*處理異常信息每辟,并存入數(shù)據(jù)庫昔头,然后發(fā)送至郵箱
* @param ex
* @return
*/
private String handleCrashReport(Throwable ex) {
String exceptionStr = "";
PackageInfo pinfo = CrashApp.getInstance().getLocalPackageInfo();
CrashException crashException = null;
if (pinfo != null) {
if (ex != null) {
//得到異常全部信息
if(sw==null){
sw = new StringWriter();
}
if(pw==null){
pw = new PrintWriter(sw);
}
ex.printStackTrace(pw);
String errorStr = sw.toString();
if (TextUtils.isEmpty(errorStr)) {
errorStr = ex.getMessage();
}
if (TextUtils.isEmpty(errorStr)) {
errorStr = ex.toString();
}
exceptionStr = errorStr;
//存入數(shù)據(jù)庫中
crashException = new CrashException(CrashException.creatCrashId(),
mFormatter.format(new Date()),getApplicationName(applicationContext),pinfo.versionName,
Build.VERSION.RELEASE,Build.MANUFACTURER,Build.MODEL,errorStr,CrashException.TYPE_TOSEND);
Log.e("print", "異常信息: "+ crashException.getCrashExceptionInfor());
//發(fā)送到郵箱
SendExceptionManager.getInstance().sendToEmail(crashException);
} else {
exceptionStr = "no exception. Throwable is null";
}
return exceptionStr;
} else {
return "";
}
}
//崩潰后彈出提示框,是否取消還是重啟
private void showPatchDialog() {
Intent intent = PatchDialogActivity.newIntent(applicationContext, getApplicationName(applicationContext), null);
applicationContext.startActivity(intent);
}
//獲取應用名稱
private String getApplicationName(Context context) {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo = null;
String name = null;
try {
applicationInfo = packageManager.getApplicationInfo(
context.getApplicationInfo().packageName, 0);
name = (String) packageManager.getApplicationLabel(applicationInfo);
} catch (final PackageManager.NameNotFoundException e) {
String[] packages = context.getPackageName().split(".");
name = packages[packages.length - 1];
}
return name;
}
}
此處感謝:http://www.reibang.com/p/fb28a5322d8a
二影兽、定義CrashApp繼承Application(注意需在AndroidManifest.xml引用此App)
該類主要是完成GreenDao 和AppUncaughtExceptionHandler初始化工作
public class CrashApp extends Application {
private static CrashApp mInstance = null;
private static DaoSession daoSession;
public static CrashApp getInstance() {
if (mInstance == null) {
throw new IllegalStateException("Application is not created.");
}
return mInstance;
}
public static DaoSession getDaoInstance(){
if (daoSession == null) {
throw new IllegalStateException("GreenDao is not created.");
}
return daoSession;
}
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
// 捕捉異常初始化
AppUncaughtExceptionHandler crashHandler = AppUncaughtExceptionHandler.getInstance();
crashHandler.init(getApplicationContext());
//初始化greendao數(shù)據(jù)庫
setupDatabase();
}
/**
* 獲取自身App安裝包信息
* @return
*/
public PackageInfo getLocalPackageInfo() {
return getPackageInfo(getPackageName());
}
/**
* 獲取App安裝包信息
* @return
*/
public PackageInfo getPackageInfo(String packageName) {
PackageInfo info = null;
try {
info = getPackageManager().getPackageInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return info;
}
private void setupDatabase() {
//創(chuàng)建數(shù)據(jù)庫myDB.db
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(mInstance,"myDB.db");
SQLiteDatabase db = devOpenHelper.getWritableDatabase();
//獲取數(shù)據(jù)庫對象
DaoMaster daoMaster = new DaoMaster(db);
//獲取Dao對象的管理者
daoSession = daoMaster.newSession();
}
}
三揭斧、在應用的啟動界面,監(jiān)聽網(wǎng)絡狀態(tài),當網(wǎng)絡可用時讹开,檢查是否有需要發(fā)送的異常日志盅视,如果有,就發(fā)送旦万,發(fā)送成功后闹击,刪除對應日志
public class MainActivity extends AppCompatActivity {
private NetworkBroadcastReceiver mReceiver;
private boolean doing;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerNetworkReceiver();
}
public void onClick(View view){
if(view.getId()== R.id.tv1){
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);
}
if(view.getId()==R.id.tv2){
String test = null;
test.length();
}
}
private void registerNetworkReceiver() {
mReceiver = new NetworkBroadcastReceiver();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mReceiver,filter);
mReceiver.setOnNetChange(new NetworkBroadcastReceiver.NetEvent() {
@Override
public void onNetChange(int netMobile) {
if(netMobile!=-1){
Log.e("print", "網(wǎng)絡變化:可用! ");
//網(wǎng)絡可以用的時候成艘,看看數(shù)據(jù)庫中是否有需要發(fā)送的錯誤日志
if(doing){
return;
}
doing = true;
List<CrashException> crashExceptionList = CrashExceptionHelper.getCrashExceptionList();
if(crashExceptionList!=null && crashExceptionList.size()!=0){
for(CrashException crashException : crashExceptionList){
crashException.setSendStat(CrashException.TYPE_SENDING);
SendExceptionManager.getInstance().sendToServer(crashException);
}
}
doing = false;
}else {
Log.e("print", "網(wǎng)絡變化:不可用赏半! ");
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mReceiver!=null){
unregisterReceiver(mReceiver);
}
}
}
四、補充細節(jié)部分:(1)發(fā)送異常信息的管理類淆两;(2)發(fā)送郵件需要有三個依賴包(在提供的github項目源碼中有)
發(fā)送異常信息的管理類
public class SendExceptionManager {
/**
* 單例
*/
private static SendExceptionManager mSendExceptionManager;
private static ScheduledExecutorService scheduledThreadPool;
public static Context applicationContext;
private SendExceptionManager() {
//創(chuàng)建定長的5個線程
scheduledThreadPool = Executors.newScheduledThreadPool(5);
}
public static synchronized SendExceptionManager getInstance() {
if (mSendExceptionManager == null) {
synchronized (SendExceptionManager.class) {
if (mSendExceptionManager == null) {
mSendExceptionManager = new SendExceptionManager();
}
}
}
return mSendExceptionManager;
}
//發(fā)送到郵箱
public void sendToEmail(final CrashException crashException) {
if (scheduledThreadPool == null) {
scheduledThreadPool = Executors.newScheduledThreadPool(5);
}
scheduledThreadPool.execute(new Runnable() {
@Override
public void run() {
//賬號密碼
//用此郵箱來發(fā)送郵件(賬號:********@qq.com , 密碼:(開啟POP3/SMTP服務的授權碼))
Mail mail = new Mail("填賬號", "填授權碼");
//接受者郵箱 可以是多個
mail.set_to(new String[]{"******@qq.com","******@hotmail.com"});
//郵件來源
mail.set_from("******@qq.com");
//設置主題標題
mail.set_subject(getApplicationName(CrashApp.getInstance()) + "錯誤日志");
mail.setBody(crashException.toString());
try {
if (mail.send()) {
Log.e("crashInfor: ", "發(fā)送郵件成功");
} else {
Log.e("crashInfor:", "發(fā)送郵件失敗");
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//發(fā)送到服務器(Ion)
public void sendToServer(final CrashException crashException) {
String device = crashException.getMobileBrand()+"--"+crashException.getMobileModel()+"--"+crashException.getOSVersion();
Ion.with(CrashApp.getInstance())
.load("url")//填寫后臺的的地址.
.setTimeout(3000)
.setBodyParameter("note", crashException.getCrashExceptionInfor())
.setBodyParameter("device",device)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
crashException.setSendStat(CrashException.TYPE_TOSEND);
if (e != null) {
CrashExceptionHelper.addCrashException(crashException);
}
if (result != null) {
Log.e("crashInfor:", "崩潰信息上傳成功:"+result);
Boolean stat = result.get("success").getAsBoolean();
if(stat){
CrashExceptionHelper.deleteCrashException(crashException);
}else {
CrashExceptionHelper.addCrashException(crashException);
}
} else {
Log.e("crashInfor:", "崩潰信息上傳失敗");
CrashExceptionHelper.addCrashException(crashException);
}
}
});
}
private static String getApplicationName(Context context) {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo = null;
String name = null;
try {
applicationInfo = packageManager.getApplicationInfo(
context.getApplicationInfo().packageName, 0);
name = (String) packageManager.getApplicationLabel(applicationInfo);
} catch (final PackageManager.NameNotFoundException e) {
String[] packages = context.getPackageName().split(".");
name = packages[packages.length - 1];
}
return name;
}
}
發(fā)送郵件:注意在Mail類中按實際 修改 _host = "smtp.qq.com"
public class Mail extends javax.mail.Authenticator{
private String _user;
private String _pass;
private String[] _to;
private String _from;
private String _port;
private String _sport;
private String _host;
private String _subject;
private String _body;
private boolean _auth;
private boolean _debuggable;
private Multipart _multipart;
public Mail() {
//以下三個才有可能需要改動断箫,我是用qq郵箱發(fā)送,就是如下設置秋冰,比如你也可以用126郵箱 則改為smtp.126.com
// default smtp server
_host = "smtp.qq.com";
// default smtp port
_port = "465";
// default socketfactory port
_sport = "465";
_user = ""; // username
_pass = ""; // password
_from = ""; // email sent from
_subject = ""; // email subject
_body = ""; // email body
_debuggable = false; // debug mode on or off - default off
_auth = true; // smtp authentication - default on
_multipart = new MimeMultipart();
// There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
}
public Mail(String user, String pass) {
this();
_user = user;
_pass = pass;
}
public boolean send() throws Exception {
Properties props = _setProperties();
if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {
Session session = Session.getInstance(props, this);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(_from));
InternetAddress[] addressTo = new InternetAddress[_to.length];
for (int i = 0; i < _to.length; i++) {
addressTo[i] = new InternetAddress(_to[i]);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(_subject);
msg.setSentDate(new Date());
// setup message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(_body);
_multipart.addBodyPart(messageBodyPart);
// Put parts in message
msg.setContent(_multipart);
// send email
Transport.send(msg);
return true;
} else {
return false;
}
}
public void addAttachment(String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(_user, _pass);
}
private Properties _setProperties() {
Properties props = new Properties();
props.put("mail.smtp.host", _host);
if(_debuggable) {
props.put("mail.debug", "true");
}
if(_auth) {
props.put("mail.smtp.auth", "true");
}
props.put("mail.smtp.port", _port);
props.put("mail.smtp.socketFactory.port", _sport);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
return props;
}
// the getters and setters
public String getBody() {
return _body;
}
public void setBody(String _body) {
this._body = _body;
}
public void set_to(String[] _to) {
this._to = _to;
}
public void set_from(String _from) {
this._from = _from;
}
public void set_subject(String _subject) {
this._subject = _subject;
}
// more of the getters and setters …..
}