android admob_将Google AdMob添加到Android应用程序

android admob

AdMob is Google’s advertising platform specifically designed for mobile applications. AdMob allows you to monetize your apps by serving ads selected according to criteria you define. So if you’ve developed an Android app and now you want to make money with your app, in this article I show you how to do it, how to add different ad formats to your apps.

AdMob是专门为移动应用程序设计的Google广告平台。 AdMob允许您通过投放根据您定义的条件选择的广告来通过应用获利。 因此,如果您已经开发了一个Android应用程序,现在又想通过该应用程序赚钱,那么在本文中,我将向您展示如何做到这一点,以及如何向您的应用程序添加不同的广告格式。

To get started, make sure you have integrated Firebase into your project. If you haven’t already done so, here’s how to do it.

首先,请确保已将Firebase集成到项目中。 如果你还没有这样做的话, 这里的如何做到这一点。

在您的AdMob帐户中设置您的应用 (Set up your application in your AdMob account)

  1. To get started you need to create an AdMob account or sign in if you already have one.

    要开始使用,您需要创建一个AdMob帐户或登录(如果已有的话)。

  2. Login to AdMob and open your dashboard. Now, go to Applications> Add an Application> Fill out the information required by AdMob> Click Add, which will generate an AdMob ID for your application.

    登录到AdMob并打开您的信息中心。 现在,转到应用程序>添加应用程序>填写AdMob所需的信息>单击添加,这将为您的应用程序生成一个AdMob ID。
  3. To link your newly added application to Firebase. Select the application, access the application settings and under Application Information you will see an option to link the application to Firebase.

    要将您新添加的应用程序链接到Firebase。 选择应用程序,访问应用程序设置,在“应用程序信息”下,您将看到一个将应用程序链接到Firebase的选项。
  4. Add your AdMob Application ID to your AndroidManifest.xml file by adding the <meta-data>tag.

    通过添加<meta-data>标签,将AdMob应用程序ID添加到AndroidManifest.xml文件中。

<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="ADMOB_APP_ID"
    />

5. Add and initialize the Mobile Ads SDK. Add the dependency of the Google Mobile Ads SDK to the build.gradle module file

5.添加并初始化Mobile Ads SDK。 将Google Mobile Ads SDK的依赖项添加到build.gradle模块文件中

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

You must initialize the Mobile Ads SDK, before you can broadcast any type of announcement made in one of your activities at the start of your application. Call this method only once and as soon as possible, ideally at the launch of the application.

您必须先初始化Mobile Ads SDK,然后才能在应用程序开始时广播其中一项活动中发出的任何类型的公告。 最好仅在应用程序启动时尽快调用此方法一次。

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        MobileAds.initialize(this, "YOUR_ADMOB_APP_ID")
    }
}

选择要在您的应用程序中实施的广告格式 (Choose an ad format to implement in your application)

AdMob offers different ad formats (Banner, Interstitial and Rewarded). You can choose the format that best suits your application’s user experience.

AdMob提供不同的广告格式(横幅广告,非页内广告和奖励广告)。 您可以选择最适合您的应用程序用户体验的格式。

制作广告横幅 (Creation of advertising banners)

Banner ads take up space in the layout of an application, at the top or bottom of the device screen. They remain on the screen while users interact with the application and may refresh automatically after a period of time. To add a banner ad to one of your activities / fragments, you will need to use AdView . In your layout file, add the following code:

横幅广告在设备屏幕顶部或底部的应用程序布局中占据空间。 当用户与应用程序交互时,它们保留在屏幕上,并且可能在一段时间后自动刷新。 要将横幅广告添加到您的活动/片段之一,您将需要使用AdView。 在布局文件中,添加以下代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        ads:adSize = "BANNER"
        ads:adUnitId = "ca-app-pub-3940256099942544~3347511713">


    </com.google.android.gms.ads.AdView>
</LinearLayout>

There are two settings to note here adSize which defines the size of your banner, there are different sizes of banners available that need to be viewed and adUnitId is the unique ID that identifies your unique ad block.

