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專案 (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.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.
在我們的活動中,我們必須創建一個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中使用任何小部件。 首先創建一個AdRequest和InterstitialAd實例。 設置非頁內廣告的廣告單元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.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.
最終運行該應用程序。
The banner ad will be displayed automatically when activity is launched but interstitial ad will be displayed on button click.
啟動活動后,橫幅廣告將自動顯示,但點擊按鈕后將顯示插頁式廣告。
屏幕截圖 (Screenshots)
如何顯示測試廣告? (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
總結
以上是生活随笔為你收集整理的Android AdMob教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果邮箱登录入口_电子邮箱的申请及使用说
- 下一篇: spserver