exception - Android - Crash report notification -
how can show notification shows when application crashed (or service crashed), , if user click on it, send stacktrace email?
saw applications that.
make own class extending interface uncaughtexceptionhandler
public class uncaughtexception implements uncaughtexceptionhandler { private context context; private static context context1; public uncaughtexception(context ctx) { context = ctx; context1 = ctx; } private statfs getstatfs() { file path = environment.getdatadirectory(); return new statfs(path.getpath()); } private long getavailableinternalmemorysize(statfs stat) { long blocksize = stat.getblocksize(); long availableblocks = stat.getavailableblocks(); return availableblocks * blocksize; } private long gettotalinternalmemorysize(statfs stat) { long blocksize = stat.getblocksize(); long totalblocks = stat.getblockcount(); return totalblocks * blocksize; } private void addinformation(stringbuilder message) { message.append("locale: ").append(locale.getdefault()).append('\n'); try { packagemanager pm = context.getpackagemanager(); packageinfo pi; pi = pm.getpackageinfo(context.getpackagename(), 0); message.append("version: ").append(pi.versionname).append('\n'); message.append("package: ").append(pi.packagename).append('\n'); } catch ( exception e ) { log.e("customexceptionhandler", "error", e); message.append("could not version information ").append(context.getpackagename()); } message.append("phone model: ").append(android.os.build.model).append('\n'); message.append("android version: ").append(android.os.build.version.release).append('\n'); message.append("board: ").append(android.os.build.board).append('\n'); message.append("brand: ").append(android.os.build.brand).append('\n'); message.append("device: ").append(android.os.build.device).append('\n'); message.append("host: ").append(android.os.build.host).append('\n'); message.append("id: ").append(android.os.build.id).append('\n'); message.append("model: ").append(android.os.build.model).append('\n'); message.append("product: ").append(android.os.build.product).append('\n'); message.append("type: ").append(android.os.build.type).append('\n'); statfs stat = getstatfs(); message.append("total internal memory: ").append(gettotalinternalmemorysize(stat)).append('\n'); message.append("available internal memory: ").append(getavailableinternalmemorysize(stat)).append('\n'); } @override public void uncaughtexception(thread t, throwable e) { try { stringbuilder report = new stringbuilder(); date curdate = new date(); report.append("error report collected on : ").append(curdate.tostring()).append('\n').append('\n'); report.append("informations :").append('\n'); addinformation(report); report.append('\n').append('\n'); report.append("stack:\n"); final writer result = new stringwriter(); final printwriter printwriter = new printwriter(result); e.printstacktrace(printwriter); report.append(result.tostring()); printwriter.close(); report.append('\n'); report.append("**** end of current report ***"); log.e(uncaughtexception.class.getname(), "error while senderrormail" + report); senderrormail(report); } catch ( throwable ignore ) { log.e(uncaughtexception.class.getname(), "error while sending error e-mail", ignore); } } /** * method call alert dialog when application crashed! */ public void senderrormail(final stringbuilder errorcontent) { final alertdialog.builder builder = new alertdialog.builder(context); new thread() { @override public void run() { looper.prepare(); builder.settitle("sorry...!"); builder.create(); builder.setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { system.exit(0); } }); builder.setpositivebutton("report", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { intent sendintent = new intent(intent.action_send); string subject = "your app crashed! fix it!"; stringbuilder body = new stringbuilder("yoddle"); body.append('\n').append('\n'); body.append(errorcontent).append('\n').append('\n'); // sendintent.settype("text/plain"); sendintent.settype("message/rfc822"); sendintent.putextra(intent.extra_email, new string[] { "yourmail@domain.com" }); sendintent.putextra(intent.extra_text, body.tostring()); sendintent.putextra(intent.extra_subject, subject); sendintent.settype("message/rfc822"); context1.startactivity(sendintent); system.exit(0); } }); builder.setmessage("oops,your application has crashed"); builder.show(); looper.loop(); } }.start(); } }
set customexceptionhandler defaultexceptionhandler
in mainactivity
import android.app.activity; import android.os.bundle; import android.view.menu; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); thread.setdefaultuncaughtexceptionhandler(new uncaughtexception(mainactivity.this)); int y = 5 / 0; } }
note :
the above example code sends email crash data. can modify meet need.
Comments
Post a Comment