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的下载也比较奇葩,需要发贴跟管理员申请才能链接。
相关文章推荐
- Myeclipse8.x集成android开发平台ADT
- Android Codec 集成和 video Overlay
- Android系统集成第三方pre-build库和程序
- 浅析:为何到现在高端Android集成解决方案仍未出现
- android 如何调用talk程序,集成到自己写的应用中
- android app(apk文件) 反编译 工具 右键集成
- android 中listview集成checkbox后使用的一些发现
- 集成Lua到你的Android游戏
- maven+android+eclipse集成配置
- 最近在U盘上集成了Android的x86版本
- 构建Android自动编译持续集成系统
- 二、MyEclipse8.5集成Android环境
- 在eclipse的帮助内容中集成Android的API文档
- 将Android源码集成到Eclipse中的方法
- Android Codec 集成和 video Overlay --转载
- Android Codec 集成和 video Overlay
- Android Codec 集成和 video Overlay
- 集成android源码中的 packages/apps 到eclipse 比如launcher2
- MyEclipse8.5集成Android环境
- Android Codec 集成和 video Overlay