Skip to content

Commit

Permalink
完善
Browse files Browse the repository at this point in the history
  • Loading branch information
gstory0404 committed Aug 7, 2021
1 parent 4ba9efb commit fec4ce7
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 22 deletions.
Empty file added android/proguard-rules.pro
Empty file.
Original file line number Diff line number Diff line change
@@ -1,21 +1,47 @@
package com.gstory.flutter_tencentad

import android.app.Activity
import android.content.Context
import androidx.annotation.NonNull
import com.gstory.flutter_tencentad.interstitialad.InterstitialAd
import com.gstory.flutter_tencentad.rewardvideoad.RewardVideoAd
import com.qq.e.comm.managers.GDTADManager
import com.qq.e.comm.managers.status.SDKStatus
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** FlutterTencentadPlugin */
class FlutterTencentadPlugin : FlutterPlugin, MethodCallHandler {
class FlutterTencentadPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {

private lateinit var channel: MethodChannel
private var applicationContext: Context? = null
private var mActivity: Activity? = null

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
mActivity = binding.activity
// Log.e("FlutterUnionadPlugin->","onAttachedToActivity")
// FlutterUnionadViewPlugin.registerWith(mFlutterPluginBinding!!,mActivity!!)
}

override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
mActivity = binding.activity
// Log.e("FlutterUnionadPlugin->","onReattachedToActivityForConfigChanges")
}

override fun onDetachedFromActivityForConfigChanges() {
mActivity = null
// Log.e("FlutterUnionadPlugin->","onDetachedFromActivityForConfigChanges")
}

