在Android应用程序中实施广告

摘掉这篇文章 (Take away from this article)

You’ll learn how to implement banner, and Interstitial ads from advertising companies like Admob(from Google), Facebook and StartApp in your android applications. This article also contains the best practices that I learned in five years of my experience with advertisements in mobile applications.

您将学习如何在Android应用程序中实施来自Admob(来自Google),Facebook和StartApp等广告公司的横幅广告和非页内广告。 本文还包含了我五年来在移动应用程序广告中所学到的最佳实践。

This article might be concentrating on how to display ads in android applications, but the overview of how things work is similar across the platforms. For suppose, in mobile applications, advertising companies use device ID to identify a specific user, whereas, on the web, they use cookies.

本文可能专注于如何在android应用程序中展示广告,但是在所有平台上,其工作原理概述都是相似的。 假设,在移动应用程序中,广告公司使用设备ID来标识特定用户,而在网络上,他们使用Cookie。

介绍 (Introduction)

In-app advertising is one of the best monetizing strategies for mobile application developers, in which they get paid to show advertisements. Displaying ads to make money isn’t a new strategy. Decades ago, newspapers are one of the best advertise strategy. As the world around us evolving rapidly, today, mobile has become an easy, effective and affordable way to meet one’s needs.

对于移动应用程序开发人员来说,应用程序内广告是最好的获利策略之一,他们可以通过付费获得展示广告的机会。 展示广告来赚钱不是新策略。 几十年前,报纸是最好的广告策略之一。 随着我们周围世界的飞速发展,今天,移动已成为满足个人需求的简便,有效且价格合理的方式。

This makes in-app advertising a vital marketing option for companies and agencies. With companies like Google and Facebook tracking users’ behavior, in-app ads can be served to a specific user with pinpoint accuracy.

这使得应用内广告成为公司和代理商的重要营销选择。 通过Google和Facebook等公司跟踪用户的行为,可以将应用内广告精确地投放给特定用户。

Admob is the most popular advertising platform for mobile applications with strict implementation guidelines and they pay the most when compared to others. Next comes Facebook; they do have guidelines to follow and their interface is quite confusing, but they also pay reasonably when compared to others after AdMob. Apart from Admob and Facebook, StartApp is the only advertising platform that I found to pay a reasonable amount to mobile publishers.

Admob是适用于移动应用程序的最受欢迎的广告平台,具有严格的实施准则,与其他平台相比,它们支付的费用最高。 接下来是Facebook; 他们确实有遵循的准则,其界面也相当混乱,但是与AdMob之后的其他人相比,他们也支付合理的费用。 除了Admob和Facebook,StartApp是我发现向移动发布者支付合理金额的唯一广告平台。

广告类型 (Types of Ads)

There are mainly two types of ads — banner and Interstitial ads. Each has a specific purpose. All most all the mobile advertising companies follow the same pattern. Let’s explore them one-by-one:

广告主要有两种类型:横幅广告和非页内广告。 每个都有特定的目的。 所有大多数移动广告公司都遵循相同的模式。 让我们一一探讨它们:

横幅广告 (Banner Ad)

A banner ad is typically a rectangle advertisement that occupies a spot in the mobile layout, preferably at the top or bottom of the screen. They stay on the screen while the user is interacting with the content. If you’re new to the mobile advertisement, this is the best option to get started.

标语广告通常是矩形广告,其占据移动布局中的位置,最好在屏幕的顶部或底部。 当用户与内容进行交互时,它们停留在屏幕上。 如果您不熟悉移动广告,这是入门的最佳选择。

非页内广告 (Interstitial Ad)

An interstitial ad is a full-screen webpage ad that covers the entire interface of the host app. They’re preferable to display between activity/fragment navigation.

插页式广告是一种全屏网页广告,涵盖了主机应用程序的整个界面。 最好在活动/片段导航之间显示。

Admob广告 (Admob Ads)

Let’s start with Google’s advertising platform — Admob. You can create an AdMob account and necessary id’s to use in the application here. It’s a simple process, so I’m not discussing it. If you’ve any doubts, feel free to comment.

让我们从Google的广告平台Admob开始。 您可以在此处创建一个AdMob帐户以及在应用程序中使用的必要ID。 这是一个简单的过程,所以我不在这里讨论。 如有任何疑问,请随时发表评论。

积分 (Integration)

Once you completed with the account setup, open your existing app and include the following line under dependencies tag in app-level build.gradle file to integrate AdMob library in your project.

完成帐户设置后,打开您现有的应用程序,并将以下行添加到应用程序级build.gradle文件中的dependencies标签下,以将AdMob库集成到您的项目中。

dependencies {
implementation 'com.google.android.gms:play-services-ads:19.1.0'
}

