Admob ads showing in emulator but not in real device

馋奶兔 提交于 2020-01-16 03:25:09

问题


I have integrated admob in my app but the ads showed up in emulator but when i tried in real device it's not showing up. For integration i have added below code in AndroidManifest.xml

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

Then in my activity's layout i have added below Linearlayout to load the ad in it :

<LinearLayout
    android:id="@+id/main_menu_banner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:gravity="center"
    android:orientation="horizontal" />

Then in activity i have written below java code for integration :

/* The view to show the ad. */
private AdView adView;
/* Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID = "My_admob_key";
// Create an ad.
adView = new AdView(MainMenu.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.main_menu_banner);
layout.addView(adView);
// Initiate a generic request.
AdRequest adRequest = new AdRequest.Builder().build();
// Load the adView with the ad request.
adView.loadAd(adRequest);
layout.bringToFront();

Any help would be appreciated why ads are not showing up!!!! Also you can point to any good tuto for integrating admob will also help. Thanks in advance


回答1:


Add the extras as well

 AdMobExtras extras = new AdMobExtras(bundle);
 AdRequest adRequest = new AdRequest.Builder()
 .addNetworkExtras(extras).build();
 adView.loadAd(adRequest);



回答2:


May be you missed adding Test device or added wrong ID, same thing happened with me as till I noticed in the Logcat this alert;

04-22 10:04:04.887 I/Ads: Use AdRequest.Builder.addTestDevice("6612F2AEECD2A4ADBD699CFC349AB01A") to get test ads on this device.

Once I added it, it started working fine.




回答3:


In my case there were an issue with the size of the view. So I debugged the app in Android Studio and found the right hint in the Android Monitor Tab:

 Not enough space to show ad. Needs 320x150 dp, but only has 318x0 dp.


来源:https://stackoverflow.com/questions/22192755/admob-ads-showing-in-emulator-but-not-in-real-device

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