简体   繁体   中英

Ads in Android Daydream/DreamService?

Does anyone know if any of the Android Advertising SDKs work with the new DreamService functionality? I tried using AdMob and first saw that the Interstitial class constructor explicitly requires an 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. I looked into trying the Amazon Mobile Ads API, but it appears identical to the AdMob one.

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). Does anyone know of any solution to using Ads in a 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. Their Interstitial and AdView classes can both take a Context rather than an Activity in the constructor. The Interstitial still didn't work out for me since it opens behind the Dream overlay. 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. When it's time to display the ad I set it to 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. 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. 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. In this method I just called finish() to stop the Dream and let the browser display the Ad.

@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();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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