The next step is to add necessary meta-data in the manifest file, as shown below:

下一步是在清单文件中添加必要的元数据,如下所示:

<manifest>
    <application>
        <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~334751171332 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="admob_app_id_here"/>
    </application>
</manifest>

横幅广告 (Banner Ad)

Now it’s time to display your first banner ad. As I said, banner ads are something that has to be displayed inside the layout. So the first step is to include a banner ad in your layout. To do so, include the following view in the activity/fragment layout file.

现在该展示您的第一个横幅广告了。 正如我所说,横幅广告是必须在版面内显示的广告。 因此,第一步是在您的布局中添加横幅广告。 为此,在活动/片段布局文件中包括以下视图。

...
  <com.google.android.gms.ads.AdView
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/adView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_alignParentBottom="true"
      app:adSize="BANNER"
      app:adUnitId="banner_add_id_here">
  </com.google.android.gms.ads.AdView>
...

Next, in the java file, we need to load the ad, as shown below:

接下来,在Java文件中,我们需要加载广告,如下所示:

mAdView = findViewById(R.id.adView);AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

That is all we need to do, when you run the app; you’ll see the ad at the particular spot in the layout. This is one way of implementing it, but we can also load the banner ad directly from Java file, as shown below:

当您运行应用程序时,这就是我们要做的; 您会在版式中的特定位置看到广告。 这是实现它的一种方法,但是我们也可以直接从Java文件加载横幅广告,如下所示:

val adView = AdView(context)
adView.adSize =  AdSize.BANNER
adView.adUnitId = "admob_banner_ad_id"
viewInLayout.addView(adView)

非页内广告 (Interstitial Ad)

Interstitial ads are full-page ads, so you don’t need to include it in your layout. The basic overview of loading an AdMob interstitial ad is to create an interstitial ad object then add an AdListener to monitor ad loading states. Once the ad loaded successfully, we need to invoke the show method on the interstitial ad object. Have a look:

插页式广告是整页广告,因此您无需将其包括在版式中。 加载AdMob插页式广告的基本概述是创建一个插页式广告对象,然后添加AdListener来监视广告的加载状态。 广告成功加载后,我们需要在插页式广告对象上调用show方法。 看一看:

val miInterstitialAd = InterstitialAd(context)
miInterstitialAd.adUnitId = "admob_interstitial_ad_id"
val adRequest = AdRequest.Builder().build()
  
miInterstitialAd.adListener = object : AdListener() {
    override fun onAdClosed() {
        // Code to be executed when the interstitial ad is closed.
    }
    override fun onAdFailedToLoad(paramAnonymousInt: Int) {
        // Code to be executed when an ad request fails.
    }
    override fun onAdLeftApplication() {
        // Code to be executed when the user has left the app.
    }
    override fun onAdLoaded() {
         miInterstitialAd.show()
    }
    override fun onAdOpened() {
        // Code to be executed when the ad is displayed.
    }
}


miInterstitialAd.loadAd(adRequest2)

Facebook广告 (Facebook Ads)

The next stop is Facebook ads. We need to create an account in the Facebook audience network and then create the Facebook banner and Interstitial ad id’s. Without any further delay, let’s get started.

下一站是Facebook广告。 我们需要在Facebook受众网络中创建一个帐户,然后创建Facebook标语和非页内广告ID。 不用再拖延了,让我们开始吧。

积分 (Integration)

To include Facebook ads, we need to add Facebook audience network library to your project, as shown below:

要包含Facebook广告,我们需要将Facebook受众网络库添加到您的项目中,如下所示:

implementation "com.facebook.android:audience-network-sdk:5.+"

横幅广告 (Banner Ad)

Displaying Facebook banner ads is similar to AdMob banner ads. We need to create the banner ad object and then add that object to the view in the hierarchy. Finally we need to invoke loadAd method on the banner object. Have a look:

显示Facebook标语广告类似于AdMob标语广告。 我们需要创建横幅广告对象,然后将该对象添加到层次结构的视图中。 最后,我们需要在loadAd对象上调用loadAd方法。 看一看:

val adView = com.facebook.ads.AdView(context, "facebook_banner_id", AdSize.BANNER_HEIGHT_50)
viewToAdd.addView(adView)
adView.loadAd()

非页内广告 (Interstitial Ad)

In the above Interstitial Ad example, we’ve used the AdMob Interstitial Ad, but here we use Facebook Interstitial Ad, and the rest of the things are almost similar. There might be minor syntax changes, but the implementation is almost identical. Have a look:

在上面的插页式广告示例中,我们使用了AdMob插页式广告,但在这里我们使用了Facebook插页式广告,其余情况几乎相似。 语法可能会稍作更改,但是实现几乎相同。 看一看:

