我有一个活生生的例子here我以前做过。
名称是否需要采用特定格式?例如。我可以命名我的频道吗
只是普通的电池?或者它是否与格式有关
my_company.flutter.io/电池?还是别的什么?
由于频道名称必须是唯一的,因此最好在其前面加上您的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";
但是“调用”它就像是在通道内放置一些东西。
因为通道是一个通道(想象一个管道),而您放入其中以便从一侧(Dart)传递到另一侧(Android/iOS)的内容完全是另一个故事 ^ _^。
而且你放在里面的东西,它应该是从另一边捕捉到的。
platform.invokeMethod('viewPdf', args);
因此,您通过渠道发送的只是消息。
你把带有字母的瓶子放在管子里^_^
现在您必须在本机代码端捕获此消息,例如 '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 原生代码端应该有这个:
@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');
因为该示例的代码期望消息 'getBatteryLevel' 通过通道传递。
强化概念
因此,为了强化所有这些概念,我将进一步告诉您,您可以决定使用频道服务于单个操作或服务于多个操作。选择权在你。
所以你可以拥有
飞镖端:
static const singleChannel =
const MethodChannel('it.versionestabile.flutterapp000001/single');
static const multiChannel =
const MethodChannel('it.versionestabile.flutterapp000001/multi');
原生端(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();
}
}
});
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),
],
)),
),