簡體   English   中英

Android中的AdMob集成

[英]AdMob integration in Android

我正在我的應用程序中實施AdMob。 我添加了xml代碼。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="350dp"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-2415896980984028/6183643605" >
    </com.google.android.gms.ads.AdView>

</LinearLayout>

我已在我的activity_main.xml中添加了此代碼。.我也已在我的MAinActivity類中添加了此代碼。 但是,當我在設備中安裝該應用程序進行測試時,它崩潰了。

    adView=(AdView) this.findViewById(R.id.adView);
    adView.setVisibility(AdView.VISIBLE);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

我無法弄清楚到底是什么問題。

您是否在manifest.xml中添加了此內容?

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<activity android:name="com.google.ads.AdActivity"
 android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

並添加了lib嗎?

您必須在Activity中編寫代碼

  private AdView adView;
    private static final String AD_UNIT_ID = "INSERT_YOUR_AD_UNIT_ID_HERE";
    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
    layout.addView(adView);

    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
        .build();

    // Start loading the ad in the background.
    adView.loadAd(adRequest);

必須在Menifest中廣告此行。

  <activity android:name="com.google.android.gms.ads.AdActivity"
   android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
   <meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

由於您是通過Google Play服務庫使用admob的,因此請添加/進行以下更改:

在您的布局中,從以下位置更改廣告xml名稱空間:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

   -----/>

至 :

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

   ----/>

然后在清單的應用程序標記中,更改:

<activity android:name="com.google.ads.AdActivity"/>

至:

<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

加:

<meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>

不要忘記權限:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM