Advertisement
PeterMinin

Android exception handler

Jul 3rd, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. // ExceptionHandler.java
  2.  
  3. import java.lang.Thread.UncaughtExceptionHandler;
  4.  
  5. import android.app.Application;
  6. import android.app.NotificationManager;
  7. import android.content.Context;
  8. import android.os.Process;
  9.  
  10. public class ExceptionHandler implements UncaughtExceptionHandler {
  11.  
  12.     private static Application application = null;
  13.  
  14.     public ExceptionHandler(Application application) {
  15.         ExceptionHandler.application = application;
  16.     }
  17.  
  18.     public void uncaughtException(Thread thread, Throwable ex) {
  19.         addBugReport(ex);
  20.         showReportNotification();
  21.         Process.killProcess(Process.myPid());
  22.     }
  23.  
  24.     private static void addBugReport(Throwable e) {
  25.         // ...
  26.     }
  27.  
  28.     private void showReportNotification() {
  29.         NotificationManager nm = (NotificationManager) application
  30.                 .getSystemService(Context.NOTIFICATION_SERVICE);
  31.         // ...
  32.     }
  33.  
  34. }
  35.  
  36. // BugReportingApplication.java
  37.  
  38. import android.app.Application;
  39.  
  40. public class BugReportingApplication extends Application {
  41.  
  42.     @Override
  43.     public void onCreate() {
  44.         super.onCreate();
  45.         Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
  46.     }
  47.  
  48. }
  49.  
  50. // AndroidManifest.xml
  51.  
  52. <?xml version="1.0" encoding="utf-8"?>
  53. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  54.     package="..."
  55.     android:versionCode="1"
  56.     android:versionName="1.0" >
  57.  
  58.     <!-- ... -->
  59.  
  60.     <application
  61.         android:name="BugReportingApplication"
  62.         android:icon="@drawable/logo"
  63.         android:label="@string/app_name" >
  64.  
  65.     <!-- ... -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement