日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android AdMob教程

發(fā)布時(shí)間:2023/12/16 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android AdMob教程 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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的廣告網(wǎng)絡(luò),可通過(guò)移動(dòng)應(yīng)用獲利。 在本教程中,我將指導(dǎo)您將admob集成到android應(yīng)用中。

Here you will learn about two types of ads.

在這里,您將了解兩種廣告。

Banner Ad:?It occupies a small portion of activity.

標(biāo)語(yǔ)廣告:它只占一小部分活動(dòng)。

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

插頁(yè)式廣告:占據(jù)全屏。 從一個(gè)活動(dòng)轉(zhuǎn)到另一個(gè)活動(dòng)時(shí)通常顯示。

Android AdMob教程 (Android AdMob Tutorial)

AdMob控制臺(tái) (AdMob Console)

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

轉(zhuǎn)到https://apps.admob.com并使用您的Google帳戶登錄。

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

現(xiàn)在轉(zhuǎn)到“ 獲利”并單擊“ 新應(yīng)用獲利”按鈕。

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.

輸入應(yīng)用名稱,然后創(chuàng)建橫幅和插頁(yè)式廣告單元。 您將獲得每個(gè)廣告單元的ID。 只要將其保存在某個(gè)地方,我們稍后便會(huì)要求它。

Android專案 (Android Project)

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

使用包名稱com.admobexample創(chuàng)建一個(gè)新的android studio項(xiàng)目

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廣告添加依賴關(guān)系。 只需在“ dependency”部分的build.gradle文件中添加以下代碼行即可 。 同步項(xiàng)目。

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

Add internet access permission in AndroidManifest.xml file.

AndroidManifest.xml文件中添加Internet訪問(wèn)權(quán)限。

<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.

對(duì)于橫幅廣告,我們必須在布局xml中使用<com.google.android.gms.ads.AdView>小部件。

<com.google.android.gms.ads.AdViewandroid: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.

在我們的活動(dòng)中,我們必須創(chuàng)建一個(gè)AdRequest實(shí)例,然后將其加載到AdView中

2. Interstitial Ad

2.非頁(yè)內(nèi)廣告

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.

對(duì)于插頁(yè)式廣告,我們不必在布局xml中使用任何小部件。 首先創(chuàng)建一個(gè)AdRequestInterstitialAd實(shí)例。 設(shè)置非頁(yè)內(nèi)廣告的廣告單元ID,然后將AdRequest加載到InterstitialAd中 。 我們將向InterstitialAd實(shí)例添加一個(gè)偵聽(tīng)器,并僅在廣告完全加載后才顯示。

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.

注意:當(dāng)您使用新創(chuàng)建的廣告單元時(shí),將需要一些時(shí)間才能開(kāi)始展示廣告。 除了展示實(shí)時(shí)廣告,您還可以展示測(cè)試廣告。 只需閱讀本教程末尾的測(cè)試廣告部分即可。

Add following code in respective files.

在相應(yīng)的文件中添加以下代碼。

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.AdViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/ad1"ads:adSize="BANNER"ads:adUnitId="ca-app-pub-9638594751160880/2769913486"/><Buttonandroid: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;@Overrideprotected 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 adAdRequest request = new AdRequest.Builder().build();ad1.loadAd(request);//interstitial adbutton1.setOnClickListener(new View.OnClickListener() {@Overridepublic 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 yoursiad.loadAd(request);iad.setAdListener(new AdListener() {@Overridepublic void onAdLoaded() {//show interstitial ad when it is fully loadedif(iad.isLoaded()){iad.show();}}});}});} }

Finally run the app.

最終運(yùn)行該應(yīng)用程序。

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

啟動(dòng)活動(dòng)后,橫幅廣告將自動(dòng)顯示,但點(diǎn)擊按鈕后將顯示插頁(yè)式廣告。

屏幕截圖 (Screenshots)

如何顯示測(cè)試廣告? (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不允許您自己點(diǎn)擊廣告。 您可能會(huì)意外點(diǎn)擊廣告。 因此,在這種情況下,您的帳戶可能會(huì)被禁止。 為了保持安全,在開(kāi)發(fā)應(yīng)用程序時(shí),請(qǐng)使用測(cè)試廣告。

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

運(yùn)行應(yīng)用程序時(shí),您可以在andorid logcat中找到以下代碼行。

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

使用AdRequest.Builder.addTestDevice(“ BB93E7FC72412E6AF38CD7317F5DA20C”)在此設(shè)備上獲取測(cè)試廣告

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.

雙引號(hào)中的字符串是您在其中運(yùn)行應(yīng)用程序的設(shè)備的唯一ID。 要顯示測(cè)試廣告,只需在制作AdRequest實(shí)例時(shí)使用addTestDevice()方法。 可以通過(guò)以下方式完成。

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

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

將雙引號(hào)中的字符串替換為從logcat獲得的ID。

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

當(dāng)您使應(yīng)用程序上線時(shí),只需刪除addTestDevice()方法即可刪除測(cè)試廣告并顯示實(shí)時(shí)廣告。

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

如果您在上述android admob教程中遇到任何困難,請(qǐng)?jiān)谙旅嬖u(píng)論。

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

總結(jié)

以上是生活随笔為你收集整理的Android AdMob教程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。