如何使用 AdMob 在活动中加载插页式广告并在下一个活动中显示?

Posted

技术标签:

【中文标题】如何使用 AdMob 在活动中加载插页式广告并在下一个活动中显示?【英文标题】:How to load an Interstitial ad in an activity and show it in the next one with AdMob? 【发布时间】:2021-10-19 12:20:30 【问题描述】:

我正在尝试在一个活动中加载一个插页式广告,并使用 Java 和 AdMob SDK 版本 20 在另一个活动中显示它。 有人知道我该怎么做吗?

第一个活动:StickerPackListActivity

public class StickerPackListActivity extends AddStickerPackActivity 
public static final String EXTRA_STICKER_PACK_LIST_DATA = "sticker_pack_list";
private static final int STICKER_PREVIEW_DISPLAY_LIMIT = 5;
private LinearLayoutManager packLayoutManager;
private RecyclerView packRecyclerView;
private StickerPackListAdapter allStickerPacksListAdapter;
private WhiteListCheckAsyncTask whiteListCheckAsyncTask;
private ArrayList<StickerPack> stickerPackList;
private AdView mBanner1;
private AdView mBanner2;
private AdView mBanner3;
private AdView mBanner4;
public static InterstitialAd mInterstitialAd;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sticker_pack_list);
    packRecyclerView = findViewById(R.id.sticker_pack_list);
    stickerPackList = getIntent().getParcelableArrayListExtra(EXTRA_STICKER_PACK_LIST_DATA);
    showStickerPackList(stickerPackList);
    if (getSupportActionBar() != null) 
        getSupportActionBar().setTitle(getResources().getQuantityString(R.plurals.title_activity_sticker_packs_list, stickerPackList.size()));
    

                        //Questa parte fa andare i banner, non eliminare.
    mBanner1 = findViewById(R.id.adViewtoplist);
    mBanner2 = findViewById(R.id.adViewbottomlist);
    mBanner3 = findViewById(R.id.adViewbottomlist2);
    mBanner4 = findViewById(R.id.adViewbottomlist3);
    AdRequest adRequest = new AdRequest.Builder().build();
    mBanner1.loadAd(adRequest);
    mBanner2.loadAd(adRequest);
    mBanner3.loadAd(adRequest);
    mBanner4.loadAd(adRequest);
                        //End
    InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
            new InterstitialAdLoadCallback() 
                @Override
                public void onAdLoaded(@NonNull InterstitialAd interstitialAd) 
                    mInterstitialAd = interstitialAd;
                
            );

第二个活动:StickerPackDetailsActivity

public class StickerPackDetailsActivity extends AddStickerPackActivity 

/**
 * Do not change below values of below 3 lines as this is also used by WhatsApp
 */
public static final String EXTRA_STICKER_PACK_ID = "sticker_pack_id";
public static final String EXTRA_STICKER_PACK_AUTHORITY = "sticker_pack_authority";
public static final String EXTRA_STICKER_PACK_NAME = "sticker_pack_name";

public static final String EXTRA_STICKER_PACK_WEBSITE = "sticker_pack_website";
public static final String EXTRA_STICKER_PACK_EMAIL = "sticker_pack_email";
public static final String EXTRA_STICKER_PACK_PRIVACY_POLICY = "sticker_pack_privacy_policy";
public static final String EXTRA_STICKER_PACK_LICENSE_AGREEMENT = "sticker_pack_license_agreement";
public static final String EXTRA_STICKER_PACK_TRAY_ICON = "sticker_pack_tray_icon";
public static final String EXTRA_SHOW_UP_BUTTON = "show_up_button";
public static final String EXTRA_STICKER_PACK_DATA = "sticker_pack";

private RecyclerView recyclerView;
private GridLayoutManager layoutManager;
private StickerPreviewAdapter stickerPreviewAdapter;
private int numColumns;
private View addButton;
private View alreadyAddedText;
private StickerPack stickerPack;
private View divider;
private WhiteListCheckAsyncTask whiteListCheckAsyncTask;
public static InterstitialAd mInterstitialAd;

private static final String TAG = "MainActivity";