override fun onDetachedFromActivity() {
mActivity = null
// Log.e("FlutterUnionadPlugin->","onDetachedFromActivity")
}

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_tencentad")
Expand All @@ -35,11 +61,22 @@ class FlutterTencentadPlugin : FlutterPlugin, MethodCallHandler {
//获取sdk版本
} else if (call.method == "getSDKVersion") {
result.success("${SDKStatus.getSDKVersion()}.${GDTADManager.getInstance().pm.pluginVersion}")
//预加载激励广告
} else if (call.method == "loadRewardVideoAd") {
RewardVideoAd.init(applicationContext!!,call.arguments as Map<*, *>)
result.success(true)
//展示激励广告
} else if (call.method == "showRewardVideoAd") {
RewardVideoAd.showAd()
result.success(true)
//预加载插屏广告
} else if (call.method == "loadUnifiedInterstitialAD") {
InterstitialAd.init(mActivity!!,call.arguments as Map<*, *>)
result.success(true)
//展示插屏广告
} else if (call.method == "showUnifiedInterstitialAD") {
InterstitialAd.showAd()
result.success(true)
} else {
result.notImplemented()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.gstory.flutter_tencentad.interstitialad

import android.app.Activity
import android.content.Context
import com.gstory.flutter_tencentad.LogUtil
import com.gstory.flutter_tencentad.rewardvideoad.RewardVideoAd
import com.qq.e.ads.interstitial2.UnifiedInterstitialAD
import com.qq.e.ads.interstitial2.UnifiedInterstitialADListener
import com.qq.e.ads.rewardvideo.RewardVideoAD
import com.qq.e.comm.util.AdError

object InterstitialAd {
private val TAG = "InterstitialAd"

private lateinit var context: Activity
private var unifiedInterstitialAD: UnifiedInterstitialAD? = null

private var codeId: String? = null
private var isFullScreen: Boolean? = false

fun init(context: Activity, params: Map<*, *>) {
this.context = context
this.codeId = params["codeId"] as String
this.isFullScreen = params["isFullScreen"] as Boolean
loadInterstitialAD()
}

private fun loadInterstitialAD() {
unifiedInterstitialAD = UnifiedInterstitialAD(
context,
codeId,
interstitialADListener
)
if(isFullScreen!!){
unifiedInterstitialAD?.loadFullScreenAD()
}else{
unifiedInterstitialAD?.loadAD()
}
}

fun showAd(){
if(unifiedInterstitialAD == null){
return
}
if(isFullScreen!!){
unifiedInterstitialAD?.showFullScreenAD(context)
}else{
unifiedInterstitialAD?.showAsPopupWindow()
}
}

private var interstitialADListener = object : UnifiedInterstitialADListener{
//插屏全屏视频广告加载完毕,此回调后才可以调用 show 方法
override fun onADReceive() {
LogUtil.e("$TAG 插屏全屏视频广告加载完毕")
}

//插屏全屏视频视频广告,视频素材下载完成
override fun onVideoCached() {
LogUtil.e("$TAG 插屏全屏视频视频广告,视频素材下载完成")
}

//广告加载失败,error 对象包含了错误码和错误信息
override fun onNoAD(p0: AdError?) {
LogUtil.e("$TAG 插屏全屏视频视频广告,加载失败 ${p0?.errorCode} ${p0?.errorMsg}")
}

//插屏全屏视频广告展开时回调
override fun onADOpened() {
LogUtil.e("$TAG 插屏全屏视频广告展开时回调")
}

//插屏全屏视频广告曝光时回调
override fun onADExposure() {
LogUtil.e("$TAG 插屏全屏视频广告曝光时回调")
}

//插屏全屏视频广告点击时回调
override fun onADClicked() {
LogUtil.e("$TAG 插屏全屏视频广告点击时回调")
}

override fun onADLeftApplication() {
LogUtil.e("$TAG 插屏全屏视频视频广告,渲染成功")
}

//插屏全屏视频广告关闭时回调
override fun onADClosed() {
LogUtil.e("$TAG 插屏全屏视频广告关闭时回调")
unifiedInterstitialAD?.close()
unifiedInterstitialAD?.destroy()
unifiedInterstitialAD = null
}

// 插屏全屏视频视频广告,渲染成功
override fun onRenderSuccess() {
LogUtil.e("$TAG 插屏全屏视频视频广告,渲染成功")
}

//插屏全屏视频视频广告,渲染失败
override fun onRenderFail() {
LogUtil.e("$TAG 插屏全屏视频视频广告,渲染失败")
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.gstory.flutter_tencentad.rewardvideoad

import android.content.Context
import com.gstory.flutter_tencentad.LogUtil
import com.gstory.flutter_tencentad.interstitialad.InterstitialAd
import com.qq.e.ads.rewardvideo.RewardVideoAD
import com.qq.e.ads.rewardvideo.RewardVideoADListener
import com.qq.e.comm.util.AdError
Expand All @@ -11,7 +12,7 @@ object RewardVideoAd{
private val TAG = "RewardVideoAd"

private lateinit var context: Context
private lateinit var rewardVideoAD: RewardVideoAD
private var rewardVideoAD: RewardVideoAD? = null

private var codeId: String? = null

Expand All @@ -23,50 +24,54 @@ object RewardVideoAd{

private fun loadRewardVideoAd() {
rewardVideoAD = RewardVideoAD(context, codeId, rewardVideoADListener) // 有声播放
rewardVideoAD.loadAD()
rewardVideoAD?.loadAD()
}

public fun showAd(){
rewardVideoAD.showAD()
fun showAd(){
if(rewardVideoAD == null){
return
}
rewardVideoAD?.showAD()
}

private var rewardVideoADListener = object : RewardVideoADListener {
override fun onADLoad() {
LogUtil.e("激励广告加载成功")
LogUtil.e("$TAG 激励广告加载成功")
}

override fun onVideoCached() {
LogUtil.e("激励广告视频素材缓存成功")
LogUtil.e("$TAG 激励广告视频素材缓存成功")
showAd()
}

override fun onADShow() {
LogUtil.e("激励视频广告页面展示")
LogUtil.e("$TAG 激励视频广告页面展示")

}

override fun onADExpose() {
LogUtil.e("激励视频广告曝光")
LogUtil.e("$TAG 激励视频广告曝光")
}

override fun onReward(p0: MutableMap<String, Any>?) {
LogUtil.e("激励视频广告激励发放 $p0")
LogUtil.e("$TAG 激励视频广告激励发放 $p0")
}

override fun onADClick() {
LogUtil.e("激励视频广告被点击")
LogUtil.e("$TAG 激励视频广告被点击")
}

override fun onVideoComplete() {
LogUtil.e("激励视频广告视频素材播放完毕")
LogUtil.e("$TAG 激励视频广告视频素材播放完毕")
}

override fun onADClose() {
LogUtil.e("激励视频广告被关闭")
LogUtil.e("$TAG 激励视频广告被关闭")
rewardVideoAD = null
}

override fun onError(p0: AdError?) {
LogUtil.e("广告流程出错 ${p0?.errorCode} ${p0?.errorMsg}")
LogUtil.e("$TAG 广告流程出错 ${p0?.errorCode} ${p0?.errorMsg}")
}

}
Expand Down
17 changes: 16 additions & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,26 @@ android {
versionName flutterVersionName
}

signingConfigs {
config {
storeFile file("..\\key\\tencentad.jks")
storePassword "tencentad"
keyAlias "tencentad"
keyPassword "tencentad"
}
}


buildTypes {
debug {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.config
}
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
signingConfig signingConfigs.config
}
}
}
Expand Down
Binary file added example/android/key/tencentad.jks
Binary file not shown.
26 changes: 24 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class _MyAppState extends State<MyApp> {

///初始化
Future<void> _register() async {
_isRegister = await FlutterTencentad.register(appId: "1200020945",debug: true);
_isRegister =
await FlutterTencentad.register(appId: "1200021532", debug: true);
_sdkVersion = await FlutterTencentad.getSDKVersion();
setState(() {});
}
Expand All @@ -47,7 +48,28 @@ class _MyAppState extends State<MyApp> {
textColor: Colors.white,
child: new Text('激励广告'),
onPressed: () async {
await FlutterTencentad.loadRewardVideoAd(codeId: "1022412724880500");
await FlutterTencentad.loadRewardVideoAd(
codeId: "8002118728021979");
},
),
//插屏广告(半屏)
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text('插屏广告(半屏)'),
onPressed: () async {
await FlutterTencentad.loadUnifiedInterstitialAD(
codeId: "5062710728123927", isFullScreen: false);
},
),
//插屏广告(全屏)
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text('插屏广告(全屏)'),
onPressed: () async {
await FlutterTencentad.loadUnifiedInterstitialAD(
codeId: "6012914718424906", isFullScreen: true);
},
),
],
Expand Down
21 changes: 20 additions & 1 deletion lib/flutter_tencentad.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class FlutterTencentad {
///
/// [codeId] 广告ID
///
static Future<bool> loadRewardVideoAd({
static Future<bool> loadRewardVideoAd({
required String codeId,
}) async {
return await _channel.invokeMethod("loadRewardVideoAd", {"codeId": codeId});
Expand All @@ -37,4 +37,23 @@ class FlutterTencentad {
static Future<bool> showRewardVideoAd() async {
return await _channel.invokeMethod("showRewardVideoAd", {});
}

/// # 预加载插屏广告
///
/// [codeId] 广告ID
///
/// [isFullScreen] 是否全屏
///
static Future<bool> loadUnifiedInterstitialAD(
{required String codeId, required bool isFullScreen}) async {
return await _channel.invokeMethod("loadUnifiedInterstitialAD", {
"codeId": codeId,
"isFullScreen": isFullScreen,
});
}

/// # 显示新模板渲染插屏
static Future<bool> showUnifiedInterstitialAD() async {
return await _channel.invokeMethod("showUnifiedInterstitialAD", {});
}
}
4 changes: 0 additions & 4 deletions test/flutter_tencentad_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,4 @@ void main() {
tearDown(() {
channel.setMockMethodCallHandler(null);
});

test('getPlatformVersion', () async {
expect(await FlutterTencentad.platformVersion, '42');
});
}

0 comments on commit fec4ce7

Please sign in to comment.