java - Libgdx。当我退出游戏时,admob 仍在工作

标签 java android libgdx admob

我在使用 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:I/Ads(2604):正在启动广告请求。 02-13 14:59:56.931:I/Ads(2604):安排从现在起 60000 毫秒后刷新广告。 02-13 14:59:56.931: I/Ads(2604): 广告加载完毕。

最佳答案

创建一个 onStop() 方法并在那里停止 adMob。

关于java - Libgdx。当我退出游戏时,admob 仍在工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28486666/

相关文章:

java - Android保存到外部SD卡?

opengl-es - LibGDX重复TextureRegion

java - android 如何按名称对图像进行排序

android - 应用程序终止时从 FCM 启动 Activity ?

android - 突出显示 TextView 中的文本

java - 如何使我的游戏的用户界面元素适应 libgdx 中不同的移动设备屏幕

android - Android SDK中的 "tools.jar"到底是什么?

java - 域模型到 JSON - 深度序列化的问题

java - JButton 保持按下状态

java - 计算 sin(x) w/oMath 并仅在 java 中使用循环