Android 项目中 Mopub 广告基础使用(插屏和横幅)

1、下载 MoPub Android SDK

repositories {
    mavenCentral() // MoPub SDK is now available in Maven Central
    ...
}

android {
      compileOptions {
          sourceCompatibility JavaVersion.VERSION_1_8
          targetCompatibility JavaVersion.VERSION_1_8
      }
}

dependencies {

    // For banners
    implementation('com.mopub:mopub-sdk-banner:+@aar') {
        transitive = true
    }

    // For fullscreen ads
    implementation('com.mopub:mopub-sdk-fullscreen:+@aar') {
        transitive = true
    }

    // For native static (images).
    implementation('com.mopub:mopub-sdk-native-static:+@aar') {
        transitive = true
    }
}

2、更新 Android 清单
2.1、声明权限:

<!-- Required permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- Optional permissions. Will pass Lat/Lon values when available. Choose either Coarse or Fine -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

//ACCESS_COARSE_LOCATION或者ACCESS_FINE_LOCATION仅当您希望设备自动发送用户的位置以进行定位时才需要。

//WRITE_EXTERNAL_STORAGE (可选并用于 MRAID 2.0 storePicture 广告)已从 MoPub SDK v.5.9.0 开始删除。

2.2、将此标签添加到以使用 Google Play 服务:

<meta-data android:name="com.google.android.gms.version"
      android:value="@integer/google_play_services_version" />

3、初始化

SdkConfiguration sdkConfiguration = new SdkConfiguration.Builder("mopub_banner_id").build();

MoPub.initializeSdk(this, sdkConfiguration, initSdkListener());

private SdkInitializationListener initSdkListener() {
   return new SdkInitializationListener() {
       @Override
       public void onInitializationFinished() {
           /* MoPub SDK initialized.
           Check if you should show the consent dialog here, and make your ad requests. */
       }
   };
}

4、使用横幅广告
4.1、布局文件中加入

<com.mopub.mobileads.MoPubView
    android:layout_width=""
    android:layout_height=""
    app:moPubAdSize=""></com.mopub.mobileads.MoPubView>

4.2、在java文件中初始化横幅广告

private MoPubView moPubView;
moPubView = (MoPubView) findViewById(R.id.adview);
moPubView.setAdUnitId("xxxxxxxxxxx"); // Enter your Ad Unit ID from www.mopub.com
moPubView.setAdSize(MoPubAdSize); // Call this if you are not setting the ad size in XML or wish to use an ad size other than what has been set in the XML. Note that multiple calls to `setAdSize()` will override one another, and the MoPub SDK only considers the most recent one.
moPubView.loadAd(MoPubAdSize); // Call this if you are not calling setAdSize() or setting the size in XML, or if you are using the ad size that has not already been set through either setAdSize() or in the XML
moPubView.loadAd();

4.3、横幅所在视图销毁时,一起销毁

moPubView.destroy();

5、使用插页式广告
5.1、在java文件中初始化并加载插页式广告

private MoPubInterstitial mInterstitial;
mInterstitial = new MoPubInterstitial(this, YOUR_INTERSTITIAL_AD_UNIT_ID_HERE);
mInterstitial.load();

5.2、在Java文件中展示插页式广告

if (mInterstitial.isReady()) {
	mInterstitial.show();
} else {
	// Caching is likely already in progress if `isReady()` is false.
	// Avoid calling `load()` here and instead rely on the callbacks as suggested below.
}

5.3、在视图销毁时,销毁该插页式广告

@Override
     protected void onDestroy() {
         mInterstitial.destroy();
         super.onDestroy();
     }

5.4、加载插页式广告后,状态的回调

//设置回调
 mInterstitial.setInterstitialAdListener(this);
 //回调的方法
 // InterstitialAdListener methods
@Override
  public void onInterstitialLoaded(MoPubInterstitial interstitial) {
      // The interstitial has been cached and is ready to be shown.
  }

  @Override
  public void onInterstitialFailed(MoPubInterstitial interstitial, MoPubErrorCode errorCode) {
  	// The interstitial has failed to load. Inspect errorCode for additional information.
  }

  @Override
  public void onInterstitialShown(MoPubInterstitial interstitial) {
  	// The interstitial has been shown. Pause / save state accordingly.
  }

  @Override
  public void onInterstitialClicked(MoPubInterstitial interstitial) {}

  @Override
  public void onInterstitialDismissed(MoPubInterstitial interstitial) {
  	// The interstitial has being dismissed. Resume / load state accordingly.
  }

https://developers.mopub.com/publishers/android/integrate/(下载 sdk,更新 Android 清单)
https://developers.mopub.com/publishers/android/initialize/(初始化)
https://developers.mopub.com/publishers/android/test/(测试 key 以及错误代码、常见错误)
https://developers.mopub.com/publishers/android/banner/(横幅广告)
https://developers.mopub.com/publishers/android/interstitial/#step-2-display-an-interstitial-ad(插页式广告)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值