Fri*_*ick 6 android dart flutter
我在 Android 中有一个后台服务,我使用 Flutter Dart 代码调用它
var methodChannel = MethodChannel("org.daytonsquareroots.near.backgroundlocation");
methodChannel.invokeMethod("startBackgroundService", {
"encKey" : encryptionKey
});
Run Code Online (Sandbox Code Playgroud)
我使用一个名为simple rsa的 flutter 库来加密位置。但是,Android 似乎没有等价物。所以,我的问题是:
我可以像使用 Flutter MethodChannel 一样从我的 Android 代码中调用 Dart 代码来调用 Android 代码吗?
我已经检查了这个github 问题,但是我找不到实际的答案。
小智 9
我不知道它有多有效,但它确实有效:D
private static final String CHANNEL = "widget.filc.hu/timetable";
// You have to run this on the main thread, i think
public static MethodChannel GetMethodChannel(Context context) {
FlutterMain.startInitialization(context);
FlutterMain.ensureInitializationComplete(context, new String[0]);
FlutterEngine engine = new FlutterEngine(context.getApplicationContext());
DartExecutor.DartEntrypoint entrypoint = new DartExecutor.DartEntrypoint("lib/main.dart", "widget");
engine.getDartExecutor().executeDartEntrypoint(entrypoint);
return new MethodChannel(engine.getDartExecutor().getBinaryMessenger(), CHANNEL);
}
Run Code Online (Sandbox Code Playgroud)
dart 端有一个像这样的 init 函数:
void widget() async {
// get preferences everything what you want....
await app.settings.update();
// Create methodChannel
const MethodChannel channel = MethodChannel(CHANNEL);
channel.setMethodCallHandler(
(call) async {
final args = call.arguments;
print('on Dart ${call.method}!');
switch (call.method) {
case 'getTimetable':
return await _getTimetable();
case 'getRandom':
return Random().nextInt(100);
case 'initialize':
return "HELLO";
default:
throw UnimplementedError("Unknow: " + call.method);
}
},
);
}
Run Code Online (Sandbox Code Playgroud)
这些链接对我有帮助:
| 归档时间: |
|
| 查看次数: |
951 次 |
| 最近记录: |