javascript - Android: Delivering your own ads through webview -
i trying create own sort of (mini) ad network cross-promote our own apps. not want use admob or other sdk because under impression not work more under 3.0 android sdks, app must run on android 2.3
after explaining this, wrote html page , uploaded our server (snippets taken web):
<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>
the javascript be:
<!-- //hide 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 appropriate href portion of banner code //example: <a href="java4free.html"><img src="java4freebanner.jpg></a> //in above example, "java4free.html" link string //and "java4freebanner.jpg" 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 block if don't want status bar messages strmessages = new array( "commission-junction. paid!", "java4free! free webtools webmasters!" ); function rotatebanner(){ if (bpause){ return; } //following code assumes banner first link/image //in html page. if not correct, need modify //the value in between [] accordingly. example, if have //2 banners appearing before banner in html, replace //the [0] [2] in lines below. document.links[0].href = strlinks[ncurrentbanner]; document.images[0].src = strimages[ncurrentbanner]; //comment out following line if don't want status bar messages window.status = strmessages[ncurrentbanner]; //now rotate next iteration if (ncurrentbanner < strlinks.length-1){ ncurrentbanner++; } else { ncurrentbanner = 0; } } function pause(){ bpause=true; } function resume(){ bpause=false; }
then created webview through xml
<webview android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" />
and inserted code oncreate method of activity want show ads
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");
questions are:
- the above javascript safe enough kind of thing? once used document.write method , site got hacked after sometime, not sure time now.
this displays ads fine guess because image in enclosing html page, when call
wv.getsettings().setloadwithoverviewmode(true);
the viewport resizes small , 320x50 image not remain same.
please help.
Comments
Post a Comment