blocked the main thread for 71ms. Plugin should use CordovaInterface.getThreadPool()

来源:互联网 发布:ubuntu使用教程pdf 编辑:程序博客网 时间:2024/05/16 13:02

在自己的cordova项目中经常出现一个警告:THREAD WARNING: exec() call to xxxxx.xxxxx blocked the main thread for 71ms. Plugin should use CordovaInterface.getThreadPool().

后来通过看一些人写的cordova教程才知道,如果你不需要在主界面线程运行,但使用WebCore线程,使用下面的方式就不会有这个警告:

    @Overridepublic boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {        if ("beep".equals(action)) {            final long duration = args.getLong(0);            cordova.getThreadPool().execute(new Runnable() {                public void run() {                    ...                    callbackContext.success();                 }            });            return true;        }        return false;    }

另外cordova插件的JavaScript不在webview界面的主线程中运行,而是在WebCore线程中调用execute方法。如果需要与用户交互(也就是android中的改变ui必须在主线程中)。所以你的cordova插件的java代码如果涉及到了改变ui需要这样写可以保证改变ui的操作一定在主线程中需要这么写:

    @Overridepublic boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {        if ("beep".equals(action)) {            final long duration = args.getLong(0);            cordova.getActivity().runOnUiThread(new Runnable() {                public void run() {                    ...                    callbackContext.success();                 }            });            return true;        }        return false;    }


0 0
原创粉丝点击