简体   繁体   中英

Display Dialog after uncaught exception

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);
    }
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM