在背景音乐的网页 [英] Track webpage in background
问题描述
我想基于web服务来设计一个应用程序,我也想通过的WebView
根据Web服务数据,打开一些背景页面,能够跟踪在 Google分析
页。
I want to design an app based on a web-service, also I want to open some background page according to web-service data via WebView
to be able to track that pages in GoogleAnalytics
.
要完成这个想法,我写了下面的单,它的工作,因为我预期,但有这code,我认为它造成的形式奇怪的内存泄漏行为的WebView
错误!
To accomplish this ideas I wrote the following singleton and It's work as I expected but there is strange memory leak behavior in this code that I assume it caused form WebView
bug!
public class SiteLoader {
private static SiteLoader mSiteLoader;
private WebView mWebView;
private Context c;
public enum LoaderUri {
MUSIC_URI, VIDEO_URI, BOOK_URI
}
private SiteLoader(Context c) {
this.c = c.getApplicationContext();
}
public static SiteLoader getInstance(Context c) {
if (mSiteLoader == null)
mSiteLoader = new SiteLoader(c);
return mSiteLoader;
}
public void trackUri(LoaderUri uri, String extraData) {
// Load new progress
switch (uri) {
case MUSIC_URI:
getWebView().loadUrl(Constants.HOST_PREFIX + "/music/?id=" + extraData);
break;
case VIDEO_URI:
getWebView().loadUrl(Constants.HOST_PREFIX + "/video/?id=" + extraData);
break;
case BOOK_URI:
getWebView().loadUrl(Constants.HOST_PREFIX + "/book/?id=" + extraData);
break;
}
}
public void destroyTracker() {
if (mWebView != null) {
mWebView.destroy();
mWebView = null;
}
}
private WebView getWebView() {
if (mWebView == null) {
mWebView = new WebView(c);
mWebView.getSettings().setJavaScriptEnabled(true);
}
return mWebView;
}
}
任何想法如何从这种情况下摆脱或者是有直接钉在页面上任何其他替代方式 Web服务
测试用例活动:
public class TestActivity extends Activity {
@Override
public void onResume() {
super.onResume();
SiteLoader.getInstance(this).trackUri(LoaderUri.VIDEO_URI, "en");
}
@Override
public void onPause() {
super.onResume();
SiteLoader.getInstance(this).destroyTracker();
}
@Override
public void onCreate(Bundle mBundle) {
super.onCreate(mBundle);
Toast.makeText(this, "System is running", Toast.LENGTH_LONG).show();
}
}
先谢谢了...
Thanks in Advance...
推荐答案
与OP讨论后,似乎没有内存泄漏本身在这种情况下,而该网页视图是使用时消耗的RAM相对高量的内存密集型组件。
After discussing with the OP, it appears that there is no memory leak per se in this case; rather that the WebView is a memory intensive component that consumes a relatively high amount of RAM when used.
这篇关于在背景音乐的网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!