横幅在多线程Android应用中无法正常工作

时间:2014-03-07 19:06:14

标签: java android multithreading admob banner

我的横幅在我的应用程序中工作时遇到了问题。我在SO里面做了很多搜索,却找不到任何有帮助我的东西。该应用程序是一个使用Canvas绘制图形的视频游戏。有一个while循环在一个连续运行的线程中调用draw()updateStateOfTheGame()。要绘制横幅,我使用WindowManager。以下是显示横幅的活动代码:

public class MainActivity extends Activity { 

    private AdView adView;  
    private HomeMainGamePanel hMGP; // This class extends surfaceview and implements callback and does the drawing of canvas, bitmaps and other stuff in the game.
    private static final String AD_UNIT_ID = "xxxxxxx";

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        // making it full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        hMGP=new HomeMainGamePanel(this, display,this);

        // Create an ad.
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);

        // window manager preparation 
        WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams();
        windowParams.gravity = Gravity.BOTTOM;
        windowParams.x = 0;
        windowParams.y = 0;
        windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        windowParams.width = WindowManager.LayoutParams.FILL_PARENT;
        windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
          | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
          | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        windowParams.format = PixelFormat.TRANSLUCENT;
        windowParams.windowAnimations = 0;

        WindowManager wm = getWindowManager();

        // Create an ad request. Check logcat output for the hashed device ID to
        // get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
            .build();

        wm.addView(hMGP,windowParams);
        wm.addView(adView,windowParams);
        adView.loadAd(adRequest);
    }    

    @Override
    public void onResume() {
        super.onResume();
        if (adView != null) {
            adView.resume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (adView != null) {
            adView.pause();
        }
        boolean retry = true;
        hMGP.getHomeMainThread().setRunning(false);
        while (retry) {
            try {
                hMGP.getHomeMainThread().join();
                retry = false;
            } catch (InterruptedException e) {
               // we will try it again and again...
            }
        }
    }

    @Override
    protected void onDestroy() {
        // Destroy the AdView.
        if (adView != null) {
            final WindowManager wm = getWindowManager();
            wm.removeView(hMGP);
            adView.destroy();
        }
        super.onDestroy();
    }

好的是,如果我从代码中移除wm.addView(hMGP,windowParams);,广告效果很好。在显示屏上我们只会看到横幅,如果您点击它,它会将您链接到正确的网站。如果我保留那行代码(这是我的任务,因为该行是显示整个游戏图形的行)当我点击横幅它不起作用,或者更好它接收我的点击但无法打开网站, logcat说Could not get info for ads overlay,这里是:

03-07 19:52:02.159: I/Ads(16984): Scheduling ad refresh 60000 milliseconds from now.
03-07 19:52:02.159: I/Ads(16984): Ad finished loading.
03-07 19:52:02.189: I/chromium(16984): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
03-07 19:52:02.229: I/chromium(16984): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
03-07 19:52:02.229: E/qdutils(16984): FBIOGET_FSCREENINFO failed
03-07 19:52:08.325: D/HomeMainThread(16984): Game loop executed 658 times
03-07 19:52:10.468: W/Ads(17151): Could not get info for ad overlay.

我的节目中也有这个:

-keep class com.google.android.gms.ads.**
-dontwarn com.google.android.gms.ads.**
-keep public class com.google.gson.**

-keep public class com.google.gson.**
-keep public class com.google.android.gms.ads.** {
    public *;
}

0 个答案:

没有答案