这里需要注意两个设置, adSize定义横幅的大小,需要查看不同大小的横幅,并且adUnitId是标识唯一广告块的唯一ID。

  • Loading an ad

    载入广告

The ad loading is done with the method loadAd() of AdViewclass. it take as parameter a object ot typeAdRequest, which contains execution information (such as targeting information) on a single ad request.

广告加载是通过AdView类的loadAd()方法完成的。 它以类型为AdRequest的对象作为参数,该对象包含有关单个广告请求的执行信息(例如定位信息)。

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.MobileAds




class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        MobileAds.initialize(this) {}


        val mAdView = findViewById<AdView> (R.id.adView)
        val adRequest = AdRequest.Builder (). build ()
        mAdView.loadAd (adRequest)
    }
}
Image for post

制作插页广告 (Creation of interstitial ad)

Interstitial announcements are full screen announcements that cover the interface of their host application. When creating interstitial advertisements, we do not need to define a view in your layout file, they can only be created by programming.

插页式公告是覆盖其宿主应用程序界面的全屏公告。 创建插页式广告时,我们不需要在布局文件中定义视图,只能通过编程来创建它们。

Interstitial announcements are requested and displayed by InterstitialAd objects. Start by instantiating InterstitialAd to set its announcement block ID. This is done in the onCreate() method of an activity.

非页内公告是由InterstitialAd对象请求并显示的。 首先实例化InterstitialAd设置其公告栏ID。 这是在活动的onCreate()方法中完成的。

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        MobileAds.initialize(this) {}


        val mInterstitialAd = InterstitialAd(this)
        mInterstitialAd.adUnitId = "ca-app-pub-3940256099942544/1033173712"
    }
}
  • Loading ad

    正在载入广告

To load an interstitial announcement, call the method loadAd() ofInterstitialA object, this method accept a object to typeAdRequest as unique parameter

要加载非页内广告,请调用InterstitialA对象的loadAd()方法,该方法接受一个对象,将AdRequest类型作为唯一参数

mInterstitialAd.loadAd(AdRequest.Builder().build())
  • Showing the Ad

    显示广告

To display an interstitial, use the method isLoaded() to check that the loading is complete, then call show().

要显示插页式广告,请使用isLoaded()方法检查加载是否完成,然后调用show()

clickButton.setOnClickListener {
            if (mInterstitialAd.isLoaded) {
                mInterstitialAd.show()
            } else {
                Log.d("TAG", "The interstitial wasn't loaded yet.")
            }
        }
Image for post

制作奖励视频广告 (Create Rewarded Video Ads)

Rewarded Video Ads are full screen video ads that users have the ability to watch in full in exchange for rewards built into the application.

奖励视频广告是全屏视频广告,用户可以全看,以换取应用程序内置的奖励。

class MainActivity : AppCompatActivity(), RewardedVideoAdListener {
    lateinit var mRewardedVideoAd : RewardedVideoAd
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        MobileAds.initialize (this)


        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this)
        mRewardedVideoAd.rewardedVideoAdListener = this
       
    }
}

Get theRewardedVideoA. object by using this method MobileAds.getRewardedVideoAdInstance().

获取RewardedVideoA. 使用此方法MobileAds.getRewardedVideoAdInstance()

To be notified of award-winning video lifecycle events, you must implement the RewardedVideoAdListenerclass, The award-winning announcement earpiece is defined using the method setRewardedVideoAdListener().

要获得获奖视频生命周期事件的通知,您必须实现RewardedVideoAdListener类。获奖公告耳机使用setRewardedVideoAdListener()方法定义。

  • Request an award-winning video ad

    索取屡获殊荣的视频广告
class MainActivity : AppCompatActivity(), RewardedVideoAdListener {
    lateinit var mRewardedVideoAd : RewardedVideoAd
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        MobileAds.initialize (this)


        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this)
        mRewardedVideoAd.rewardedVideoAdListener = this
        loadRewardedVideoAd()
        
    }
    private fun loadRewardedVideoAd() {
        mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
            AdRequest.Builder().build())
    }
}
  • Post an award-winning video ad

    发布屡获殊荣的视频广告

