gpt4 book ai didi

android - Flutter 平台 channel 的 channel 名称应该是什么格式?

转载 作者:IT王子 更新时间:2023-10-29 07:12:52 27 4
gpt4 key购买 nike

我正在添加创建我自己的平台 channel (具体来说,Android 的方法 channel )并关注 https://flutter.io/docs/development/platform-integration/platform-channels#step-1-create-a-new-app-project

我明白了:

All channel names used in a single app must be unique; we recommend prefixing the channel name with a unique ‘domain prefix’, e.g. samples.flutter.io/battery.

名称是否需要特定格式?例如。我可以将我的 channel 命名为简单的 ol' battery 吗?或者它有一些围绕格式 my_company.flutter.io/battery 的东西吗?或者是其他东西?我尝试了各种字符串,但每次我都得到:

MissingPluginException(未在 channel 电池上找到方法 getBattery 的实现)

所以我想知道是不是我的 channel 名称格式不正确。我认为我的方法名称 getBattery 是正确的。但要澄清的是,我想做的是:

final result = await platform.invokeMethod('getBattery');

没有插件前缀,对吧?

final result = await platform.invokeMethod('battery.getBattery'); 

最佳答案

我有一个实例 here我以前做过几次。

Does the name need to be a specific format? E.g. Can I name my channel just plain ol' battery? Or does it have be something around the format my_company.flutter.io/battery? Or something else?

由于 channel 名称必须是唯一的,因此最好在其前面加上您的 bundle(iOS)/package(Android) id

例如:

static const platform =
const MethodChannel('it.versionestabile.flutterapp000001/pdfViewer');

无论如何,您可以随意调用它,但它应该是独一无二的,并且在您的 Dart 和 Android/iOS 方面是平等的。

private static final String CHANNEL = "it.versionestabile.flutterapp000001/pdfViewer";

但是“调用”它就像放入 channel 中一样。

因为 channel 是一个 channel (想象一个管道),你把它放在里面从一边(Dart)传递到另一个(Android/iOS)完全是另一个故事^ _^。

你放在里面的东西,应该从另一边被捕获。

platform.invokeMethod('viewPdf', args);

因此,您通过 channel 发送的只是消息

你在 pipe 里放了一个带有字母的瓶子^_^

现在您必须在您的 native 代码端捕获此消息,例如 'viewPdf'

new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("viewPdf")) {
if (call.hasArgument("url")) {
String url = call.argument("url");
File file = new File(url);
//*
Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider",
file);
//*/
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(photoURI,"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
target.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(target);
result.success(null);
}
} else {
result.notImplemented();
}
}
});
}

否则你会陷入困境

} else {
result.notImplemented();
}

如果您遵循示例,您应该在您的 Android native 代码端拥有此示例:

@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("getBatteryLevel")) {
int batteryLevel = getBatteryLevel();

if (batteryLevel != -1) {
result.success(batteryLevel);
} else {
result.error("UNAVAILABLE", "Battery level not available.", null);
}
} else {
result.notImplemented();
}
}

所以你应该调用:

platform.invokeMethod('getBatteryLevel');

因为该示例的代码需要通过 channel 传递消息 'getBatteryLevel'

强化概念

因此,为了强化所有这些概念,我将进一步告诉您,您可以决定使用 channel 来服务于单个操作或服务于多个操作。选择权在您。

所以你可以

飞镖方面:

  static const singleChannel =
const MethodChannel('it.versionestabile.flutterapp000001/single');
static const multiChannel =
const MethodChannel('it.versionestabile.flutterapp000001/multi');

native 端 (Android):

  private static final String SINGLE_CHANNEL = "it.versionestabile.flutterapp000001/single";
private static final String MULTI_CHANNEL = "it.versionestabile.flutterapp000001/multi";

还有一些玩具搬运工:

new MethodChannel(getFlutterView(), MULTI_CHANNEL).setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("op1")) {
new AlertDialog.Builder(MainActivity.this)
.setTitle(call.method)
.setMessage("I'm the " + call.method + " of the by design multi operation channel!")
.create()
.show();
} else if (call.method.equals("op2")) {
new AlertDialog.Builder(MainActivity.this)
.setTitle(call.method)
.setMessage("I'm the " + call.method + " of the by design multi operation channel!")
.create()
.show();
} else {
result.notImplemented();
}
}
});

new MethodChannel(getFlutterView(), SINGLE_CHANNEL).setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("hello")) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("hello!")
.setMessage("I'm the by design single operation channel!")
.create()
.show();
} else {
result.notImplemented();
}
}
});

enter image description here

  void _invokeMultiChannelOp2() {
multiChannel.invokeMethod('op2');
}

void _invokeMultiChannelOp1() {
multiChannel.invokeMethod('op1');
}

void _invokeSingleChannel() {
singleChannel.invokeMethod('hello');
}

floatingActionButton: SafeArea(
child: Padding(
padding: EdgeInsets.only(left: 8.0),
child: Row(
children: <Widget>[
new IconButton(
icon: new Icon(Icons.library_music),
onPressed: _invokeMultiChannelOp1),
new IconButton(
icon: new Icon(Icons.note),
onPressed: _invokeMultiChannelOp2),
new IconButton(
icon: new Icon(Icons.plus_one),
onPressed: _invokeSingleChannel),
],
)),
),

关于android - Flutter 平台 channel 的 channel 名称应该是什么格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53888502/

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