简体   繁体   English

Android Daydream / DreamService中的广告?

[英]Ads in Android Daydream/DreamService?

Does anyone know if any of the Android Advertising SDKs work with the new DreamService functionality? 有没有人知道任何Android广告SDK是否适用于新的DreamService功能? I tried using AdMob and first saw that the Interstitial class constructor explicitly requires an Activity. 我尝试使用AdMob,并首先看到Interstitial类构造函数显式需要一个Activity。 I saw the the AdView has a constructor that just needs a context so I tried that, but got a runtime exception telling me the problem was that I'm trying to inflate an AdView using a Context other than Activity. 我看到AdView有一个构造函数,只需要一个上下文,所以我尝试了,但得到一个运行时异常告诉我问题是我正在尝试使用除Activity之外的上下文来扩充AdView。 I looked into trying the Amazon Mobile Ads API, but it appears identical to the AdMob one. 我查看了尝试亚马逊移动广告API,但它看起来与AdMob相同。

I tried to get creative and start another Activity from my DreamService that creates an Interstitial ad, but it was created behind the DreamService UI (kinda makes sense since the Daydream overlays everything). 我试图发挥创意,从我的DreamService开始创建插页式广告的另一个活动,但它是在DreamService用户界面后创建的(有点理智,因为Daydream覆盖了所有内容)。 Does anyone know of any solution to using Ads in a Daydream? 有没有人知道在Daydream中使用广告的任何解决方案?

I came up with something that solves this issue, though I still don't really like the solution. 我想出了解决这个问题的东西,尽管我仍然不喜欢这个解决方案。 Would welcome a more elegant approach if anyone knows of one. 如果有人知道的话,欢迎采用更优雅的方法。

What I did was use the mMedia SDK instead of AdMob. 我所做的是使用mMedia SDK而不是AdMob。 Their Interstitial and AdView classes can both take a Context rather than an Activity in the constructor. 他们的非页内广告和AdView类都可以在构造函数中使用Context而不是Activity。 The Interstitial still didn't work out for me since it opens behind the Dream overlay. 由于它在Dream叠加层后面打开,因此插页式广告仍然无法解决。 So what I ended up doing was adding an AdView to my Dream's XML layout, then setting its visibility to View.GONE until I wanted to display it. 所以我最终做的是在我的Dream的XML布局中添加AdView,然后将其可见性设置为View.GONE,直到我想要显示它。 When it's time to display the ad I set it to View.VISIBLE. 当我需要显示广告时,我将其设置为View.VISIBLE。

The other issue I encountered was that after clicking the AdView it launches the browser with the ad's URL, which of course opens behind the Dream, defeating the purpose of showing an ad. 我遇到的另一个问题是,在点击AdView后,它会使用广告的网址启动浏览器,这当然会在Dream背后打开,从而无法展示广告。 So I ended up setting the Dream to be interactive, caught the onTouchEvent, then if the Ad is VISIBLE when the click happens call the Ad's callOnClick method. 所以我最终将Dream设置为交互式,捕获onTouchEvent,然后如果点击发生时广告是可见的,请调用Ad的callOnClick方法。 I also had to set the Ad's RequestListener to my Dream Service and implement the MMAdOverlayLaunched method, which is called when the Ad launches the browser. 我还必须将Ad的RequestListener设置为我的Dream Service并实现MMAdOverlayLaunched方法,该方法在广告启动浏览器时调用。 In this method I just called finish() to stop the Dream and let the browser display the Ad. 在这个方法中,我只是调用finish()来停止Dream并让浏览器显示广告。

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    // Exit dream upon user touch
    setInteractive(true);
    // Hide system UI
    setFullscreen(true);
    // Set the dream layout
    setContentView(R.layout.dream_layout);
    //Initialize Ads
    this.initAdvertising();
}

private void initAdvertising(){
     MMSDK.initialize(this);
     mDreamAd = (MMAdView) findViewById(R.id.adView);
     //Separate thread will handle showing the ad
     mDreamAd.setVisibility(View.GONE);
     mAdRequest = new MMRequest();
     //TODO add metadata to Request
     mDreamAd.setMMRequest(mAdRequest);
     mDreamAd.setListener(this);
     mDreamAd.getAd();
}

@Override
public boolean dispatchTouchEvent(MotionEvent event){
    super.dispatchTouchEvent(event);
    if(mDreamAd != null && mDreamAd.isShown()){
        mDreamAd.callOnClick();
    }
    return true;
}

@Override
public void MMAdOverlayLaunched(MMAd ad) {
    //Finish so we can display the ad the user has clicked
    if(ad.equals(this.mDreamAd))
        this.finish();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM