Android:通过webview提供自己的广告

时间:2013-07-25 13:12:58

标签: javascript android html

我正在尝试创建自己的(迷你)广告网络来交叉推广我们自己的应用。我不想使用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(真);

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

    请帮忙。

0 个答案:

没有答案
相关问题