我尝试使用Google Play Services API将AdMob广告插入我的应用程序 无论如何,他们没有露面。
我希望有人能帮助我
我有2个Activity(MainActivity.java和AdMobAdd.java)
继承人AdMobAdd.java(如果有人需要MainActivity,请告诉我)
(我使用了这里的例子http://kyanogen.com/admob-interstitial-ads/)
package com.myapp.helpme:D;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.rajatbsosz.timbermancheat.R;
public class AdMobAdd extends Activity {
/**
* Your ad unit id, you must replace it with your actual ad unit id
* Which you can generate from Admob website
*
*/
private static final String AD_UNIT_ID = "ca-app-pub-99******************";
private static final String TAG = "AdMobAdd";
private InterstitialAd iAd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iAd = new InterstitialAd(this);
iAd.setAdUnitId(AD_UNIT_ID);
iAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Log.d(TAG, "onAdLoaded");
Toast.makeText(AdMobAdd.this, "Ad loaded successfully", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdFailedToLoad(int errorCode) {
String errorMessage = String.format("Failed to load add : "+ getErrorReason(errorCode));
Log.d(TAG, errorMessage);
Toast.makeText(AdMobAdd.this, errorMessage, Toast.LENGTH_SHORT).show();
}
});
loadInterstitial();
}
public void loadInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("You can add you device id here, run code once and get id from logs")
.build();
iAd.loadAd(adRequest);
}
public void showInterstitial() {
if (iAd.isLoaded()) {
iAd.show();
} else {
Log.d(TAG, "Interstitial ad is not loaded yet");
}
}
/**
* Gets a string error reason from an error code
*
* @param errorCode
* @return
*/
private String getErrorReason(int errorCode) {
String errorReason = "unknown error";
switch(errorCode) {
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
errorReason = "internal error";
break;
case AdRequest.ERROR_CODE_INVALID_REQUEST:
errorReason = "invalid request";
break;
case AdRequest.ERROR_CODE_NETWORK_ERROR:
errorReason = "network Error";
break;
case AdRequest.ERROR_CODE_NO_FILL:
errorReason = "no fill";
break;
}
return errorReason;
}
@Override
protected void onDestroy() {
showInterstitial();
super.onDestroy();
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.***.***"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<!-- Used to request banner and interstitial ads. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Used to avoid sending an ad request if there is no connectivity. -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Required permissions for video ads. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
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" >
<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|s mallestScreenSize"/>
</application>
</manifest>
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/d3"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.example.MainActivity" >
<ScrollView
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:fillViewport="true" >
<RelativeLayout
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
about 10 TextViews and Buttons...
...
</RelativeLayout>
</ScrollView>
</RelativeLayout>
答案 0 :(得分:1)
有两种类型的横幅,这是我的例子
public class MainActivity extends SherlockActivity {
private Button btnList;
private AdView adView;
LinearLayout layout;
private InterstitialAd interstitial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (LinearLayout) findViewById(R.id.banner);
addBanner();
// Create the interstitial or big banner
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(Utils.idBANNERBIG);
// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Log.d("GAME", "onAdLoaded");
displayInterstitial();
}
@Override
public void onAdFailedToLoad(int errorCode) {
Log.d("GAME", errorCode + "");
}
});
}
// Invoke displayInterstitial() when you are ready to display an
// interstitial.
private void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
@Override
public void onPause() {
adView.pause();
super.onPause();
}
@Override
public void onResume() {
super.onResume();
adView.resume();
}
@Override
public void onDestroy() {
adView.destroy();
super.onDestroy();
}
private void addBanner() { //small banner
adView = new AdView(this);
adView.setAdUnitId(Utils.idBANNER);
adView.setAdSize(AdSize.SMART_BANNER);
layout.addView(adView);
// Iniciar una solicitud gen�rica.
AdRequest adRequest = new AdRequest.Builder().addTestDevice(
AdRequest.DEVICE_ID_EMULATOR).build();
// Cargar adView con la solicitud de anuncio.
adView.loadAd(adRequest);
}
}
答案 1 :(得分:0)
不要在showInterstitial()
中致电onDestroy()
。
无法保证会被调用,并且在任何情况下,您的活动在此时已经处于部分解构状态且插页式广告可能没有时间触发。
相反,您应该在应用中的自然断点处调用showInterstitial()
。
注意:您可以通过添加按钮来测试您的插页式广告是否正常工作,该按钮会在您按下时显示插页式广告。请记得在显示接收新的插页式广告后致电interstitial.load()
。