[英]I am having trouble creating an Interstitial ad in my android app
我正在為Android開發數學類型的游戲。 但我想制作一個插頁式廣告。 Google有一個創建示例的示例,但是它有一個創建廣告的按鈕。 我希望我的廣告僅在活動開始時開始,而用戶只需將其關閉。
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
import android.content.Intent;
import java.util.concurrent.TimeUnit;
import android.os.CountDownTimer;
import com.google.android.gms.ads.*;
public class activity_2 extends MainActivity implements DialogInterface.OnClickListener{
int getWrong=0, num, sum = 0, score = 0, three, four;
Random rand = new Random();
int one = rand.nextInt(10) + 1;
int two = rand.nextInt(10) + 1;
int solution = one + two;
String myString = String.format("%d + %d =", one, two);
InterstitialAd mInterstitialAd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
// beginPlayingGame(); * the google sample had a this class which i dont want to work with
}
});
TextView text = (TextView) findViewById(R.id.Question);
text.setText("" + myString);
TextView timeSTART = (TextView) findViewById(R.id.TimmerT);
timeSTART.setText("10");
requestNewInterstitial();
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
mInterstitialAd.loadAd(adRequest);
}
您無法顯示未加載的添加...!
為什么您不為此目的使用Singleton
?
您應該按照以下步驟操作:
首先,在您的Application(而非Activity)類中創建一個公共靜態InterstitialAd
對象。
然后,您需要使用Application的onCreate
方法而不是Activity的onCreate來初始化mInterstitialAd
對象。
然后,從您的Activity類中調用mInterstitialAd對象的show方法...!
通過此過程,您可以在應用的任何位置調用show Google Add ...!
您的Application類應如下所示,
public class DefaultApplication extends Application {
public static InterstitialAd mInterstitialAd;
@Override
public void onCreate() {
super.onCreate();
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
// beginPlayingGame(); * the google sample had a this class which i dont want to work with
}
});
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
mInterstitialAd.loadAd(adRequest);
}
}
要從Activity的onCreate加載“添加”,請執行以下操作,
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
DefaultApplication.mInterstitialAd.show();
}
首先,您必須初始化MobileAds SDK。
因此,轉到您的應用程序並執行缺少的操作:
MobileAds.initialize(context, getString(R.string.banner_ad_unit_id));
之后,只需進入“活動”並展示插頁式廣告:
if (interstitialAd != null && interstitialAd.isLoaded()) {
interstitialAd.show();
}
Admob示例使用一個按鈕,這樣您可以輕松地使用該概念,還可以表明您需要特定的事件來展示廣告。
您需要在應用中找到一個特定的點,例如在游戲/關卡之間,您可以在該點顯示廣告。
在應用啟動時顯示廣告會打斷用戶流量,提供糟糕的用戶體驗,並且違反Admob政策,並且可能會禁止您的帳戶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.