@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sticker_pack_details);

    boolean showUpButton = getIntent().getBooleanExtra(EXTRA_SHOW_UP_BUTTON, false);
    stickerPack = getIntent().getParcelableExtra(EXTRA_STICKER_PACK_DATA);
    TextView packNameTextView = findViewById(R.id.pack_name);
    TextView packPublisherTextView = findViewById(R.id.author);
    ImageView packTrayIcon = findViewById(R.id.tray_image);
    TextView packSizeTextView = findViewById(R.id.pack_size);

    addButton = findViewById(R.id.add_to_whatsapp_button);
    alreadyAddedText = findViewById(R.id.already_added_text);
    layoutManager = new GridLayoutManager(this, 1);
    recyclerView = findViewById(R.id.sticker_list);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(pageLayoutListener);
    recyclerView.addOnScrollListener(dividerScrollListener);
    divider = findViewById(R.id.divider);
    if (stickerPreviewAdapter == null) 
        stickerPreviewAdapter = new StickerPreviewAdapter(getLayoutInflater(), R.drawable.sticker_error, getResources().getDimensionPixelSize(R.dimen.sticker_pack_details_image_size), getResources().getDimensionPixelSize(R.dimen.sticker_pack_details_image_padding), stickerPack);
        recyclerView.setAdapter(stickerPreviewAdapter);

    
    packNameTextView.setText(stickerPack.name);
    packPublisherTextView.setText(stickerPack.publisher);
    packTrayIcon.setImageURI(StickerPackLoader.getStickerAssetUri(stickerPack.identifier, stickerPack.trayImageFile));
    packSizeTextView.setText(Formatter.formatShortFileSize(this, stickerPack.getTotalSize()));
    addButton.setOnClickListener(v -> addStickerPackToWhatsApp(stickerPack.identifier, stickerPack.name));

    if (getSupportActionBar() != null) 
        getSupportActionBar().setDisplayHomeAsUpEnabled(showUpButton);
        getSupportActionBar().setTitle(showUpButton ? getResources().getString(R.string.title_activity_sticker_pack_details_multiple_pack) : getResources().getQuantityString(R.plurals.title_activity_sticker_packs_list, 1));
    


private void launchInfoActivity(String publisherWebsite, String publisherEmail, String privacyPolicyWebsite, String licenseAgreementWebsite, String trayIconUriString) 
    Intent intent = new Intent(StickerPackDetailsActivity.this, StickerPackInfoActivity.class);
    intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_ID, stickerPack.identifier);
    intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_WEBSITE, publisherWebsite);
    intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_EMAIL, publisherEmail);
    intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_PRIVACY_POLICY, privacyPolicyWebsite);
    intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_LICENSE_AGREEMENT, licenseAgreementWebsite);
    intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_TRAY_ICON, trayIconUriString);
    startActivity(intent);

两个活动都被缩短了,因为我想分别使用第一个和第二个活动的 OnCreate 方法加载和显示广告。

【问题讨论】:

【参考方案1】:

如果您的意思是要点击一个按钮来打开另一个活动,那么广告就会显示 那么很容易你可以在打开其他活动的按钮的onclick中使用这一行

    mInterstitialAd.show(YourCurrentActivity.this); // note that mInterstitialAd is the Ad id

【讨论】:

【参考方案2】:

第一个活动加载广告

public static InterstitialAd mInterstitialAd;

现在创建函数

public void InterstitialAd_load()
AdRequest adRequest = new AdRequest.Builder().build();

      InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
        new InterstitialAdLoadCallback() 
      @Override
      public void onAdLoaded(@NonNull InterstitialAd interstitialAd) 
        // The mInterstitialAd reference will be null until
        // an ad is loaded.
        mInterstitialAd = interstitialAd;
        Log.i(TAG, "onAdLoaded");
      

      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) 
        // Handle the error
        Log.i(TAG, loadAdError.getMessage());
        mInterstitialAd = null;
      
    );

创建展示广告的静态方法

public static void showAd()
if (mInterstitialAd != null) 
  mInterstitialAd.show(MyActivity.this);


第二个Activity导入类

import com.your.packagename.firstactivity.showad;

第二个Activity调用方法

ShowAd();

【讨论】:

我试图这样做。但是,当第二个活动加载时,应用程序崩溃 请在此处发布您的代码,以便我可以为您提供解决方案 尝试“使用静态关键字制作一个展示广告的方法,并在下一个活动中调用它” 您可以在更新的问题中找到它 查看更新的答案

以上是关于如何使用 AdMob 在活动中加载插页式广告并在下一个活动中显示?的主要内容,如果未能解决你的问题,请参考以下文章

适用于单项活动的AdMob插页式广告

AdMob - 同一活动中的横幅和插页式广告 - 好不好?

Admob 和 facebook 插页式广告在同一个活动中

插页式 Admob 广告:“IllegalStateException:只有全屏活动可以请求方向”

在我的应用中加载Admob rewardedVideo时出错

如何在 android studio 中实现 Admob 插页式广告 - Kotlin