📜  安卓 |适用于 Android Studio 的 AdMob 横幅广告

📅  最后修改于: 2022-05-13 01:57:04.089000             🧑  作者: Mango

安卓 |适用于 Android Studio 的 AdMob 横幅广告

横幅广告是占据应用布局中某个位置的矩形图像或文字广告。如果您不熟悉移动广告,横幅广告是最容易实施的。本文向您展示如何将 AdMob 中的横幅广告集成到 Android 应用中。
例子:

首先,在 Android Studio 中创建一个新项目并添加以下代码以导入 Google Mobile Ads SDK。
项目级 build.gradle文件中,将突出显示的代码添加到allprojects部分。

allprojects
{
    repositories
    {
        google()
        jcenter()    
    }
}

应用级 build.gradle文件中,将突出显示的代码添加到依赖项部分。

dependencies
{
    implementation fileTree(dir
                            : 'libs', include
                            : [ '*.jar' ])
    implementation 'com.android.support:appcompat-v7:26.1.0' 
    implementation 'com.google.android.gms:play-services-ads:19.3.0'
}

AndroidManifest.xml文件中的“ ”标签之间添加 Admob App Id,如下所示。

XML

        


Java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
 
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // Initialize the Mobile Ads SDK
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
             Toast.makeText(this, " sucesfull ", Toast.LENGTH_SHORT).show();
            }
        });
    }
}


XML


 
    
      
  
 


Java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
 
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // Initialize the Mobile Ads SDK
          MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
             Toast.makeText(this, " sucesfull ", Toast.LENGTH_SHORT).show();
            }
        });
 
        AdView mAdView;
          mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
}


XML


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


将以下代码添加到 MainActivity 以初始化 Mobile Ads SDK(只需在应用生命周期中执行一次)。您可以在 AdMob 控制台中找到应用 ID。

Java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
 
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // Initialize the Mobile Ads SDK
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
             Toast.makeText(this, " sucesfull ", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

将突出显示的代码添加到activity_main.xml以显示横幅广告。
activity_main.xml 文件:

XML



 
    
      
  
 

将突出显示的代码添加到Main Activity以显示横幅广告。
主要活动。 Java文件:

Java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
 
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // Initialize the Mobile Ads SDK
          MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
             Toast.makeText(this, " sucesfull ", Toast.LENGTH_SHORT).show();
            }
        });
 
        AdView mAdView;
          mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
}

将 Admob App Id 和 Banner Ad Id 添加到字符串.xml
字符串.xml 文件:

XML



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