更新记录
1.0.0(2020-12-16) 下载此版本
1.新增第一版本 2.功能:GoogleAdMob 激励视频,横幅广告,java源码
平台兼容性
Uni-Android-GoogleAdmob(仅支持安卓离线)
玩在线打包的请绕道,玩在线打包的请绕道,玩在线打包的请绕道
环境
1.HX2.9.8、离线Android-SDK v2.9.8.80513_20201111(HBuilder-Hello工程)、AdnroidStudio v3.6.3;
2.minSdkVersion 16 或更高版本;
3.compileSdkVersion 28 或更高版本;
注:由于使用了最新版的Google Admob库,所以需要做adnroidX兼容,而在线打包查了下说不兼容,有兴趣可以自己去捣鼓下; 详情见:https://developers.google.cn/admob/android/quick-start
步骤
1.解压离线包,由于我们公司都是离线打包,用到的是HBuilder-Hello这个工程,导入as中;
2.新建Module,命名:zhj_googlead;
3.在插件中导入uniapp和google视频必要的库,zhj-googlead/build.gradle中提那添加如下:
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.alibaba:fastjson:1.1.46.android'
compileOnly fileTree(dir: '../app/libs', include: ['uniapp-release.aar'])
// 添加google视频所需要的库
implementation 'com.google.android.gms:play-services-ads:19.5.0'
}
4.app/build.gradle也需要添加依赖
dependencies {
...
// 添加uni-app插件
implementation project(':zhj-googlead')
// 添加google视频所需要的库
implementation 'com.google.android.gms:play-services-ads:19.5.0'
}
5.兼容AndroidX,若为老项目android.support,需在gradle.properties中添加:
android.enableJetifier=true
android.useAndroidX=true
6.Java接口与视频接入: 1)新建类GoogleAdModule作为激励视频拉起接口,此为插件开发模式,继承UniModule,通过onGoogleAdModule拉起,源码如下:
public class GoogleAdModule extends UniModule {
String TAG = "GoogleAdModule";
public static int REQUEST_CODE = 1000;
UniJSCallback m_callback = null;
//‘ca-app-pub-3940256099942544/5224354917’测试广告id 此处必须上架或者出release包才能改为正式广告id
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/5224354917";
private RewardedAd rewardedAd;
boolean isAward = false;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_CODE && data.hasExtra("respond")) {
Log.e("GoogleAdModule", "----"+data.getStringExtra("respond"));
if(m_callback != null) {
JSONObject datas = new JSONObject();
datas.put("code", data.getStringExtra("respond"));
m_callback.invoke(datas);
//callback.invokeAndKeepAlive(data);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
@UniJSMethod (uiThread = true)
public void onGoogleAdModule(UniJSCallback callback){
m_callback = callback;
rewardedAd = new RewardedAd(mUniSDKInstance.getContext(),AD_UNIT_ID);
RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
@Override
public void onRewardedAdLoaded() {
// Ad successfully loaded.
Log.e(TAG, "Ad successfully loaded.--");
Toast.makeText(mUniSDKInstance.getContext(), "Ad successfully loaded", Toast.LENGTH_SHORT).show();
showRewardedVideo();
}
@Override
public void onRewardedAdFailedToLoad(LoadAdError adError) {
// Ad failed to load.
Log.e(TAG, "Ad failed to load."+adError);
Toast.makeText(mUniSDKInstance.getContext(), "LoadAdError", Toast.LENGTH_SHORT).show();
navigateBack("no");
}
};
rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
}
private void showRewardedVideo() {
if (rewardedAd.isLoaded()) {
RewardedAdCallback adCallback = new RewardedAdCallback() {
@Override
public void onRewardedAdOpened() {
// Ad opened.
}
@Override
public void onRewardedAdClosed() {
// Ad closed.
navigateBack("no");
}
@Override
public void onUserEarnedReward(RewardItem rewardItem) {
// User earned reward.
isAward = true;
}
@Override
public void onRewardedAdFailedToShow(AdError adError) {
// Ad failed to display.
Toast.makeText(mUniSDKInstance.getContext(), "失败", Toast.LENGTH_SHORT).show();
navigateBack("no");
}
};
Activity activity = (Activity )mWXSDKInstance.getContext();
rewardedAd.show(activity, adCallback);
} else {
Log.d("TAG", "The rewarded ad wasn't loaded yet.");
}
}
private void navigateBack(String str){
Intent intent = new Intent();
if (isAward){
str = "yes";
}
intent.putExtra("respond", str);
if(m_callback != null) {
JSONObject datas = new JSONObject();
datas.put("code", str);
m_callback.invoke(datas);
}
}
}
2)新建类AdViews作为Banner广告,此为插件开发模式,必须继承UniComponent,源码如下:
public class AdViews extends UniComponent<AdView> {
//‘ca-app-pub-3940256099942544/5224354917’测试广告id 此处必须上架或者出release包才能改为正式广告id
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/6300978111";
public AdViews(UniSDKInstance instance, AbsVContainer parent, AbsComponentData basicComponentData) {
super(instance, parent, basicComponentData);
}
// 初始化AdView
@Override
protected AdView initComponentHostView(Context context) {
AdView adView = new AdView(context);
// 这里默认BANNER ,要多样化的话f12进去看看,有很多不同尺寸的Banner,可自己封装出来
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
MobileAds.initialize(context, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
return adView;
}
@Override
public void onActivityResume() {
super.onActivityResume();
}
@Override
public void onActivityPause() {
super.onActivityPause();
}
@Override
public void onActivityDestroy() {
super.onActivityDestroy();
}
}
7.向uniapp注册这两个类的方法,app/src/main/assets下,新建dcloud_uniplugins.json文件,将上面两个api注册到uniapp,具体格式不懂的看下官方文档;
{
"nativePlugins": [
{
"plugins": [
{
"type": "module",
"name": "zhj-GoogleAdModule",
"class": "com.example.zhj_googlead.GoogleAdModule"
}
]
},
{
"plugins": [
{
"type": "component",
"name": "zhj-GoogleBanner",
"class": "com.example.zhj_googlead.AdViews"
}
]
}
]
}
8.至此,基本都结束了,在uni中直接requireNativePlugin调用就行,特别注意的是Banner广告是基于UniComponent下,必须要在原生渲染nvue中使用
有时间的人可以把视频回调做详细点,Banner也可以多样式调用
完整示例源码
微信搜索小程序《爱上小工具》,在资讯文章中免费获取源码(见怪莫怪,第一次做小程序,就为了加个uv)