我面临有关在 5.0 以下的 android 中加载 url 的问题( Lollipop ) 加载数据太慢,但在 android 5.0 或更高版本中工作正常。如何解决这个问题? I have follow this link。 & How to improve webview load time
我的代码是
@SuppressLint({ "SetJavaScriptEnabled", "DefaultLocale" })
public class YS_WebViewActivity extends Activity
{
private Button btnDone;
private WebView webViewLoadUrl;
private YS_GeneralUtility generalUtility;
private Dialog alertDialogBox;
private String url = "";
private TextView tvLoading;
private Button btnCancel;
private Button btnRetry;
@SuppressLint("InlinedApi")
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
btnDone = (Button) findViewById(R.id.btnDone);
webViewLoadUrl = (WebView) findViewById(R.id.webViewLoadUrl);
//webViewLoadUrl.setBackgroundColor(Color.parseColor("#123456"));
tvLoading = (TextView) findViewById(R.id.tvLoading);
url = getIntent().getExtras().getString("url");
webViewLoadUrl.setVisibility(View.INVISIBLE);
generalUtility = new YS_GeneralUtility(this);
webViewLoadUrl.getSettings().setJavaScriptEnabled(true); // enable javascript
try {
webViewLoadUrl.getSettings().setRenderPriority(RenderPriority.HIGH);
webViewLoadUrl.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webViewLoadUrl.getSettings().setDomStorageEnabled(true);
webViewLoadUrl.getSettings().setAllowFileAccess(true);
webViewLoadUrl.getSettings().setAllowContentAccess(true);
webViewLoadUrl.getSettings().setAllowFileAccessFromFileURLs(true);
//webViewLoadUrl.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
} catch (Exception e) {
e.printStackTrace();
}
tvLoading.setTypeface(generalUtility.faceAvenirLight);
btnDone.setTypeface(generalUtility.faceAvenirMedium);
btnDone.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
myErrorFinish();
}
});
webViewLoadUrl.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
//Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url)
{
//Log.i(TAG, "Finished loading URL: " + url);
webViewLoadUrl.setVisibility(View.VISIBLE);
tvLoading.setVisibility(View.INVISIBLE);
}
String errorData = "<html><head></head>"
+ "<body style=' font-family:Avenir !important;font-weight:lighter !important; color:#ffffff; margin : 0px auto;'>"
+ "<div style = 'width : 100% ; height : 100% ; background:#123456;'> </div>"
+ "</body></html>";
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Log.e(TAG, failingUrl + "Error: " + description);
view.loadData(errorData, "text/html", "UTF-8");
alertDialogOnInternetOff(getString(R.string.errorMsgInternet));
// finish();
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
handler.proceed();
}
});
webViewLoadUrl.loadUrl(url);
// setContentView(webview);
}
private void alertDialogOnInternetOff(String message)
{
alertDialogBox = new Dialog(YS_WebViewActivity.this, R.style.InternetErrorPopup);
alertDialogBox.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialogBox.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialogBox.setContentView(R.layout.internetpopup_dialog);
alertDialogBox.setCancelable(false);
alertDialogBox.show();
btnCancel = (Button) alertDialogBox.findViewById(R.id.btnCancel);
btnRetry = (Button) alertDialogBox.findViewById(R.id.btnRetry);
TextView txtExitMessage = (TextView) alertDialogBox.findViewById(R.id.textViewMessage);
TextView horizontalLinePopup = (TextView) alertDialogBox.findViewById(R.id.horizontalLinePopup);
TextView verticalLinePopup = (TextView) alertDialogBox.findViewById(R.id.verticalLinePopup);
horizontalLinePopup.setBackgroundColor(getResources().getColor(R.color.internetpopupBorderinnerpageColor));
verticalLinePopup.setBackgroundColor(getResources().getColor(R.color.internetpopupBorderinnerpageColor));
txtExitMessage.setTypeface(generalUtility.faceAvenirLight);
btnCancel.setTypeface(generalUtility.faceAvenirLight);
btnRetry.setTypeface(generalUtility.faceAvenirLight);
txtExitMessage.setText(R.string.errorMsgInternet);
btnCancel.setText(R.string.cancel);
btnRetry.setText(R.string.retry);
alertDialogBox.setCancelable(false);
btnCancel.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
alertDialogBox.dismiss();
Log.d("AlertDialog", "Negative");
YS_WebViewActivity.this.finish();
}
});
btnRetry.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
alertDialogBox.dismiss();
Log.d("AlertDialog", "Positive");
tvLoading.setVisibility(View.VISIBLE);
webViewLoadUrl.loadUrl(url);
}
});
}
private void myErrorFinish()
{
/*
* Jump to the Setting Screen on pressing back button of device
*/
YS_WebViewActivity.this.finish();
//overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
}
@Override
public void onBackPressed()
{
myErrorFinish();
super.onBackPressed();
}
}
这是 XML...
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/listback"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="42dip"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@drawable/navigationbackground" />
<Button
android:id="@+id/btnDone"
android:layout_width="wrap_content"
android:layout_height="42dip"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@android:color/transparent"
android:text="@string/done"
android:textColor="@android:color/white"
android:textSize="17sp" />
<WebView
android:id="@+id/webViewLoadUrl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/btnDone"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/tvLoading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="@string/loading"
android:textColor="@android:color/white"
android:textSize="18sp" />
</RelativeLayout>
最佳答案
在 Android 4.4(2013 年 10 月)中,Google 切换了其 WebView to use the Chromium rendering engine — 此举带来了显着的速度改进、大量新 API 以及使用 Chrome DevTools 进行远程调试的能力。
根据这个Google’s documentation他们在 Android 5.0 中更新了一些额外的功能,为 webview 带来了更高的性能。
The initial release for Android 5.0 includes a version of Chromium for WebView based on the Chromium M37 release, adding support for WebRTC, WebAudio, and WebGL.
Chromium M37 also includes native support for all of the Web Components specifications: Custom Elements, Shadow DOM, HTML Imports, and Templates. This means you can use Polymer and its material design elements in a WebView without needing polyfills.
Although WebView has been based on Chromium since Android 4.4, the Chromium layer is now updatable from Google Play.
因此,根据 webview 中的高端变化,与低版本相比,它可以更有效地加载数据。
对于你的问题,你可以在你的情况下加快 android webview 的性能,比如 Android webview slow和 Android WebView performance或者喜欢 Android webview loading data performance very slow
Hardware Acceleration vll 就是诀窍。您可以在此 answer 中的应用程序中以不同级别使用它我也提到了其他类型。
我认为以下方法效果最好:
if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
希望这个回答对你有帮助。
关于Android webview 在低于 5.0(lollipop) 时很慢,但在 5.0 中运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32457143/
我是ruby的新手,我认为重新构建一个我用C#编写的简单聊天程序是个好主意。我正在使用Ruby2.0.0MRI(Matz的Ruby实现)。问题是我想在服务器运行时为简单的服务器命令提供I/O。这是从示例中获取的服务器。我添加了使用gets()获取输入的命令方法。我希望此方法在后台作为线程运行,但该线程正在阻塞另一个线程。require'socket'#Getsocketsfromstdlibserver=TCPServer.open(2000)#Sockettolistenonport2000defcommandsx=1whilex==1exitProgram=gets.chomp
我正在为一个类赋值,它在rspec测试中使用了column_types方法。it"Userdatabasestructureinplace"doexpect(User.column_names).toinclude"password_digest","username"expect(User.column_types["username"].type).toeq:stringexpect(User.column_types["password_digest"].type).toeq:stringexpect(User.column_types["created_at"].type).t
如何在Rake任务中运行Capybara功能?例如:访问('http://google.com')谢谢! 最佳答案 在任务中尝试这样的事情:require'capybara'require'capybara/dsl'Capybara.current_driver=:seleniumBrowser=Class.new{includeCapybara::DSL}page=Browser.new.pagepage.visit("http://www.google.com")puts(page.html)
我不确定这种事情是否很常见,但我一直在尝试创建只是Rails应用程序包装器的gem。我的gem将有一个生成器来创建config.ru,但Rails应用程序将位于gem的lib目录中。我需要知道如何“嵌入”Rails应用程序并对其进行配置,以便它可以在gem中运行。例如:$mygemnewprojectmygemcreatedadirectorycalled"project"withthefollowingfiles:project/config.ruproject/widgets/project/foobars/我的gem还将生成一些需要以某种方式添加到Rails的目录,以便我可以从G
在纯Rubyirb中,不能输入{if:1}。该语句不会终止,因为irb认为if不是符号,而是if语句的开始。那么为什么Rails可以有before_filter接受if作为参数?该指南的代码如下:classOrderunless也会发生同样的事情。 最佳答案 这是一个irb问题,而不是Ruby。bash=>ruby-e"puts({if:1})"bash=#{:if=>1}您可以改用pry。它将正确读取输入。https://github.com/pry/pry 关于ruby-on-rai
我使用“newapp_name”创建了一个新的Rails应用程序,我正在尝试编辑.gitignore文件,但在我的应用程序文件夹中找不到它。我在哪里可以找到它?我安装了Git。 最佳答案 .gitignore位于项目的root中,而不是app子目录中。首先打开终端并进入您的目录。您需要使用ls-a来显示stash文件。然后使用打开.gitignore 关于ruby-on-rails-尝试打开.gitignore以在文本编辑器中对其进行编辑,但在OSXMountainLion上找不到文件位
我有一个定义类的Ruby脚本。我希望脚本执行语句BoolParser.generate:file_base=>'bool_parser'仅当脚本作为可执行文件被调用时,而不是当它被irbrequire(或通过-r在命令行上传递)时。我可以用什么来包装上面的语句,以防止它在我的Ruby文件加载时执行? 最佳答案 条件$0==__FILE__...!/usr/bin/ruby1.8classBoolParserdefself.generate(args)p['BoolParser.generate',args]endendif$0==_
我有一个包含多个组件的存储库,其中大部分是用JavaScript(Node.js)编写的,一个是用Ruby(RubyonRails)编写的。我想要一个.travis.yml文件来触发一个运行每个组件的所有测试的构建。根据thisTravisCIGoogleGroupthread,目前还没有官方支持。我的目录结构是这样的:.├──构建服务器├──核心├──扩展├──网络应用├──流浪文件├──package.json├──.travis.yml└──生成文件我希望能够运行特定版本的Ruby(2.2.2)和Node.js(0.12.2)。我已经有了一个make目标,所以maketest在每
我遇到了这种行为,想知道是否有其他人看到过它。我有一个解决方法,因此它不会成为阻碍。我使用Cedar堆栈在Heroku上创建了一个新应用程序。在演示多个环境时,我添加了以下配置变量:herokuconfig:addRACK_ENV=staging--appappname我目视验证环境变量已设置,然后将以下路由放入我的简单Sinatra示例中:get'/?'doENV['RACK_ENV']end当我在笔记本电脑上进行本地测试时,我收到了预期的开发。当我推送到Heroku并在herokuapp.com上点击相同的路线时,我得到了development而不是staging。我通过Procf
我是编程新手,正在尝试遵循使用#!用ruby评论。我一直收到消息:bash:matz.rb:找不到命令我正在使用这个评论:#!/usr/bin/envruby我试过有和没有后面的空格!以及有和没有环境。当我使用$哪个rubyruby在:/usr/bin/ruby我还进入了操作系统,将所有用户对文件matz.rb的权限更改为rwx,但没有任何效果。是我做错了什么还是我的系统设置不正确? 最佳答案 /usr/bin/env部分没问题。运行时需要为bash提供matz.rb的路径。如果您在matz.rb所在的目录中,请键入“./matz.