gpt4 book ai didi

plugins - 如何在后台运行cordova插件?

转载 作者:行者123 更新时间:2023-12-03 12:12:10 27 4
gpt4 key购买 nike

我正在基于phonegap(cordova)制作应用程序。我已经测试过一段时间了,最​​近我在xcode中看到一条消息,说“插件应使用后台线程”。那么有可能使cordova插件在应用程序的后台运行吗?如果是这样,请告诉如何。谢谢!

最佳答案

后台线程与在应用程序处于后台时执行代码不同,后台线程用于在执行长任务时不阻止UI。

iOS上的背景线程示例

- (void)myPluginMethod:(CDVInvokedUrlCommand*)command
{
// Check command.arguments here.
[self.commandDelegate runInBackground:^{
NSString* payload = nil;
// Some blocking logic...
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:payload];
// The sendPluginResult method is thread-safe.
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}

Android上的背景线程示例
@Override
public 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(); // Thread-safe.
}
});
return true;
}
return false;
}

关于plugins - 如何在后台运行cordova插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22382260/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com