我对admob有疑问。 Admob工作得很好但是当我退出游戏时,admob仍在工作。我做错了什么?请帮我。
即使我退出游戏,在logcat中我看到admob仍在工作。
AndroidLuncher.java
public class AndroidLauncher extends AndroidApplication {
private AdView adView;
private static final String AD_UNIT_ID = "xxxxxxxxxxxxxxxxxxxxxxx";
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useWakelock = true;
cfg.useAccelerometer = false;
cfg.useCompass = false;
// Create the layout
RelativeLayout layout = new RelativeLayout(this);
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// Create the libgdx View
View gameView = initializeForView(new MyGameStart(), cfg);
// Add the libGDX view
layout.addView(gameView);
// Create and setup the AdMob view
AdView adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.build();
// Add the AdMob view
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_IN_PARENT);
adView.loadAd(adRequest);
layout.addView(adView, adParams);
// Hook it all up
setContentView(layout);
}
@Override
public void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}
@Override
public void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
@Override
public void onDestroy() {
// Destroy the AdView.
if (adView != null) {
adView.destroy();
}
super.onDestroy();
} }
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.google.android.gms.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="xxxxxxxxxxxxxxxxxxxxxx"
ads:adSize="SMART_BANNER"/>
</LinearLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pl.com.test.testgame.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" />
<!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/GdxTheme" >
<!--This meta-data tag is required to use Google Play Services.-->
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="pl.com.test.testgame.android.AndroidLauncher"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--Include the AdActivity configChanges and theme. -->
<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>
02-13 14:58:55.271: I/Ads(2604): Starting ad request.
02-13 14:58:56.111:I / Ads(2604):从现在起安排广告刷新60000毫秒。 02-13 14:58:56.111:I / Ads(2604):广告已完成加载。
退出游戏
02-13 14:59:56.121:我/广告(2604):开始广告请求。 02-13 14:59:56.931:I / Ads(2604):从现在起安排广告刷新60000毫秒。 02-13 14:59:56.931:我/广告(2604):广告已完成加载。
答案 0 :(得分:0)
创建一个onStop()方法并在那里停止adMob。
答案 1 :(得分:0)
尝试将AdView实例设置为NULL并将其留在那里供GC处理。它可能会抛出NullPointerException,所以请确保你处理它。
答案 2 :(得分:0)
我解决了这个问题。 我有两次Adview广告;
public class AndroidLauncher extends AndroidApplication {
private AdView adView;
@Override
protected void onCreate (Bundle savedInstanceState) {
***
// Create and setup the AdMob view
AdView adView = new AdView(this);
***
当我删除一个时,AdMob工作正常。