您的位置:首页 > 移动开发 > Android开发

Android Mobile Web 集成 Webtrends

2015-01-06 15:00 369 查看
最近,需要在Sencha Touch + Phonegap的架构中在Android下集成Webtrends,记录下一些过程,查了下官网SDK说明,看起来是支持在混合模式下做点事情的,大概步骤如下,   Add a custom WebViewClient to the WebView to handle communication from JavaScript in the embedded web content to the native application.For a basic implementation, use the extendsWebView helper method in the WebtrendsDataCollector to add the Webtrends custom WebViewClient:
String URL = "file:///android_asset/WebContent/webViewContent.html";
WebView wv = (WebView)findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
WebtrendsDataCollector.getInstance().extendsWebView(wv);
wv.loadUrl(URL);
If your application uses its own custom WebViewClient, you should not use the extendsWebView helper method. Instead, ensure that the WebtrendsWebViewClient also gets invoked by following these steps:Modify your custom WebViewClient to extend WebtrendsWebViewClient instead of WebViewClient.If you are overriding onLoadResource, call super.onLoadResource.Set your custom WebViewClient on your WebView.
import android.webkit.WebView;
import com.webtrends.mobile.analytics.android.webViewExtension.WebtrendsWebViewClient;

public class MyCustomWebViewClient extends WebtrendsWebViewClient{
@Override
public void onLoadResource(WebView view, String url){
super.onLoadResource(view, url);
}
}

String URL = "file:///android_asset/WebContent/webViewContent.html";
WebView wv = (WebView)findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new MyCustomWebViewClient());
wv.loadUrl(URL);
Add WebtrendsMobileLib.js to the content that will be loaded in the WebView. WebtrendsMobileLib.js is delivered as part of the Webtrends Mobile Library for Android.Use the WebtrendsLib to generate events. In the embedded web content, use the JavaScript helper functions of the webtrendsLib object to generate events through the native app. A JavaScript helper function exists for each of the event helper functions exposed in the native Webtrends SDK. The JavaScript helper functions take the same arguments as the native helper functions.
<html>
<head>
<script type="text/javascript" src="js/WebtrendsMobileLib.js"></script>
<script type="text/javascript">
function sendButtonClickEvent() {

var eventPath = "/HelloWorld/button/click";
var eventDesc = "HelloWorld Button Click Event";
var eventType = "click";
var customData = {
customKey : "CustomValue"
};
webtrendsLib.onButtonClick(eventPath, eventDesc, eventType,
customData);
}
</script>
</head>
<body>
<div>
<input type="button" onclick="sendButtonClickEvent()" value="Click Me">
</input>
</div>
</body>
</html>
这时候问题来了,用了Phonegap后默认MainActivity要继承CordovaActivity,而如果要集成Webtrends,又得继承WebtrendsActivity,在Java下面不支持多继承,怎么搞呢。。。
看来只能出大招了,把WebtrendsSDK反编译,



看起来代码还不算多,把WebtrendsActivity.java里面的代码拷出来,粘贴到自己新建的的MyWebtrendsActivity.java,然后让MyWebtrendsActivity继承CordovaActivity,最后让MainActivity继承MyWebtrendsActivity,用组合的方法解决多重继承,继续一番折腾后,it works.

另一个问题就是如果有了自己的WebClient,还需要去继承WebtrendsWebViewClient,不幸的是PhoneGap还真实现了自己的WebViewClient,即CordovaWebViewClient,又是同样的问题,去修改PhoneGap的代码不现实也不科学。咋搞呢??纠结了一番后,发现万幸的事情,在Webtrends代码中WebtrendsDataCollector.getInstance().extendsWebView(webView);像上面说的,webview是一个原生的webview,如果有自己的webview,必须继承WebtrendsWebViewClient。我试了下把CordovaWebView传进来,也可以,不用自己去实现,汗Σ( ° △ °|||)︴

这两个问题后,其他都还好。注意在配置中修改timezone以及wt_dc_dcsid是必须的,wt_dc_dcsid需要跟Webtrends拿,估计价格不菲吧,我们是客户提供的,直接粘贴。

SDK的下载也比较奇葩,需要发贴跟管理员申请才能链接。


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: