I am developing a crash reporter plugin for phonegap android apps. For the testing purpose, I have to make my application crash & \"Unfortunately, application has stopped\"
Unfortunately, JS exceptions are handled by the WebView and even native plugins run in their own separate thread, and will not crash the main Application thread if you raise an exception there (this may not be true for hybrid applications). So you need to hook into the main activity and cause it to throw an Exception on the main thread.
Subclass the CordovaActivity
class and override the plugin messaging method:
@Override
public Object onMessage(String id, Object data) {
if ("crashApp".equals(id)) {
throw new Exception();
}
super(id, data);
}
Write an Android native plugin for Phonegap as per this answer. What it does doesn't matter, but when you message it from the Javascript you should use the id 'crashApp' which should be picked up by your overridden method in the CordovaActivity
subclass, and raise the Exception on the main thread.
. Then you can message your plugin using this id whenever you have a Javascript exception by overriding the window.onerror
function:
window.onerror = function myFunction(errorMsg, url, lineNumber) {
/* call Android native function to throw Exception */ ;
return false; }
This will throw all Javascript errors down into your Java code where they can raise the ANR dialog (and you can pass debugging information with it too).