java - 如何在 android 中正确实现插页式广告?

标签 java android admob

我有一个贴纸应用程序,我希望插页式广告在按下添加按钮并发生操作后立即出现。问题是广告只出现了几次。我认为这是由于实现。有人可以分析代码并告诉您我可以在哪里让它正常工作吗?

protected void addStickerPackToWhatsApp(String identifier, String stickerPackName) {
    try {
        //if neither WhatsApp Consumer or WhatsApp Business is installed, then tell user to install the apps.
        if (!WhitelistCheck.isWhatsAppConsumerAppInstalled(getPackageManager()) && !WhitelistCheck.isWhatsAppSmbAppInstalled(getPackageManager())) {
            Toast.makeText(this, R.string.add_pack_fail_prompt_update_whatsapp, Toast.LENGTH_LONG).show();
            return;
        }
        final boolean stickerPackWhitelistedInWhatsAppConsumer = WhitelistCheck.isStickerPackWhitelistedInWhatsAppConsumer(this, identifier);
        final boolean stickerPackWhitelistedInWhatsAppSmb = WhitelistCheck.isStickerPackWhitelistedInWhatsAppSmb(this, identifier);
        if (!stickerPackWhitelistedInWhatsAppConsumer && !stickerPackWhitelistedInWhatsAppSmb) {
            //ask users which app to add the pack to.
            launchIntentToAddPackToChooser(identifier, stickerPackName);
        } else if (!stickerPackWhitelistedInWhatsAppConsumer) {
            launchIntentToAddPackToSpecificPackage(identifier, stickerPackName, WhitelistCheck.CONSUMER_WHATSAPP_PACKAGE_NAME);
        } else if (!stickerPackWhitelistedInWhatsAppSmb) {
            launchIntentToAddPackToSpecificPackage(identifier, stickerPackName, WhitelistCheck.SMB_WHATSAPP_PACKAGE_NAME);
        } else {
            Toast.makeText(this, R.string.add_pack_fail_prompt_update_whatsapp, Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Log.e(TAG, "error adding sticker pack to WhatsApp",  e);
        Toast.makeText(this, R.string.add_pack_fail_prompt_update_whatsapp, Toast.LENGTH_LONG).show();
    }

}

private void launchIntentToAddPackToSpecificPackage(String identifier, String stickerPackName, String whatsappPackageName) {
    Intent intent = createIntentToAddStickerPack(identifier, stickerPackName);
    intent.setPackage(whatsappPackageName);
    try {
        startActivityForResult(intent, ADD_PACK);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(this, R.string.add_pack_fail_prompt_update_whatsapp, Toast.LENGTH_LONG).show();
    }
}

//Handle cases either of WhatsApp are set as default app to handle this intent. We still want users to see both options.
private void launchIntentToAddPackToChooser(String identifier, String stickerPackName) {
    Intent intent = createIntentToAddStickerPack(identifier, stickerPackName);
    try {
        startActivityForResult(Intent.createChooser(intent, getString(R.string.add_to_whatsapp)), ADD_PACK);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(this, R.string.add_pack_fail_prompt_update_whatsapp, Toast.LENGTH_LONG).show();
    }
}

@NonNull
private Intent createIntentToAddStickerPack(String identifier, String stickerPackName) {
    Intent intent = new Intent();
    intent.setAction("com.whatsapp.intent.action.ENABLE_STICKER_PACK");
    intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_ID, identifier);
    intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_AUTHORITY, BuildConfig.CONTENT_PROVIDER_AUTHORITY);
    intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_NAME, stickerPackName);

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("my-ad-unit-id");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());

    mInterstitialAd.setAdListener(new AdListener() {
        public void onAdClosed() {
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
        }
    });

    return intent;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == ADD_PACK) {
        if (resultCode == Activity.RESULT_CANCELED) {
            if (data != null) {
                final String validationError = data.getStringExtra("validation_error");
                if (validationError != null) {
                    if (BuildConfig.DEBUG) {
                        //validation error should be shown to developer only, not users.
                        MessageDialogFragment.newInstance(R.string.title_validation_error, validationError).show(getSupportFragmentManager(), "validation error");
                    }
                    Log.e(TAG, "Validation failed:" + validationError);
                }
            } else {
                new StickerPackNotAddedMessageFragment().show(getSupportFragmentManager(), "sticker_pack_not_added");
            }
        }
        if(mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }

}

public static final class StickerPackNotAddedMessageFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity())
                .setMessage(R.string.add_pack_fail_prompt_update_whatsapp)
                .setCancelable(true)
                .setPositiveButton(android.R.string.ok, (dialog, which) -> dismiss())
                .setNeutralButton(R.string.add_pack_fail_prompt_update_play_link, (dialog, which) -> launchWhatsAppPlayStorePage());

        return dialogBuilder.create();
    }

    private void launchWhatsAppPlayStorePage() {
        if (getActivity() != null) {
            final PackageManager packageManager = getActivity().getPackageManager();
            final boolean whatsAppInstalled = WhitelistCheck.isPackageInstalled(WhitelistCheck.CONSUMER_WHATSAPP_PACKAGE_NAME, packageManager);
            final boolean smbAppInstalled = WhitelistCheck.isPackageInstalled(WhitelistCheck.SMB_WHATSAPP_PACKAGE_NAME, packageManager);
            final String playPackageLinkPrefix = "http://play.google.com/store/apps/details?id=";
            if (whatsAppInstalled && smbAppInstalled) {
                launchPlayStoreWithUri("https://play.google.com/store/apps/developer?id=WhatsApp+Inc.");
            } else if (whatsAppInstalled) {
                launchPlayStoreWithUri(playPackageLinkPrefix + WhitelistCheck.CONSUMER_WHATSAPP_PACKAGE_NAME);
            } else if (smbAppInstalled) {
                launchPlayStoreWithUri(playPackageLinkPrefix + WhitelistCheck.SMB_WHATSAPP_PACKAGE_NAME);
            }
        }
    }

    private void launchPlayStoreWithUri(String uriString) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(uriString));
        intent.setPackage("com.android.vending");
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(getActivity(), R.string.cannot_find_play_store, Toast.LENGTH_LONG).show();
        }
    }
}

我在 Intent 中添加了广告加载,因为当点击添加按钮时它会调用 Intent ,然后我通过 if 执行操作后将它们放在显示广告

最佳答案

Admob 提供的广告非常差,填充率较低。因此,如果其中一个网络无法提供广告,我建议您也添加其他广告网络。(例如 Facebook、StartApp、AppLovin 等)

关于java - 如何在 android 中正确实现插页式广告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56450465/

相关文章:

java - 如何将数据添加到不同的模型类android

Android 如何将 Scalr 4.2 或 'java-image-scaling' 用于 HQ 缩略图

admob - 如何使用 ad mob 和 google web designer?

Java编程: Integer value to Hexadecimal

java - 重构以公开用于单元测试的私有(private)方法

android - 下载时进度条没有反应

admob - 来自 Google AdMob 的过多广告调用

ios - 在 UICollectionView 中滚动时 Admob Native Expess Ads 阻塞

java - 带引擎的 Sql 解析器

java - 验证文件java