0

我正在尝试创建自己的(迷你)广告网络来交叉推广我们自己的应用程序。我不想使用 admob 或任何其他 SDK,因为我的印象是它们不再适用于 3.0 以下的 Android SDK,该应用程序必须在 Android 2.3 上运行

解释完后,我写了一个html页面并上传到我们的服务器(摘自网络):

<html>
<head>
<script src="banners.js">
<style>
html {
    position: relative;
    min-width: 320px;
    min-height: 50px;
    height: 100%;
}
</style>
</script>
</head>
<body style='margin:0;padding:0;' onLoad = "javascript:rotateBanner();javascript:setInterval('rotateBanner()',20000);">
<a href= "http://www.bigfoot.com/~java4free" onMouseOver = "javascript:pause()" onMouseOut = "javascript:resume()">
<img style="margin-left:auto;margin-right:auto" src = "first1.jpg"></a>
</body>
</html>

用于此的 javascript 将是:

<!-- //Hide from non-javascript browsers

//Author: Mark Ganson
//Date: 3/10/2000
//Site: http://www.bigfoot.com/~java4free

var nCurrentBanner = 0;
var bPause = false;

//replace these links with the appropriate href portion of your banner code
//example: <a href="java4free.html"><img src="java4freebanner.jpg></a>
//in the above example, "java4free.html" is the link string
//and "java4freebanner.jpg" is the image string

//href parts
strLinks = new Array (

"http://www.commission-junction.com/track/track.dll?AID=10347&PID=296657&URL=http%3A%2F%2Fwww%2Ecj%2Ecom%2FAffiliate%2Findex%2Easp",

"http://www.bigfoot.com/~java4free.html"

);

//img parts
strImages = new Array (

"http://myserver.co.uk/banners/first1.jpg",
"http://myserver.co.uk/banners/first2.png"

);

//status bar messages
//comment out this block if you don't want status bar messages
strMessages = new Array(

"Commission-Junction.  Get paid!",
"Java4Free!  Free webtools for webmasters!"

);

function rotateBanner(){

    if (bPause){
        return;
    }

    //following code assumes that the banner will be the first link/image
    //in your html page.  If this is not correct, you will need to modify
    //the value in between the [] accordingly.  For example, if you have
    //2 banners appearing before this banner in your html, you would replace
    //the [0] with [2] in the lines below.

    document.links[0].href = strLinks[nCurrentBanner];
    document.images[0].src = strImages[nCurrentBanner];

    //comment out the following line if you don't want status bar messages
    window.status = strMessages[nCurrentBanner];

    //now rotate for next iteration
    if (nCurrentBanner < strLinks.length-1){
        nCurrentBanner++;
    } else {
        nCurrentBanner = 0;
    }
}

function pause(){
    bPause=true;
}

function resume(){
    bPause=false;
}

然后我通过 XML 创建了一个 webview

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

然后我将此代码插入到我想要展示广告的活动的 onCreate 方法中

    WebView wv = (WebView) findViewById(R.id.webview);
    WebSettings webSettings = wv.getSettings();
    webSettings.setJavaScriptEnabled(true);
    wv.getSettings().setLoadWithOverviewMode(true);
    wv.getSettings().setUseWideViewPort(true);
    wv.setBackgroundColor(Color.TRANSPARENT);
    wv.loadUrl("http://myserver.co.uk/banners/banner.html");

问题是:

  1. 上面的 javaScript 对这种事情足够安全吗?我以前曾经使用过 document.write 方法,但一段时间后网站被黑了,现在不确定这个时间。
  2. 这可以很好地显示广告,但我猜是因为图像位于封闭的 html 页面中,当我调用时

    wv.getSettings().setLoadWithOverviewMode(true);

视口将其大小调整为非常小,并且 320x50 图像不会保持不变。

请帮忙。

4

0 回答 0