Tuesday, September 11, 2012

Android UncaughtExceptionHandler

Android UncaughtExceptionHandler

Implemented by objects that want to handle cases where a thread is being terminated by an uncaught exception. Upon such termination, the handler is notified of the terminating thread and causal exception. If there is no explicit handler set then the thread's group is the default handler. 

Below i wrote the code user can send some bug report to Developer when application crashed.

Activity Code

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.ViewFlipper;
* @author vijayakumar
public class AndroidMADQAActivity extends Activity {
ViewFlipper flipper;
TextView textView = null;
Throwable throwable;
UnCaughtException un = null;
public void onCreate(Bundle savedInstanceState) {
Thread.setDefaultUncaughtExceptionHandler(new UnCaughtException(AndroidMADQAActivity.this));
Integer[] items = { R.drawable.a, R.drawable.e,R.drawable.d,R.drawable.c};
textView.setText("Helloo Error Welcome");

Screen Shot

User Got Alert When Application Crashed!

When User click Report Button

Developer Got Bug Report In EMail


package com.madqa;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.Date;
import java.util.Locale;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.os.Looper;
import android.os.StatFs;
import android.util.Log;
* {@link UncaughtExceptionHandler} send an e-mail with
* some debug information to the developer.
public class UnCaughtException implements UncaughtExceptionHandler {
private static final String RECIPIENT = "iamvijayakumar@gmail.com";
private Thread.UncaughtExceptionHandler previousHandler;
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 get Version information for ").append(
message.append("Phone Model: ").append(android.os.Build.MODEL).append(
message.append("Android Version: ").append(
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(
message.append("Type: ").append(android.os.Build.TYPE).append('\n');
StatFs stat = getStatFs();
message.append("Total Internal memory: ").append(
message.append("Available Internal memory: ").append(
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');
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
report.append("**** End of current Report ***");
"Error while sendErrorMail"+report);
} catch (Throwable ignore) {
"Error while sending error e-mail", ignore);
// previousHandler.uncaughtException(t, e);
* This method for call alert dialog when application crashed!
* @author vijayakumar
public void sendErrorMail(final StringBuilder errorContent) {
final AlertDialog.Builder builder= new AlertDialog.Builder(context);
new Thread(){
public void run() {
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
builder.setPositiveButton("Report", new DialogInterface.OnClickListener() {
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");
// sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { RECIPIENT });
sendIntent.putExtra(Intent.EXTRA_TEXT, body.toString());
sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
// context.startActivity(Intent.createChooser(sendIntent, "Error Report"));
builder.setMessage("Unfortunately,This application has stopped");

Download Source Code


  1. Thanks for all the information, it was very helpful i really like that you are providing information on android app development ,being enrolled in android app development for beginners http://www.wiziq.com/course/3303-android-app-development-for-beginners i was looking for such android app development online course to assist me and your information helped me a lot. I really like that you are providing such information,thanks.

  2. hi vijaykumar,
    i need help you and hope you will.
    i want to send mail form android intent.
    mail is sending very well . but turning point is ass CC with sender's default id.
    for that i have use given like.

    but did not working.
    in accounts[] i get []. null . no mail id are get in that array. so , where are that problem ? i did't get .

    plz help me.

  3. Hello Anonymous,

    Please Check your permission details in Manifest.

  4. hi i m chintan .i have add permission in manifest.
    i can send mail very well. but same mail should be send to sender's id. so i that. i have use account manager but didn't get output.

  5. This is code for getting sender's id.

    Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
    Account[] accounts = AccountManager.get(getApplicationContext()).getAccounts();
    for (Account account : accounts)
    if (emailPattern.matcher(account.name).matches())
    possibleEmail = account.name;

  6. Hi chintan,
    I will check then i will let you know...

  7. thinks vijaykumar .i will wait for positive reply. because had so much try but did not get output.
    thanks again

  8. hi vijaykumar ,
    i have one new query regarding sending email.
    i used html format in Email .its looks good in body area and sending successfully from Gmail accounts. but same code are not supported for any other mail clients . like yahoo , hotmail , etc .
    so what should be that solution . This two things are very depress to me . i hope you will help me.
    thanks in again.

  9. hi vijaykumar. i get user's Email id form device only if user select Gmail . other wise not and same if i send mail from Gmail then it shows my html format nicely. so , is that any rule that user can't access email client properties. ?

  10. hi vijaykumar . i am chintan. i got the result of above mail query. problem was that i am jst checking in emulator. its work beautifully in device.i got the result in next days and forget to remand you.sorry for late .By the way thanks because you was only person who help me at that time. thank you AGAIN.

  11. Only several of such organizations help in getting you an car finance even if
    you might have a bad credit score score or no credit ranking short term loans as annoying because your monthly premiums
    are, student loan debt isn't all that bad.

  12. Thank you, your code is working!

  13. Hi,
    I used this source code but when i click the Report button it is not going for sending email instead it again prints the exception details in logcat. If I click cancel it does not get finished instead it again prints exception details in logcat and again displays the dialog box.

    Can you please give me solution for this?



Check out this may be help you

Related Posts Plugin for WordPress, Blogger...