作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我收到以下警告:
THREAD WARNING: exec()
call to MyPlugin.setAndroidPreferences blocked the main thread for 49ms.
Plugin should use CordovaInterface.getThreadPool().
但是在我的代码中,我使用了 cordova.getThreadPool()
:
private boolean setAndroidPreferences(
final JSONArray args,
final CallbackContext callbackContext)
{
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
try {
/* ... */
if ( /* ... */)
{
final SharedPreferences settings = cordova.getActivity().getSharedPreferences(preferenceLib, Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = settings.edit();
editor.putString(preferenceName, preferenceValue);
editor.commit();
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
} else { callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, "getSetSharePreferences" + ": Error: " + PluginResult.Status.JSON_EXCEPTION);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
}
}
});
return true;
}
我做错了什么?
谢谢,
最佳答案
public boolean execute(String action, final JSONArray inputs, final CallbackContext callbackContext) throws JSONException {
if (action.equals("setAndroidPreferences")) {
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
callbackContext.sendPluginResult(setAndroidPreferences(inputs));
}
});
}
}
private PluginResult setAndroidPreferences(JSONArray args) {
try {
if ( /* ... */) {
SharedPreferences settings = cordova.getActivity().getSharedPreferences(preferenceLib, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(preferenceName, preferenceValue);
editor.commit();
return new PluginResult(PluginResult.Status.OK);
} else {
return new PluginResult(PluginResult.Status.ERROR);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, "getSetSharePreferences" + ": Error: " + PluginResult.Status.JSON_EXCEPTION);
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
}
关于android - 即使在使用 CordovaInterface.getThreadPool() 之后,Cordova Exec 也会阻塞主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24588103/
有人可以向我澄清主线 DHT 规范中的声明吗? Upon inserting the first node into its routing table and when starting up th
我正在尝试使用 USB 小工具驱动程序使嵌入式设备作为 MTP 设备工作。 我知道 Android 从大容量存储设备切换到 MTP 设备已经有一段时间了,并且找到了 source code for M
我是一名优秀的程序员,十分优秀!