我正在尝试在背景中运行任务(不要使用现有插件响应),但在第一次运行时我收到此错误“-32601 从应用程序收到:找不到方法”
一步步
我有一种方法可以通过MethodChannel
向本机发送句柄
class Backgroundtask {
static const MethodChannel _channel = const MethodChannel('backgroundtask');
static Future<bool> periodic({@required Function callback}) async {
assert(callback != null, 'task cannot be null');
CallbackHandle handle = PluginUtilities.getCallbackHandle(callback);
if (handle == null) {
return false;
}
final bool result = await _channel.invokeMethod(
'periodic',
<dynamic>[
handle.toRawHandle(),
],
);
return result ?? false;
}
}
另一方面,我启动了一个 WorkerManager,handle
将Data
.
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
[...]
val data = Data.Builder()
data.putLong("handle", handle)
val constrains = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val pWorkRequest = PeriodicWorkRequest
.Builder(CustomWorker::class.java, 15, TimeUnit.MINUTES)
.setInputData(data.build())
.setConstraints(constrains)
.build()
WorkManager.getInstance(context).enqueue(pWorkRequest)
[...]
}
我的 androidx.work.Worker.doWork()
[...]
Handler(Looper.getMainLooper()).post {
Log.d("periodic", "background service start")
val handle = inputData.getLong("handle", 0)
val flutterEngine = FlutterEngine(context)
val flutterCallback = FlutterCallbackInformation.lookupCallbackInformation(handle)
if (flutterCallback.callbackName == null) {
Log.e("periodic", "callback not found")
return
}
val assets = context.assets
val bundle = FlutterMain.findAppBundlePath()
val executor = flutterEngine.dartExecutor
val dartCallback = DartExecutor.DartCallback(assets, bundle, flutterCallback)
executor.executeDartCallback(dartCallback)
Log.d("periodic", "background service end")
}
[...]
我的错误发生在回调执行后,不管它做什么。我想这一定是我没有看到的愚蠢的东西,但我的头坏了。
非常感谢您提前。