1

I tried to start an activity with an implicit intent after an uncaught exception with the unCaughtExceptionHandler. The intent should start an Activity as a Dialog in the same app that has crashed. This corresponds to the example listed in this thread:

Need to handle uncaught exception and send log file

I call the original unCaughtExceptionHandler at the end of my own handler procedure, like this:

public class ThisApplication extends Application
{
    Thread.UncaughtExceptionHandler originalUncaughtExceptionHandler;

    @Override
    public void onCreate ()
    {
        originalUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();

        Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
        {
            @Override
            public void uncaughtException (Thread thread, Throwable e)
            {
                handleUncaughtException (thread, e);
            }
        });
        super.onCreate();
    }

    public void handleUncaughtException (Thread thread, Throwable e)
    {
        e.printStackTrace();

        Intent intent = new Intent ();
        intent.setAction ("de.mydomain.myapp.action.PROCESS_LOG");

        intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);

        if (intent.resolveActivity(getPackageManager()) == null) {
            Log.d("ThisApplication","No receiver");
        } else {
            Log.d("ThisApplication", "Intent start");
            startActivity(intent);
        }

        originalUncaughtExceptionHandler.uncaughtException(thread, e);
    }
}

The result is, that after an Exception the standard Dialog is displayed that says something like "Unfortunately App xxx was closed". Behind that Dialog, in the background, I can see my Dialog that should be started with this intent "PROCESS_LOG". So obviously is was started, but the problem is, that after the standard Dialog has been closed, my custom dialog also closes. If I add

android:launchMode="singleInstance"

in the manifest of the dialog activity, the dialog is hidden, too, but it can be activated again when the app is selected from the recent apps menu. This seems to me as if the dialog is not started fully independently from the former app process/task.

Can somebody say what I did wrong?

This is the manifest part of the dialog activity:

<activity
    android:name=".ProcessLogActivity"
    android:windowSoftInputMode="stateHidden"
    android:theme="@style/ProcessLogActivity"
    android:process=":report_process"
    >
    <intent-filter>
        <action android:name="de.mydomain.myapp.action.PROCESS_LOG" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

The corresponding style:

<style name="ProcessLogActivity" parent="@style/Theme.AppCompat.Light.Dialog">
</style>

This is the Dialog Activity class:

public class ProcessLogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature (Window.FEATURE_NO_TITLE);
        setFinishOnTouchOutside (false);
        Log.d("ThisApplication", "Intent received");
        setContentView(R.layout.activity_process_log);
    }
}
Community
  • 1
  • 1
Harry500
  • 11
  • 1
  • You may be better served just plugging into ACRA. If nothing else, look at ACRA's implementation and determine how they handle it. Doing anything like this during an unhandled exception is tricky to get right, as the environment may be fairly screwed up (e.g., `OutOfMemoryError` is what triggered the crash). – CommonsWare Mar 02 '17 at 19:04
  • Thank you for your tip. I will take a look to ACRA. Nevertheless I would really be interested why it does not work, because in the other thread everybody seemed to get it run. However, I tested it with an Exception thrown by myself, so just a simple "empty" RuntimeException to test this scenario at first. – Harry500 Mar 02 '17 at 19:11
  • Do you know if it is possible with ACRA to display a custom dialog(Fregment) or activity shown as a dialog to display the report data and to offer several other functionalities as saving the report for example? Maybe with use of the integrated function of implementing an own report sender? Or is it not possible at this point to display any other own fragments/dialogs/activities anymore? – Harry500 Mar 03 '17 at 15:03
  • https://github.com/ACRA/acra/wiki/AdvancedUsage#custom-dialog – CommonsWare Mar 03 '17 at 15:07
  • Thank you for this link. I tried to use ACRA with the built-in dialog-functionality, but I could not get it work. But the built-in funtionality to show a "Toast" works! So that's why I ask myself where the problem is showing the dialog. I use the following @ReportCrashed Annotation: – Harry500 Mar 04 '17 at 17:28
  • `@ReportsCrashes( formUri = "http://yourserver.com/yourscript", mode = ReportingInteractionMode.DIALOG, resDialogText = R.string.app_name )` – Harry500 Mar 04 '17 at 17:35

2 Answers2

0

To post a full message (comment is too short), here the full class and configuration:

I tried to use ACRA with the built-in dialog-functionality, but I could not get it work. But the built-in funtionality to show a "Toast" works! So that's why I ask myself where the problem is showing the dialog. I use the following @ReportCrashed Annotation for testing:

@ReportsCrashes(
    formUri = "http://yourserver.com/yourscript",
    mode = ReportingInteractionMode.NOTIFICATION,
    resDialogText = R.string.app_name,
    resNotifTickerText = R.string.app_name,
    resNotifTitle = R.string.app_name,
    resNotifText = R.string.app_name
)

Inside my own Application-Class I use the following initialisation:

public class ThisApplication extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

        final ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(this);
        configurationBuilder.setBuildConfigClass(BuildConfig.class);

        final ACRAConfiguration config;
        try {
            config = configurationBuilder.build();
            ACRA.init(this, config);
        } catch (ACRAConfigurationException e) {
            e.printStackTrace();
        }
    }
}

My App uses two different Build flavors and the two build types "Debug" and "Release".

When I throw an unhandled exception the app closes and a dialog is only sometimes displayed for a very short moment (less than half a second) before the whole app is closed without any dialog.

Any ideas?...

EDIT: The above Annotation was the try with a Notification, that also does not work. The notification is also displayed only for a very short moment and then disappears immediately. The dialog Annotation was:

@ReportsCrashes(
    formUri = "http://yourserver.com/yourscript",
    mode = ReportingInteractionMode.DIALOG,
    resDialogText = R.string.app_name
)

This has the effect described above.

Harry500
  • 11
  • 1
0

The Problem was - at least in the case of the ACRA-Dialog - that it is not working as the app is debugged with the built-in functionality from android studio. So you have to start the app on the android test system (on the debugging device) without support from android studio IDE. When you do that and an exception is thrown, the ACRA-Dialog appears as it should.

Harry500
  • 11
  • 1