val interstitialAd = com.facebook.ads.InterstitialAd(mContext, "facebook_intestitial_Placement_ID")
interstitialAd.setAdListener(object : com.facebook.ads.InterstitialAdListener {
                override fun onInterstitialDisplayed(p0: com.facebook.ads.Ad?) {


                }


                override fun onAdClicked(p0: com.facebook.ads.Ad?) {


                }


                override fun onInterstitialDismissed(p0: com.facebook.ads.Ad?) {
                   
                }


                override fun onError(p0: com.facebook.ads.Ad?, p1: AdError?) {
                    
                }


                override fun onAdLoaded(p0: com.facebook.ads.Ad?) {
                      interstitialAd.show();
                }


                override fun onLoggingImpression(p0: com.facebook.ads.Ad?) {


                }


            })
            interstitialAd.loadAd()

StartApp广告 (StartApp Ads)

Now, it’s time to implement StartApp Ads. First, you need to create an account here and generate an app id to display ads in mobile applications. As the process is very basic, I’m not discussing it here.

现在,是时候实施StartApp Ads了。 首先,您需要在此处创建一个帐户并生成一个应用ID,以在移动应用中展示广告。 由于该过程非常基本,因此我不在这里讨论。

积分 (Integration)

Include the following line under the dependencies tag in the app level build.gradle file.

在应用程序级别build.gradle文件中的dependencies标签下添加以下行。

implementation 'com.startapp:inapp-sdk:3.6.8'

横幅广告 (Banner Ad)

Displaying startapp banner ad is no different from AdMob and Facebook banner ads. When displaying ads with startapp, we don’t need a banner key separately. StartApp only provides appid, which is used to display all types of ads. Let’s see how to implement a banner ad:

显示startapp横幅广告与AdMob和Facebook横幅广告没有什么不同。 使用startapp展示广告时,我们不需要单独的横幅键。 StartApp仅提供appid ,用于显示所有类型的广告。 让我们看看如何实施横幅广告:

val startAppBanner = BannerStandard(activity)
viewInLayout.addView(startAppBanner)
startAppBanner.showBanner()

非页内广告 (Interstitial Ad)

To display startapp interstitial ads first we need to create startAppAd instance, as shown below:

要首先显示startapp非页内广告,我们需要创建startAppAd实例,如下所示:

val startAppAd = StartAppAd(activity);

To display the startapp interstitial ad, we need to invoke loadAd function with AdEventListener. In which we’ve two functions such as onReceiveAd & onFailedToReceiveAd. In the onReceiveAd method, we need to invoke the showAd method to display the ad.

要显示startapp插页式广告,我们需要使用AdEventListener调用loadAd函数。 其中有两个函数,例如onReceiveAdonFailedToReceiveAd 。 在onReceiveAd方法中,我们需要调用showAd方法以显示广告。

startAppAd.loadAd(object : AdEventListener {
    override fun onReceiveAd(ad: Ad) {
        startAppAd.showAd()
    }
    override fun onFailedToReceiveAd(ad: Ad) {
        //code to implement if ad loading failed
    }
})

最佳实践 (Best Practices)

  1. Don’t place banner ads where users might click accidentally. Advertisement agencies like admob will take such actions very seriously.

    请勿将横幅广告放置在用户可能会意外点击的位置。 广告代理(例如admob)将非常认真地采取此类行动。
  2. Never show Interstitial Ad while user is interacting with the content.

    用户与内容互动时,切勿显示非页内广告。
  3. Always use test ad ids while you’re developing or add you device to testers list.

    在开发过程中,请始终使用测试广告ID,或将设备添加到测试人员列表。
  4. Your admob account will be terminated if you try to access the ads often assuming to increase impressions.

    如果您经常尝试增加展示次数来尝试访问广告,则admob帐户将被终止。
  5. Always show progress dialog when you start loading Interstitial Ad and dismiss it right before you show the ad. In this way accidental clicks will be restricted to maximum extent.

    当您开始加载非页内广告时,始终显示进度对话框,并在显示广告前立即关闭它。 这样,意外点击将受到最大程度的限制。

If you violate any of the admob guidelines more than twice or thrice they will terminate your account. So be careful with admob.

如果您违反任何一项Admob指南两次或三次以上,它们将终止您的帐户。 因此,请谨慎使用admob。

I hope you learn something useful, thanks for reading.

希望您能学到一些有用的东西,感谢您的阅读。

You can find me on Medium, Twitter, Quora and LinkedIn.

您可以在MediumTwitterQuoraLinkedIn上找到我。

翻译自: https://medium.com/android-dev-hacks/implementing-ads-in-your-android-application-28ac676024aa

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值