[英]Flutter native android callback
I am working on a flutter app which requires the help of some native Android codes. 我正在开发一个颤动的应用程序,需要一些原生Android代码的帮助。 To be more specific, I want to integrate an ad network named "IronSource" to serve ads in my app. 更具体地说,我想整合一个名为“IronSource”的广告网络,以便在我的应用中投放广告。 But currently they do not provide a library for flutter so I wrote the required codes in java language and did a platform call to show an ad. 但是目前他们没有提供一个用于颤动的库,所以我用java语言编写了所需的代码并进行了平台调用以显示广告。 The ad is showing successfully but I have a problem on how to listen to their callback methods that they have provided. 该广告已成功展示,但我对如何收听他们提供的回调方法存在疑问。
I will post what I have tried so far. 我将发布到目前为止我尝试过的内容。
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("loadInterstitialAd")) {
IronSource.loadInterstitial();
} else {
result.notImplemented();
}
}
});
The above method will listen to the channel and will load the ad. 上述方法将收听频道并加载广告。 However, these are some of the callback methods that are available in native library: 但是,这些是本机库中可用的一些回调方法:
public void onInterstitialAdLoadFailed
public void onInterstitialAdOpened()
public void onInterstitialAdClosed()
public void onInterstitialAdShowSucceeded()
and so on... 等等...
Now I do not understand how to access these methods in flutter and write my own code. 现在我不明白如何在flutter中访问这些方法并编写我自己的代码。
Any idea/suggestion would be appreciated. 任何想法/建议将不胜感激。
Thanks 谢谢
You can define some Integer
values for onInterstitialAdLoadFailed
、 onInterstitialAdOpened
、 onInterstitialAdClosed
、 onInterstitialAdShowSucceeded
and call Result.onSuccess(Object)
in those callback methods. 您可以为onInterstitialAdLoadFailed
, onInterstitialAdOpened
, onInterstitialAdClosed
, onInterstitialAdShowSucceeded
定义一些Integer
值,并在这些回调方法中调用Result.onSuccess(Object)
。 Here is the sample code: 以下是示例代码:
Native part: 原生部分:
Result mResult;
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("loadInterstitialAd")) {
IronSource.loadInterstitial();
mResult = result;//save the result
} else {
result.notImplemented();
}
}
});
public void onInterstitialAdLoadFailed(){
mResult.success(0);
}
public void onInterstitialAdOpened(){
mResult.success(1);
}
public void onInterstitialAdClosed(){
mResult.success(2);
}
public void onInterstitialAdShowSucceeded(){
mResult.success(3);
}
Flutter part: 颤动部分:
int resultCode = await loadAds();
if(resultCode == 0){ ... }
else if(resultCode == 1){ ... }
else if(resultCode == 2){ ... }
else if(resultCode == 3){ ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.