Flutter广告集成插件cs_tapjoy_ad的使用
cs_tapjoy_ad #
A new Flutter plugin project.
使用步骤 #
以下内容将详细介绍如何在 Flutter 项目中集成并使用 cs_tapjoy_ad 插件。
第一步:添加依赖
在项目的 pubspec.yaml 文件中添加 cs_tapjoy_ad 依赖:
dependencies:
cs_tapjoy_ad: ^1.0.0
然后运行以下命令以更新依赖项:
flutter pub get
第二步:初始化插件
在应用启动时调用 initialize 方法来初始化插件。以下是完整的示例代码:
示例代码
import 'package:cs_tapjoy_ad/cs_tapjoy_ad_listener.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:cs_tapjoy_ad/cs_tapjoy_ad.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with CsTapjoyAdListener {
String _platformVersion = 'Unknown';
final String _placementName = 'Tapjoy Offerwall Location';
final _csTapjoyAdPlugin = CsTapjoyAd();
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
// 初始化插件
Future<void> initPlatformState() async {
String platformVersion = "准备初始化!!!!";
try {
await _csTapjoyAdPlugin.setTapjoyAdListener(this); // 设置监听器
await _csTapjoyAdPlugin.initialize("xxxxxxxxxxx"); // 替换为实际的 Tapjoy App ID
await _csTapjoyAdPlugin.setDebug(true); // 启用调试模式
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
/// 初始化成功回调
[@override](/user/override)
void onInitSuccess() {
debugPrint("cs_tapjoy_ad onInitSuccess");
setState(() {
_platformVersion = "SDK 初始化成功";
});
}
/// 初始化失败回调
[@override](/user/override)
void onInitFailed() {
debugPrint("cs_tapjoy_ad onInitFailed");
}
/// 请求广告成功回调
[@override](/user/override)
void onRequestSuccess(String? placementName) {
debugPrint("cs_tapjoy_ad onRequestSuccess - pn:$placementName");
setState(() {
_platformVersion = "请求 placement 成功";
});
}
/// 请求广告失败回调
[@override](/user/override)
void onRequestFailure(String? placementName, int? errCode, String? errMsg) {
debugPrint("cs_tapjoy_ad onRequestFailure - pn:$placementName");
}
/// 广告内容准备就绪回调
[@override](/user/override)
void onContentReady(String? placementName) {
debugPrint("cs_tapjoy_ad onContentReady - pn:$placementName");
}
/// 广告展示回调
[@override](/user/override)
void onContentShow(String? placementName) {
debugPrint("cs_tapjoy_ad onContentShow - pn:$placementName");
}
/// 广告关闭回调
[@override](/user/override)
void onContentDismiss(String? placementName) {
debugPrint("cs_tapjoy_ad onContentDismiss - pn:$placementName");
}
/// 购买请求回调
[@override](/user/override)
void onPurchaseRequest(String? placementName, String? requestToken, String? productId) {
debugPrint("cs_tapjoy_ad onPurchaseRequest - pn:$placementName");
}
/// 奖励请求回调
[@override](/user/override)
void onRewardRequest(String? placementName, String? itemId, int quantity) {
debugPrint("cs_tapjoy_ad onRewardRequest - pn:$placementName");
}
/// 点击广告回调
[@override](/user/override)
void onClick(String? placementName) {
debugPrint("cs_tapjoy_ad onClick - pn:$placementName");
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('cs_tapjoy_ad 示例'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
_platformVersion,
style: const TextStyle(fontSize: 20, color: Colors.black),
),
const SizedBox(height: 100),
ElevatedButton(
onPressed: () {
_csTapjoyAdPlugin.requestPlacement(_placementName); // 请求广告
},
child: const Text(
"请求广告",
style: TextStyle(fontSize: 20, color: Colors.black),
),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
_csTapjoyAdPlugin.showPlacement(_placementName); // 展示广告
},
child: const Text(
"展示广告",
style: TextStyle(fontSize: 20, color: Colors.black),
),
),
],
),
),
),
);
}
}
更多关于Flutter广告集成插件cs_tapjoy_ad的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter广告集成插件cs_tapjoy_ad的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
cs_tapjoy_ad 是一个用于在 Flutter 应用中集成 Tapjoy 广告的插件。Tapjoy 是一个移动广告平台,提供激励视频广告、插页式广告等多种广告形式。以下是如何在 Flutter 项目中使用 cs_tapjoy_ad 插件的步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml 文件中添加 cs_tapjoy_ad 插件的依赖。
dependencies:
flutter:
sdk: flutter
cs_tapjoy_ad: ^1.0.0 # 请根据实际情况使用最新版本
然后运行 flutter pub get 来获取依赖。
2. 初始化 Tapjoy
在使用 Tapjoy 广告之前,你需要初始化 Tapjoy SDK。通常在你的应用的 main.dart 文件中进行初始化。
import 'package:cs_tapjoy_ad/cs_tapjoy_ad.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 Tapjoy
await CsTapjoyAd.initialize(
sdkKey: 'YOUR_TAPJOY_SDK_KEY', // 替换为你的 Tapjoy SDK Key
debug: true, // 是否开启调试模式
);
runApp(MyApp());
}
3. 请求和显示广告
Tapjoy 提供了多种广告形式,如激励视频广告、插页式广告等。以下是如何请求和显示激励视频广告的示例。
激励视频广告
import 'package:flutter/material.dart';
import 'package:cs_tapjoy_ad/cs_tapjoy_ad.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _isRewarded = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Tapjoy Ad Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(_isRewarded ? 'Rewarded!' : 'Watch a video to get reward'),
ElevatedButton(
onPressed: () async {
// 请求并显示激励视频广告
bool isRewarded = await CsTapjoyAd.showRewardedVideoAd(
placementName: 'your_placement_name', // 替换为你的广告位名称
);
setState(() {
_isRewarded = isRewarded;
});
},
child: Text('Show Rewarded Video'),
),
],
),
),
);
}
}
插页式广告
import 'package:flutter/material.dart';
import 'package:cs_tapjoy_ad/cs_tapjoy_ad.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Tapjoy Ad Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
// 请求并显示插页式广告
await CsTapjoyAd.showInterstitialAd(
placementName: 'your_placement_name', // 替换为你的广告位名称
);
},
child: Text('Show Interstitial Ad'),
),
),
);
}
}
4. 处理广告事件
你可以监听广告的加载、显示、关闭等事件,以便在广告生命周期中进行相应的处理。
CsTapjoyAd.setAdListener(
onAdLoaded: (placementName) {
print('Ad loaded: $placementName');
},
onAdFailed: (placementName, error) {
print('Ad failed: $placementName, error: $error');
},
onAdClosed: (placementName) {
print('Ad closed: $placementName');
},
onAdRewarded: (placementName, reward) {
print('Ad rewarded: $placementName, reward: $reward');
},
);