The isLoaded() method indicates whether the award-winning video ad request has been successfully processed.

isLoaded()方法指示获奖视频广告请求是否已成功处理。

class MainActivity : AppCompatActivity(), RewardedVideoAdListener {
    lateinit var mRewardedVideoAd : RewardedVideoAd
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        MobileAds.initialize (this)


        //using Rewarded ads
        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this)
        mRewardedVideoAd.rewardedVideoAdListener = this
        loadRewardedVideoAd()
        clickButtonR.setOnClickListener {
            if (mRewardedVideoAd.isLoaded) {
                mRewardedVideoAd.show()
            }
        }
    }
    private fun loadRewardedVideoAd() {
        mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
            AdRequest.Builder().build())
    }


    override fun onRewardedVideoAdClosed() {
        Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show()
    }


    override fun onRewardedVideoAdLeftApplication() {
        Toast.makeText(this, "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show()
    }


    override fun onRewardedVideoAdLoaded() {
        Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show()
    }


    override fun onRewardedVideoAdOpened() {
        TODO("Not yet implemented")
    }


    override fun onRewardedVideoCompleted() {
        Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show()
    }


    override fun onRewarded(p0: RewardItem?) {


    }


    override fun onRewardedVideoStarted() {


    }


    override fun onRewardedVideoAdFailedToLoad(p0: Int) {


    }


}
Image for post

结论 (Conclusion)

Don’t hesitate to ask questions if you can’t understand a concept explained in the article, you can find the complete source code by clicking here.

如果您不理解本文中介绍的概念,请随时提出问题,可以通过单击找到完整的源代码。 在这里

For testing purposes, you can use the sample AdMob Application ID provided by Google:

为了进行测试,您可以使用Google提供的示例AdMob应用ID:

ca-app-pub-3940256099942544~3347511713
ca-app-pub-3940256099942544/1033173712

翻译自: https://proandroiddev.com/adding-google-admob-to-an-android-application-6621d58d92c5

android admob

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android 应用中接入 AdMob 广告,需要进行以下步骤: 1. 注册 AdMob 账号并创建广告单元 2. 在 Android Studio 中添加 AdMob SDK 3. 在 AndroidManifest.xml 文件中添加必要的权限和服务 4. 在布局文件中添加 AdView 控件 5. 在代码中加载广告并显示 以下是一个简单的示例: 1. 注册 AdMob 账号并创建广告单元 在 AdMob 的网站上注册账号,并创建一个广告单元。记下广告单元 ID,稍后将用到。 2. 在 Android Studio 中添加 AdMob SDK 在 app 模块的 build.gradle 文件中添加以下依赖: ```groovy implementation 'com.google.android.gms:play-services-ads:20.4.0' ``` 3. 在 AndroidManifest.xml 文件中添加必要的权限和服务 添加以下权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> ``` 添加以下服务: ```xml <service android:name="com.google.android.gms.ads.AdService" android:exported="false" /> ``` 4. 在布局文件中添加 AdView 控件 在需要显示广告的布局文件中添加 AdView 控件: ```xml <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" ads:adSize="BANNER" ads:adUnitId="YOUR_AD_UNIT_ID" /> ``` 将 `adUnitId` 替换为你的广告单元 ID。 5. 在代码中加载广告并显示 在 Activity 或 Fragment 中加载广告并显示: ```java import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; public class MainActivity extends AppCompatActivity { private AdView mAdView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 加载 AdView 控件 mAdView = findViewById(R.id.adView); // 创建广告请求 AdRequest adRequest = new AdRequest.Builder().build(); // 加载广告 mAdView.loadAd(adRequest); } @Override protected void onPause() { // 暂停广告 mAdView.pause(); super.onPause(); } @Override protected void onResume() { super.onResume(); // 恢复广告 mAdView.resume(); } @Override protected void onDestroy() { // 销毁广告 mAdView.destroy(); super.onDestroy(); } } ``` 以上就是接入 AdMob 广告的基本步骤。需要注意的是,为了获得更好的广告收益,建议遵循 Google 的广告政策并遵守良好的用户体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值