2016-09-24 71 views
2

我正在尝试将AdMob集成到我的应用程序中,并且我已经成功检查了AdMob示例,并在我的Android设备上添加了该功能,但是当我尝试为我的应用程序执行相同的代码时,Android,Admob

我的代码

private void startGame() { 
     // Request a new ad if one isn't already loaded, hide the button, and kick off the timer. 
     if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) { 

runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
     AdRequest adRequest = new AdRequest.Builder() 
       .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
       .addTestDevice("0C3BF98F2CA55F66D0BE2D63D962BF6D") 
       .build(); 
     mInterstitialAd.loadAd(adRequest); 
    } 
}); 

     } 


    } 
    InterstitialAd mInterstitialAd; 

    void StartAds() 
    { 
     MobileAds.initialize(this, "ca-app-pub-3940256099942544~1033173712"); 
     mInterstitialAd = new InterstitialAd(this); 
     // Defined in res/values/strings.xml 
     mInterstitialAd.setAdUnitId(getString(R.string.google_home_ad_unit_id)); 

     mInterstitialAd.setAdListener(new AdListener() { 
      @Override 
      public void onAdClosed() { 

      } 
     }); 


     if (mInterstitialAd != null && mInterstitialAd.isLoaded()) { 
      mInterstitialAd.show(); 
     } else { 

      startGame(); 
     } 

    } 

清单文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="ads.example"> 

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



    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

     <meta-data android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version"/> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name="com.google.android.gms.ads.AdActivity" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
      android:theme="@android:style/Theme.Translucent" /> 

    </application> 

</manifest> 

gradle这个

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.1" 
defaultConfig { 
    applicationId "example.ads.app" 
    minSdkVersion 15 
    targetSdkVersion 23 
    versionCode 1 
    versionName "1.0" 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'com.android.support:design:23.1.1' 
    compile 'com.google.firebase:firebase-ads:9.0.0' 
} 

apply plugin: 'com.google.gms.google-services' 

/

/Top-level build file where you can add configuration options common to all sub-projects/modules. 

buildscript { 
    repositories { 
     jcenter() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:2.1.2' 
     classpath 'com.google.gms:google-services:3.0.0' 
     // NOTE: Do not place your application dependencies here; they belong 
     // in the individual module build.gradle files 
    } 
} 

allprojects { 
    repositories { 
     jcenter() 
    } 
} 

task clean(type: Delete) { 
    delete rootProject.buildDir 
} 

回答

4

横幅广告

Java类

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

XML

<com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerInParent="true" 
     ads:adSize="BANNER" 
     ads:adUnitId="@string/adUnitID"> 

//依赖

dependencies{ 
     compile 'com.google.android.gms:play-services:9.2.0' 
    } 

//字符串

<string name="adUnitID">your add mob UnitID</string> 
<string name="interstial_id">your add mob UnitID</string> 

//体现

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

对于插页式广告

Java类

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
      interstitial = new InterstitialAd(INTERSTIAL_ads_Activity.this); 
      interstitial.setAdUnitId(getResources().getString(R.string.interstial_id); 
    AdRequest adRequest = new AdRequest.Builder().build(); 
      interstitial.loadAd(adRequest); 
      interstitial.setAdListener(new AdListener() { 
       public void onAdLoaded() { 
        // Call displayInterstitial() function 
        displayInterstitial(); 
       } 
      }); 

private void displayInterstitial(){ 
      if (interstitial.isLoaded()) { 
        interstitial.show(); 
       } 
}