Skip to content

Commit

Permalink
跑起来
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangruiyu committed Dec 12, 2024
1 parent fde4ebf commit eaa2cc6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 60 deletions.
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ class _IndexPageState extends State<IndexPage> {
));
print("sdk初始化 $_init");
_version = await FlutterUnionad.getSDKVersion();
print("sdk初始化 1");
_themeStatus = await FlutterUnionad.getThemeStatus();
print("sdk初始化 12");
setState(() {});
}

Expand Down
12 changes: 12 additions & 0 deletions example/ohos/build-profile.json5
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@
]
}
]
},
{
"name": "flutter_unionad",
"srcPath": "../../ohos",
"targets": [
{
"name": "default",
"applyToProducts": [
"default"
]
}
]
}
]
}
2 changes: 1 addition & 1 deletion example/ohos/oh-package.json5
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"overrides": {
"@ohos/flutter_ohos": "file:./har/flutter.har",
"flutter_unionad": "file:./har/flutter_unionad.har",
"flutter_unionad": "../../ohos",
"@ohos/flutter_module": "file:./entry"
}
}
127 changes: 68 additions & 59 deletions ohos/src/main/ets/components/plugin/FlutterUnionadPlugin.ets
Original file line number Diff line number Diff line change
@@ -1,77 +1,86 @@
import {
FlutterPlugin,
FlutterPluginBinding,
MethodCall,
MethodCallHandler,
MethodChannel,
MethodResult,
FlutterPlugin,
FlutterPluginBinding,
MethodCall,
MethodCallHandler,
MethodChannel,
MethodResult,
} from '@ohos/flutter_ohos';
import {
CSJAdSdk,
SDKConfig,
SDKConfigBuilder
AdLoadType,
AdSlotBuilder, CSJAdSdk,
CSJRewardAd,
RewardAdLoadListener, SDKConfig,
SDKConfigBuilder
} from '@csj/openadsdk';

/** FlutterUnionadPlugin **/
export default class FlutterUnionadPlugin implements FlutterPlugin, MethodCallHandler {
private channel: MethodChannel | null = null;
private applicationContext: Context | null = null;

constructor() {
}

getUniqueClassName(): string {
return "FlutterUnionadPlugin"
}
private channel: MethodChannel | null = null;
private applicationContext: Context | null = null;
private adCreator = CSJAdSdk.getAdCreator();
constructor() {
}

onAttachedToEngine(binding: FlutterPluginBinding): void {
this.channel = new MethodChannel(binding.getBinaryMessenger(), "flutter_unionad");
this.channel.setMethodCallHandler(this)
this.applicationContext = binding.getApplicationContext();
}
getUniqueClassName(): string {
return "FlutterUnionadPlugin"
}

onDetachedFromEngine(binding: FlutterPluginBinding): void {
if (this.channel != null) {
this.channel.setMethodCallHandler(null)
onAttachedToEngine(binding: FlutterPluginBinding): void {
this.channel = new MethodChannel(binding.getBinaryMessenger(), "flutter_unionad");
this.channel.setMethodCallHandler(this)
this.applicationContext = binding.getApplicationContext();
}
}

onMethodCall(call: MethodCall, result: MethodResult): void {
if (call.method == "register") {
onDetachedFromEngine(binding: FlutterPluginBinding): void {
if (this.channel != null) {
this.channel.setMethodCallHandler(null)
}
}

// 创建SDKConfig对象
let adConfigBuilder = new SDKConfigBuilder()
let config: SDKConfig = adConfigBuilder
.appId("5638354")
.appName("穿山甲Demo")
.allowShowNotify(true)
.debug(true)
.build()
onMethodCall(call: MethodCall, result: MethodResult): void {
if (call.method == "register") {

// 初始化SDK
CSJAdSdk.init(this.applicationContext, config) //context类型必须为UIAbility,否则可能存在展示异常场景
// 创建SDKConfig对象
let adConfigBuilder = new SDKConfigBuilder()
let config: SDKConfig = adConfigBuilder
.appId("5638354")
.appName("穿山甲Demo")
.allowShowNotify(true)
.debug(true)
.build()

// 启动SDK
CSJAdSdk.start();
result.success(true)
}else if (call.method == "getSDKVersion") {
// 创建SDKConfig对象
let adConfigBuilder = new SDKConfigBuilder()
let config: SDKConfig = adConfigBuilder
.appId("5638354")
.appName("穿山甲Demo")
.allowShowNotify(true)
.debug(true)
.build()
// 初始化SDK
CSJAdSdk.init(this.applicationContext, config) //context类型必须为UIAbility,否则可能存在展示异常场景

// 初始化SDK
CSJAdSdk.init(this.applicationContext, config) //context类型必须为UIAbility,否则可能存在展示异常场景
// 启动SDK
CSJAdSdk.start();
result.success(true)
} else if (call.method == "getSDKVersion") {
result.success(CSJAdSdk.getSDKVersion())
} else if (call.method == "getThemeStatus") {
result.success(0)
} else if (call.method == "loadRewardVideoAd") {

// 启动SDK
CSJAdSdk.start();
result.success(true)
} else {
result.notImplemented()
let adSlot = new AdSlotBuilder()
.setCodeId("codeId")
.setAdLoadType(AdLoadType.LOAD)
// .setMediaExtra(mediaExtra)
.build()
let mLoadListener: RewardAdLoadListener = {
onAdLoaded: (rewardAd: CSJRewardAd) => {
// 广告基础信息加载完成
},
onAdCached: (rewardAd: CSJRewardAd) => {
// 广告基础信息与素材缓存完成
},
onError: (code: number, message: string) => {
}
}
this.adCreator.loadRewardAd(adSlot, mLoadListener)
result.success(true)
} else {
result.notImplemented()
}
}
}
}

0 comments on commit eaa2cc6

Please sign in to comment.