1

I have a library module "A" which is initialized on the application.onCreate() of android application (wrapper). In case there is any uncaught exception in module "A", it sends a broadcast which should be received by a receiver in module "B" which in-turn sends the info to the server.

Now the problem is that when an exception is caught in module "B", it sends the broadcast but it is never received and the apps hangs.

My code in onCreate() of module A's only activity. I am using LocalBroadcastManager to send the broadcasts.

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable ex) {
                trackCardInfoSessionEnd(CardInfoAnalyticsConstants.CARD_INFO_BROADCAST_SESSION_END, "failed");
                finish();
            }
        });
Rahul
  • 878
  • 9
  • 15

1 Answers1

0

If you have multiple activity set uncaught handler in your application class, inside handler you can handle the log, exit the current app and start new app/activity to send bug report.

public class MyApp extends Application {

@Override
public void onCreate() {
    super.onCreate();

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            trackCardInfoSessionEnd(CardInfoAnalyticsConstants.CARD_INFO_BROADCAST_SESSION_END, "failed");
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(10);
        }
    });
}

}

Please check this link.

Community
  • 1
  • 1
Praveen
  • 697
  • 6
  • 21
  • Not the answer I am looking for. android.os.Process.killProcess(android.os.Process.myPid()); or System.exit(10); just kills the app so the broadcast-receiver is not able to receive the intent and do the processing. I need the processing to be done while not killing the app but just finishing the activity. – Rahul Sep 22 '16 at 11:36
  • 1
    1. All android components(activity, service, broadcast receiver) runs on main thread. 2. Uncaught exception are handled based on the thread in which they occur. 3. If exception occurs on a thread the dispatchUncaughtException handler is called of that thread, and thread is terminated. http://www.javamex.com/tutorials/exceptions/exceptions_uncaught_handler.shtml . 4. if exception occurs on the main thread, the main thread will be terminated so the app's other component. However you can restart the component using pending intent and alarm manager, but that would not be a correct use case. – Praveen Sep 22 '16 at 17:15
  • I was thinking of like -> storing the "broadcast action to be sent" on crash -> let the app crash - > on next start send the broadcast. – Rahul Sep 23 '16 at 05:11
  • sticky broadcast is deprecated in API 21. if you have to log the crash report, broadcast receiver is not required. why do you need a broadcast receiver after a crash? – Praveen Sep 23 '16 at 10:06
  • Check the question. It is a project specific thingy. – Rahul Sep 27 '16 at 03:54