使用iOS RoboVM/libgdx如何检测AdMob插页式广告的关闭?

5
我正在使用RoboVM绑定来显示AdMob插页式广告的iOS应用程序。当我关闭插页式广告时,我失去了所有的触摸控制。有没有办法检测到广告被关闭,这样我就可以把触摸放回游戏中?还是有更好的方法来实现插页式广告?下面是我的代码:
public class IOSLauncher extends IOSApplication.Delegate implements IActivityRequestHandler{
private static final Logger log = new Logger(IOSLauncher.class.getName(), Application.LOG_DEBUG);
private IOSApplication iosApplication;

//interstitial
private static final String INTERSTITIAL_AD = "MY_AD_ID";
private GADInterstitial interstitial;
private UIWindow window;
private UIViewController rootViewController;

@Override
protected IOSApplication createApplication() {
    IOSApplicationConfiguration config = new IOSApplicationConfiguration();
    config.orientationLandscape = true;
    config.orientationPortrait = false;

    iosApplication = new IOSApplication(new PaperPig(this), config);
    return iosApplication;
}

public static void main(String[] argv) {
    NSAutoreleasePool pool = new NSAutoreleasePool();
    UIApplication.main(argv, null, IOSLauncher.class);
    pool.close();
}

@Override
public void initializeAds() {
    intializeInterstitial();
}

public void intializeInterstitial () {
    rootViewController = new UIViewController();

    interstitial = new GADInterstitial();
    interstitial.setAdUnitID(INTERSTITIAL_AD);

    interstitial.setDelegate(new GADInterstitialDelegateAdapter() {
        @Override
        public void didReceiveAd (GADInterstitial ad) {
            System.out.println("Did receive ad.");
        }

        @Override
        public void didFailToReceiveAd (GADInterstitial ad, GADRequestError error) {
            System.out.println(error.description());
            System.out.println(error.getErrorCode());
        }
    });

    window = new UIWindow(UIScreen.getMainScreen().getBounds());
    window.setRootViewController(rootViewController);
    window.addSubview(rootViewController.getView());

    interstitial.loadRequest(GADRequest.create());
}

@Override
public void showOrLoadInterstital() {
    if (interstitial.isReady()) {
        if (rootViewController == null) {
            rootViewController = new UIViewController();
        }
        if (window == null) {
            window = new UIWindow(UIScreen.getMainScreen().getBounds());
            window.setRootViewController(rootViewController);
        }
        window.makeKeyAndVisible();
        interstitial.present(rootViewController);
    }

将触摸返回给游戏
UIApplication.getSharedApplication().getKeyWindow().setRootViewController(rootViewController);}

注:该文本是代码片段,翻译后并不能直接使用。

你可以通过重写 GADInterstitialDelegateAdapter 中的 didDismissScreen 方法来检测关闭。但是在那之后要做什么,我也卡住了。如果我解决了,我会告诉你的。 - Will Calderwood
1个回答

6

您需要调用:

        window.setHidden(true);

将创建GADInterstitialDelegateAdapter()的代码更改为以下内容。
   interstitial.setDelegate(new GADInterstitialDelegateAdapter() {
        @Override
        public void didReceiveAd (GADInterstitial ad) {
            System.out.println("Did receive ad.");

        }

        @Override
        public void didDismissScreen(GADInterstitial ad) {
             window.setHidden(true);
        }

        @Override
        public void didFailToReceiveAd (GADInterstitial ad, GADRequestError error) {
            System.out.println(error.description());
            System.out.println(error.getErrorCode());
        }
    });

感谢您的支持,当我尝试调用另一个插页式广告时,触摸返回游戏后,我收到了以下错误信息:2014-10-24 01:25:24.938 IOSLauncher[7680:2519837] <Google> Request Error: Will not send request because interstitial object has been used. Error Domain=com.google.ads Code=6 "Request Error: Will not send request because interstitial object has been used." UserInfo=0x23e02490 {NSLocalizedDescription=Request Error: Will not send request because interstitial object has been used., NSLocalizedFailureReason=Request Error: Will not send request because interstitial object has been used.} - islander_zero
这实际上应该是一个不同的问题。检查interstitial.isBeenUsed()。如果返回true,则重新创建interstitial实例并再次设置广告单元ID。如果这解决了您的问题,请接受答案。谢谢。 - Will Calderwood
理想情况下,您希望在 didDismissScreen 中加载广告的新实例,以便在下次需要时准备好。 - Will Calderwood
你说得对,@Will CalderWood。现在它完美地运作了。我只是在 didDimissScreen() 中调用了我的整个 intializeInterstitial()。希望这不会成为问题,但它正常工作了...再次感谢! - islander_zero

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接