I’m trying to set FLAG_SHOW_WHEN_LOCKED in my phonegap app, but only when a certain page is shown. To do so, I have a Java plugin extending from CordovaPlugin with the following code in the execute method:
if (action.equals("showWhenLocked")) {
boolean showWhenLocked = args.getBoolean(0);
if (showWhenLocked) {
this.cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
} else {
this.cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
callbackContext.success();
return true;
}
It gets called with cordova.exec(null, null, 'MyPluginClass', 'showWhenLocked', [myVar]), but on execution I get the error
Uncaught Error: Error calling method on NPObject. at file:///android_asset/www/cordova-2.2.0.js:984
Any ideas what’s causing this/what I’m doing wrong and how to fix it? If I set the flag upon creating the activity it works just fine.
I have found already that this kind of error can be caused by calls that require threads that are not available. Your use of
getWindow()tell me that this is even more likely. You are most likely accessing information locked by another thread, like the UI thread. Check out the cordova documentation in the section about threading in the UI thread.