Android AdMob教程

This is android admob tutorial.

这是android admob教程。

AdMob is an ad network by Google that allows to monetize mobile apps. In this tutorial I will guide you to integrate admob in android app.

AdMob是Google的广告网络,可通过移动应用获利。 在本教程中,我将指导您将admob集成到android应用中。

Here you will learn about two types of ads.

在这里,您将了解两种广告。

Banner Ad: It occupies a small portion of activity.

标语广告:它只占一小部分活动。

Interstitial Ad: Occupies full screen. Generally shown while moving from one activity to another.

插页式广告:占据全屏。 从一个活动转到另一个活动时通常显示。

Android AdMob教程 (Android AdMob Tutorial)

AdMob控制台 (AdMob Console)

Go to https://apps.admob.com and login with your google account.

转到https://apps.admob.com并使用您的Google帐户登录。

Now go to Monetize and click on Monetize New App button.

现在转到“ 获利”并单击“ 新应用获利”按钮。

Enter name of app and then create a banner and an interstitial ad unit. You will get id for each ad unit. Just keep it somewhere, we will require it later.

输入应用名称,然后创建横幅和插页式广告单元。 您将获得每个广告单元的ID。 只要将其保存在某个地方,我们稍后便会要求它。

Android AdMob Tutorial 1

Android专案 (Android Project)

Create a new android studio project with package name com.admobexample

使用包名称com.admobexample创建一个新的android studio项目

We have to add dependency for google admob ads. Just add following line of code in build.gradle file under dependency section. Sync the project.

我们必须为Google admob广告添加依赖关系。 只需在“ dependency”部分的build.gradle文件中添加以下代码行即可 。 同步项目。

compile 'com.google.android.gms:play-services-ads:8.4.0'

Add internet access permission in AndroidManifest.xml file.

AndroidManifest.xml文件中添加Internet访问权限。

<uses-permission android:name="android.permission.INTERNET" />

1. Banner Ad

1.横幅广告

For banner ad we have to use <com.google.android.gms.ads.AdView> widget in layout xml.

对于横幅广告,我们必须在布局xml中使用<com.google.android.gms.ads.AdView>小部件。

<com.google.android.gms.ads.AdView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ad1"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-9638594751160880/2769913487"/>

Just replace the ad unit id with your banner ad unit id. Make sure the root layout element contains following attribute.

只需将广告单元ID替换为横幅广告单元ID。 确保根布局元素包含以下属性。

xmlns:ads="http://schemas.android.com/apk/res-auto"

In our activity we have to create an instance of AdRequest and then load it in AdView.

在我们的活动中,我们必须创建一个AdRequest实例,然后将其加载到AdView中

2. Interstitial Ad

2.非页内广告

For interstitial ad we don’t have to use any widget in layout xml. First make an instance of AdRequest and InterstitialAd. Set ad unit id for interstitial ad and then load the AdRequest inside InterstitialAd. We will add a listener to InterstitialAd instance and show the ad only when it is fully loaded.

对于插页式广告,我们不必在布局xml中使用任何小部件。 首先创建一个AdRequestInterstitialAd实例。 设置非页广告的广告单元ID,然后将AdRequest加载到InterstitialAd中 。 我们将向InterstitialAd实例添加一个侦听器,并仅在广告完全加载后才显示。

Note: When you use a newly created ad unit then it will take some time to start showing ads. Instead of showing live ads you can show test ads. Just read the test ad section at the end of this tutorial.

注意:当您使用新创建的广告单元时,将需要一些时间才能开始展示广告。 除了展示实时广告,您还可以展示测试广告。 只需阅读本教程末尾的测试广告部分即可。

Add following code in respective files.

在相应的文件中添加以下代码。

activity_main.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.admobexample.MainActivity">
 
    <com.google.android.gms.ads.AdView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ad1"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-9638594751160880/2769913486"/>
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open Interstitial Ad"
        android:layout_centerInParent="true"
        android:id="@+id/button1"/>
 
 
</RelativeLayout>

MainActivity.java

MainActivity.java

package com.admobexample;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
 
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
 
public class MainActivity extends AppCompatActivity {
    AdView ad1;
    Button button1;
    InterstitialAd iad;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        button1 =(Button)findViewById(R.id.button1);
        ad1 = (AdView)findViewById(R.id.ad1);
 
        //banner ad
        AdRequest request = new AdRequest.Builder().build();
        ad1.loadAd(request);
 
        //interstitial ad
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iad = new InterstitialAd(MainActivity.this);
                AdRequest request = new AdRequest.Builder().build();
                iad.setAdUnitId("ca-app-pub-9638594751160880/5583779080");  //replace ad unit id with yours
                iad.loadAd(request);
 
                iad.setAdListener(new AdListener() {
                    @Override
                    public void onAdLoaded() {
                        //show interstitial ad when it is fully loaded
                        if(iad.isLoaded()){
                            iad.show();
                        }
                    }
                });
            }
        });
    }
}

Finally run the app.

最终运行该应用程序。

The banner ad will be displayed automatically when activity is launched but interstitial ad will be displayed on button click.

启动活动后,横幅广告将自动显示,但点击按钮后将显示插页式广告。

屏幕截图 (Screenshots)

Android AdMob Tutorial 2

如何显示测试广告? (How to show test ads?)

AdMod doesn’t allows you to click on ads yourself. It may be possible that you will accidently click on ads. So in that case your account can be banned. To remain on safer side use test ads while you are developing the app.

AdMod不允许您自己点击广告。 您可能会意外点击广告。 因此,在这种情况下,您的帐户可能会被禁止。 为了保持安全,在开发应用程序时,请使用测试广告。

You can find following line of code in andorid logcat when you will run the app.

运行应用程序时,您可以在andorid logcat中找到以下代码行。

Use AdRequest.Builder.addTestDevice(“BB93E7FC72412E6AF38CD7317F5DA20C”) to get test ads on this device

使用AdRequest.Builder.addTestDevice(“ BB93E7FC72412E6AF38CD7317F5DA20C”)在此设备上获取测试广告

The string in double quotes is the unique id for the device in which you are running the app. To show test ads just use addTestDevice() method while making AdRequest instance. It can be done in following way.

双引号中的字符串是您在其中运行应用程序的设备的唯一ID。 要显示测试广告,只需在制作AdRequest实例时使用addTestDevice()方法。 可以通过以下方式完成。

AdRequest request = new AdRequest.Builder().addTestDevice("BB93E7FC72412E6AF38CD7317F5DA20C").build();

Replace the string in double quotes with the id that you got from your logcat.

将双引号中的字符串替换为从logcat获得的ID。

When you are making the app live just remove addTestDevice() method to remove test ads and show live ads.

当您使应用程序上线时,只需删除addTestDevice()方法即可删除测试广告并显示实时广告。

Comment below if you are facing any difficulty in above android admob tutorial.

如果您在上述android admob教程中遇到任何困难,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2016/10/android-admob-tutorial.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值