Phonegap和MillennialMedia的问题

时间:2013-08-12 13:01:35

标签: android cordova millennial-media

我正在尝试在我的Phonegap应用程序上使用MillenialMedia,但我无法使其正常工作。 我在LogCat上遇到以下错误:

08-12 12:52:36.755:I / MillennialMediaSDK(333):初始化MMLayout。
08-12 12:52:37.385:W / MillennialMediaSDK(333):MMLayout添加视图(MMWebView最初来自(1)MRaidState(加载)。)到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检查父母:1对1 08-12 12:52:38.355:I / MillennialMediaSDK(333):千禧年广告回归失败。返回零内容长度 08-12 12:52:38.965:E / MillennialMediaSDK(333):无法握手。不受信任的服务器证书

这是我的代码:

/*
       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;
    }
}

任何帮助将不胜感激。 提前谢谢!

1 个答案:

答案 0 :(得分:1)

PhoneGap将HTML / CSS / JS投入到一个特殊的WebView(CordovaWebView)中,该视图在整个屏幕上运行。控制此默认行为的代码存在于其DroidApp类中。

要让Millennial广告正常工作(或者任何标准的Android控件),你必须创建一个扩展android.app.Activity(而不是DroidApp)的类,并将你的CordovaWebView嵌入到该活动中。 CordovaWebView将处理您的所有应用程序的Phonegap相关资产,而您的活动及其相应的布局可以处理您的Android特定类(如Millennial的MMAdView)。

为了更深入地了解PhoneGap如何在Android上运行,我强烈建议您在此处阅读他们的文档:http://cordova.apache.org/docs/en/2.7.0/guide_cordova-webview_android.md.html

要了解如何专门获取Millennial SDK在该环境中工作的更多信息,请与支持人员联系:https://tools.mmedia.com/user/supportDevPortal