Google Admob Android: Working on only one device

只愿长相守 提交于 2021-02-10 07:36:56

问题


I have set up an admob adview in my android app: The manifest:

  <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-123456567787889990">

My xml:

   <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-0987784576567456234511"
        ads:layout_constraintBottom_toBottomOf="parent"
        ads:layout_constraintEnd_toEndOf="parent"
        ads:layout_constraintStart_toStartOf="parent">
    </com.google.android.gms.ads.AdView>

In my java code I have done the following:

 MobileAds.initialize(this, "ca-app-pub-123456567787889990");
        AdRequest adRequest = null;
        if (BuildConfig.DEBUG) {
            adRequest = new AdRequest.Builder().addTestDevice("EDDADA7CC97DD3A4AAD9123312312321").build();
        } else {
            adRequest = new AdRequest.Builder().build();
        }
        adView.loadAd(adRequest);

I understand that real ads are only to be used in production and I have to use test ads. However the test ads appear only on the one device(the device that I am testing on), its working properly there. However it does not appear on any other device. I sent an apk to my client and the ad-space always appears blank with no test-ad.

Is this the intented behavior or am I missing something?? Please help.


回答1:


You have added only one test device for adRequest() in debug mode. So it's working for that device only. First of all, you don't need to test with a live account with real AdMob unit id, you can use test ad unit id. And don't need to check if it is in debug mode or not.

You can follow this way to integrate AdMob test banner ads.

  1. Add this in project-level build.gradle
    allprojects {
        repositories {
            google()
            jcenter()
        }
    }
  1. Add this app-level build.gradle
    dependencies {

        implementation 'com.google.android.gms:play-services-ads:17.1.1'
    }
  1. Then add this in Manifest.xml file inside application tag

    <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="[ADMOB_APP_ID]"/>
    

4.Then In you layout xml file

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
  1. Then in your activity do this
package ...

import ...
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {
    private AdView mAdView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(this,
            "ca-app-pub-3940256099942544~3347511713");

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
}

Then it will work perfectly for all devices. After that you have to just replace your admob app id and banner unit id. Hope it will help you.



来源:https://stackoverflow.com/questions/54955274/google-admob-android-working-on-only-one-device

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!