Reputation: 75
I was trying to integrate admob banner in my application several hours, but I couldn't do that, so I created new app the only purpose of which is show admob banner but it doesn't work too. here is code
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AdView adView = new AdView(this, AdSize.BANNER, "XXX");
adView.setVisibility(View.VISIBLE);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layout.addView(adView, adParams);
setContentView(layout);
AdRequest adRequest = new AdRequest();
adView.loadAd(adRequest);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
and here is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zzzzzz_admobtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.zzzzzz_admobtest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
</manifest>
what am I doing wrong?
Also, when I output adView.isActivated() and adView.isReady() they both are false.
I am using "XXX" instead of ID because I don't have one. Could it be a problem?
I am using device. Here is errors on ligcat
08-07 22:43:04.248: E/ActivityThread(24181): Failed to find provider info for com.google.plus.platform 08-07 22:43:04.454: E/Ads(24181): JS: Uncaught ReferenceError: AFMA_getSdkConstants is not defined (http://media.admob.com/:1) 08-07 22:43:04.454: E/Web Console(24181): Uncaught ReferenceError: AFMA_getSdkConstants is not defined at http://media.admob.com/:1
the last 2 errors could be fixed by this code
(new Thread() {
public void run() {
Looper.prepare();
adView.loadAd(new AdRequest());
}
}).start();
as I understand the first one is not an error but just a warning. so why this isn't work??
Upvotes: 4
Views: 10360
Reputation: 11
how to integrate AdMob and Facebook banner ads into Android applications
Build.Gradle :
implementation 'com.google.android.gms:play-services-ads:19.1.0'
implementation 'com.facebook.android:audience-network-sdk:5.+'
MyAplication.java:
AudienceNetworkAds.initialize(this);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
Manifest.xml:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="App_ID_Here.."/> //Test App ID For Testing Purpose.
<activity
android:name="com.facebook.ads.AudienceNetworkActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />
Permission:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
public class MainActivity extends AppCompatActivity {
private LinearLayout bannerView;
//Banner
private AdView facebookAdView;
private com.google.android.gms.ads.AdView admobAdView;
//Interstitial
private InterstitialAd facebookInterstitialAds;
private com.google.android.gms.ads.InterstitialAd adMobInterstitialAds;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bannerView = (LinearLayout) findViewById(R.id.bannerView);
//Load Banner Ads
showFacebookBannerAds();
//Load Interstitial Ads
showFacebookInterstitial();
}
private void showFacebookBannerAds() {
facebookAdView = new AdView(this, "Facebook_Banner_ID", AdSize.BANNER_HEIGHT_50);
bannerView.addView(facebookAdView);
facebookAdView.setAdListener(new AdListener() {
@Override
public void onError(Ad ad, AdError adError) {
//If Load Fail then
showAdMobBanner();
}
@Override
public void onAdLoaded(Ad ad) {
}
@Override
public void onAdClicked(Ad ad) {
}
@Override
public void onLoggingImpression(Ad ad) {
}
});
facebookAdView.loadAd();
}
private void showAdMobBanner() {
admobAdView = new com.google.android.gms.ads.AdView(this);
admobAdView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
admobAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
bannerView.addView(admobAdView);
admobAdView.loadAd(new AdRequest.Builder().build());
}
//Interstitial Ads
private void showFacebookInterstitial() {
facebookInterstitialAds = new InterstitialAd(this, "Interstitial_Ad_ID");
facebookInterstitialAds.setAdListener(new InterstitialAdListener() {
@Override
public void onInterstitialDisplayed(Ad ad) {
}
@Override
public void onInterstitialDismissed(Ad ad) {
//any work on ads close
finish();
}
@Override
public void onError(Ad ad, AdError adError) {
//if load fail then
loadAdMobInterstitial();
}
@Override
public void onAdLoaded(Ad ad) {
}
@Override
public void onAdClicked(Ad ad) {
}
@Override
public void onLoggingImpression(Ad ad) {
}
});
facebookInterstitialAds.loadAd();
}
private void loadAdMobInterstitial() {
adMobInterstitialAds = new com.google.android.gms.ads.InterstitialAd(this);
adMobInterstitialAds.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
adMobInterstitialAds.loadAd(new AdRequest.Builder().build());
adMobInterstitialAds.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
//any work on ads close
finish();
}
});
}
//Interstitial Ads show on button click listener
public void showAds(View view){
if (facebookInterstitialAds.isAdLoaded())
facebookInterstitialAds.show();
else if (adMobInterstitialAds!=null)
if (adMobInterstitialAds.isLoaded())
adMobInterstitialAds.show();
}
@Override
protected void onPause() {
super.onPause();
if (admobAdView != null)
admobAdView.pause();
}
@Override
protected void onResume() {
super.onResume();
if (admobAdView != null)
admobAdView.resume();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (facebookAdView != null)
facebookAdView.destroy();
if (admobAdView != null)
admobAdView.destroy();
if (facebookInterstitialAds != null)
facebookInterstitialAds.destroy();
}
}
Upvotes: 1
Reputation: 11
This question is kinda old, but I want to answer it, because new technology appeared, which lets AdMob integration really easy. You don't need to code anything additionally in most cases, just create and .apk file and Admob will be injected into it automatically.
Actually you can integrate more than 20 SDKs available by just selecting their checkbox. This list includes various ads providers, Crash reporting, Dolby Audio, etc.
But if you just want the banner to be present on your screen all the time, which is the easiest solution, you don't need to change your game code! This solution is called ZeroCode and it works for any development technology: event for the .apk and .ipa, which you've got from an HTML5-game or from Game Maker.
Two last types are marked as ZeroCode. You don't need to code anything in your game to have them integrated. For our sample we'll select just a Persistent banner.
Now upload our app.
On the next screen we can select a banner type. We can use, for example SMART_BANNER or any other banner type from AdMob help. We'll also input the banner id from our AdMob account. When creating a banner in AdMob account, we can set refresh interval.
The final step - signing method selection. You ca use your own developer's certificate, then you'll be able to submit the game to the store right after the AdMob has been injected. You can select a testing certificate to test the app on your own device. Or you can download an unsigned app and sign it locally before submitting to the store.
One more progress bar...
And here we are!
The Enhance service is completely free for the developers. You'll receive the same amount of money from ads as if you'd integrated the ads manually. FGL receives the income from the SDK providers, because, in fact, FGL Enhance makes SDKs more attractive for the new developers.
source: http://www.airapport.com/2016/09/tutorial-how-to-quickly-integrate-admob.html
Upvotes: 1
Reputation: 1220
You can download complete source code from here
Add following lib in your dependency
compile 'com.google.android.gms:play-services-ads:7.8.0'
For Smart Banner
Add following things in xml file
<com.google.android.gms.ads.AdView
android:id="@+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/banner_ad_unit_id" />
Add following things in java file
mAdView = (AdView) findViewById(R.id.ad_view);
AdRequest adRequest = new AdRequest.Builder().build();
// Start loading the ad in the background.
mAdView.loadAd(adRequest);
For Interstitial Advertise
mInterstitialAd = new InterstitialAd(MainActivity.this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.full_screen_ad_unit_id));
AdRequest adRequestFull = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequestFull);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
// Full screen advertise will show only after loading complete
mInterstitialAd.show();
}
});
Upvotes: 0
Reputation: 20196
I am using "XXX" instead of ID because I don't have one. Could it be a problem?
Yes. Absolutely yes. Sign up for an Admob account. Get yourself an ID and use that.
Upvotes: 2
Reputation: 3195
Add Just two line in your onCreate
AdView ad = (AdView) findViewById(R.id.adView);
ad.loadAd(new AdRequest());
And add AddView in Your XML like
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/ads_unit_id"
ads:loadAdsOnCreate="true" />
Upvotes: 0