Pages

Saturday, 21 May 2011

Handle the Force Close and other UncaughtException of Your Application.

Some times we don't handle the all exception properly so and user may get force close for some exception that we don't handle so here is solution handle that all exceptions.

First we need to implements the UncaughtExceptionHandler class and using uncaughtException method to capture the exception.

now you need to set your default UncaughtException Handler in every activity where you want to capture uncaught exception but it may inconvenient to write in every activity.

so you can create one Base activity class that extends Activity class and other activities extends that base class activity.

/**

* BaseActivity the other activities will extends this activity class.

* Here we implement the UncaughtExeptionHandler and register the Default Exception Handler in onCreate of the Activity.

* @author 

*/




public class BaseActivity extends Activity{

@Override



protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub



super.onCreate(savedInstanceState);



Thread.setDefaultUncaughtExceptionHandler(new TopExceptionHandler(this));

}



public class TopExceptionHandler implements Thread.UncaughtExceptionHandler {







public TopExceptionHandler(Activity app) {

Thread.getDefaultUncaughtExceptionHandler();

}







public void uncaughtException(Thread t, Throwable e)

{




StackTraceElement[] arr = e.getStackTrace();

String report = e.toString()+"\n\n";

report += "--------- Stack trace ---------\n\n";

for (int i=0; i<arr.length; i++)

{

report += "    "+arr[i].toString()+"\n";

}

report += "-------------------------------\n\n";




// If the exception was thrown in a background thread inside

// AsyncTask, then the actual exception can be found with getCause



report += "--------- Cause ---------\n\n";




Throwable cause = e.getCause();

if(cause != null) {

report += cause.toString() + "\n\n";

arr = cause.getStackTrace();

for (int i=0; i<arr.length; i++)

{

report += "    "+arr[i].toString()+"\n";

}

}



/**

* Getting the Device brand,model and sdk verion details. 

*/



report += "-------------------------------\n\n";




report += "-------------------------------\n\n";

report += "--------- Device ---------\n\n";

report += "Brand: " + Build.BRAND + "\n";

report += "Device: " + Build.DEVICE + "\n";

report += "Model: " + Build.MODEL + "\n";

report += "Id: " + Build.ID + "\n";

report += "Product: " + Build.PRODUCT + "\n";

report += "-------------------------------\n\n";

report += "--------- Firmware ---------\n\n";

report += "SDK: " + Build.VERSION.SDK + "\n";

report += "Release: " + Build.VERSION.RELEASE + "\n";

report += "Incremental: " + Build.VERSION.INCREMENTAL + "\n";

report += "-------------------------------\n\n";




Log.e("Report ::",report);





Intent i1 = new Intent(BaseActivity.this,Error.class);

i1.putExtra("Error",report.toString());

i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

i1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(i1);



System.exit(0);//If you want to restart activity and want to kill after crash.s




}

}

}

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Demo Activity that will display the ListView and footer.
 * @author 
 */

public class Demo extends BaseActivity {

 TextView textView;
 Button Ok;
 String temp;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);


  textView = (TextView)findViewById(R.id.TextView01);
  Ok = (Button)findViewById(R.id.Button01);
  Ok.setOnClickListener(new View.OnClickListener(){

   public void onClick(View v) {
    // TODO Auto-generated method stub
    
   }
   
  });
 }
}
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Error class just want to show this screen when some unwanted Exception got caught.
 * @author 
 *
 */


public class Error extends BaseActivity{
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  TextView textView = (TextView)findViewById(R.id.TextView02);
  textView.setText("App Got Crashed");
  
  
  
  Intent sendIntent = new Intent(Intent.ACTION_SEND);
  String subject = "Error report";
  String body =
   "Crash Report: "+
   "\n\n"+
   getIntent().getExtras().getString("Error").toString()+
   "\n\n";

  sendIntent.putExtra(Intent.EXTRA_EMAIL,
    new String[] {"enter your email id here"});
  sendIntent.putExtra(Intent.EXTRA_TEXT, body);
  sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
  sendIntent.setType("message/rfc822");

  Error.this.startActivity(
    Intent.createChooser(sendIntent, "Title:"));

  Button okButton = (Button)findViewById(R.id.okBT);
  
  
  
  okButton.setOnClickListener(new View.OnClickListener(){

   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent i1 = new Intent(Error.this,ViewFlippers.class);
    i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i1);
    finish();
   }
  });
 }
}
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;

public class ViewFlippers extends BaseActivity{
 
 Button ok;
 ViewFlipper viewFlipper;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  setContentView(R.layout.viewflipper);
  
  ok=(Button)findViewById(R.id.Button01);
  viewFlipper = (ViewFlipper) findViewById(R.id.ViewF);
  
  ok.setOnClickListener(new View.OnClickListener(){
   public void onClick(View v) {
    startActivity(new Intent(ViewFlippers.this,Demo.class));
   }
  });
 }
}

    
        
        
    
 
  
  
 


    
    
        
        
        
    


Android Manifest:





 
  
   
    
    
   
  
  
  
  
  

 

2 comments:

  1. This is realy a Nice blog post read on of my blogs It is really helpful article please read it too my blog Facebook app crashing. you can visits our websites or toll free no +1-866-558-4555. solve your problem fastly.

    ReplyDelete

  2. It is really helpful article please read it too my blog FACEBOOK APP CRASHING.

    ReplyDelete