Issue with Phonegap and MillennialMedia

蓝咒 提交于 2019-12-13 06:37:12

问题


I'm trying to use MillenialMedia on my Phonegap app, but i just can't make it work. I get the following errors on the LogCat:

08-12 12:52:36.755: I/MillennialMediaSDK(333): Initializing MMLayout.
08-12 12:52:37.385: W/MillennialMediaSDK(333): MMLayout adding view (MMWebView originally from(1) MRaidState(loading).) to AdType[(b) InternalId(1) LinkedId(0) isFinishing(false)]
08-12 12:52:37.645: W/MillennialMediaSDK(333): AdView onLayout changedtrue int left 0 int top 687 int right 480 int bottom 762
08-12 12:52:37.685: W/MillennialMediaSDK(333): Id check for parent: 1 versus 1
08-12 12:52:38.355: I/MillennialMediaSDK(333): Millennial ad return failed. Zero content length returned.
08-12 12:52:38.965: E/MillennialMediaSDK(333): Could not get a handshake. Not trusted server certificate

Here's my code:

/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
 */

package com.logicstudio.paradise.Replay;

import android.os.Bundle;
import org.apache.cordova.*;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class Replay extends DroidGap
{
    //Constants for tablet sized ads (728x90)
    private static final int IAB_LEADERBOARD_WIDTH = 728;
    private static final int IAB_LEADERBOARD_HEIGHT = 90;

    private static final int MED_BANNER_WIDTH = 480;
    private static final int MED_BANNER_HEIGHT = 60;

    //Constants for phone sized ads (320x50)
    private static final int BANNER_AD_WIDTH = 320;
    private static final int BANNER_AD_HEIGHT = 50;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        super.setIntegerProperty("splashscreen", R.drawable.splash);
        super.loadUrl(Config.getStartUrl());

        int placementWidth = BANNER_AD_WIDTH;
        int placementHeight = BANNER_AD_HEIGHT;

        //Finds an ad that best fits a users device.
        if(canFit(IAB_LEADERBOARD_WIDTH)) {
            placementWidth = IAB_LEADERBOARD_WIDTH;
            placementHeight = IAB_LEADERBOARD_HEIGHT;
        }
        else if(canFit(MED_BANNER_WIDTH)) {
            placementWidth = MED_BANNER_WIDTH;
            placementHeight = MED_BANNER_HEIGHT;
        }


        com.millennialmedia.android.MMSDK.initialize(this);

        com.millennialmedia.android.MMAdView adView = new com.millennialmedia.android.MMAdView(this);

        LinearLayout layout = super.root;

        adView.setApid("131468");

        //Set your metadata in the MMRequest object
        com.millennialmedia.android.MMRequest request = new com.millennialmedia.android.MMRequest();

        //Add the MMRequest object to your MMAdView.
        adView.setMMRequest(request);

        //Sets the id to preserve your ad on configuration changes.
        adView.setId(com.millennialmedia.android.MMSDK.getDefaultAdId());

        //Set the ad size. Replace the width and height values if needed.
        adView.setWidth(placementWidth);
        adView.setHeight(placementHeight);

        //Calculate the size of the adView based on the ad size. Replace the width and height values if needed.
        int layoutWidth = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, placementWidth, getResources().getDisplayMetrics());
        int layoutHeight = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, placementHeight, getResources().getDisplayMetrics());

        //Create the layout parameters using the calculated adView width and height.
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(layoutWidth, layoutHeight);

        //This positions the banner.
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

        adView.setLayoutParams(layoutParams);

        //Add the adView to the layout. The layout is assumed to be a RelativeLayout.
        layout.addView(adView);

        adView.getAd();
    }

    protected boolean canFit(int adWidth) {
        int adWidthPx = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, adWidth, getResources().getDisplayMetrics());
        DisplayMetrics metrics = this.getResources().getDisplayMetrics();
        return metrics.widthPixels >= adWidthPx;
    }
}

Any help will be greatly appreciated. Thanks in advance!


回答1:


PhoneGap works by throwing HTML/CSS/JS into a special WebView (CordovaWebView), which is running on the entire screen. The code that controls this default behavior lives inside their DroidApp class.

To get Millennial ads to work (or any standard Android controls, for that matter), you have to create a class that extends android.app.Activity (rather than DroidApp) and embed your CordovaWebView within that activity. That CordovaWebView will handle all of your app's Phonegap-related assets while your activity and its corresponding layout can handle your Android-specific classes (like Millennial's MMAdView).

To get a deeper understanding of how PhoneGap works on Android, I strongly recommend reading their docs here: http://cordova.apache.org/docs/en/2.7.0/guide_cordova-webview_android.md.html

To get more on how to specifically get Millennial's SDK working in that environment, talk to the support folks here: https://tools.mmedia.com/user/supportDevPortal



来源:https://stackoverflow.com/questions/18187681/issue-with-phonegap-and-millennialmedia

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!