android全局webview加载慢,Android webview加载数据性能非常慢

博主在处理Android应用时,使用WebView从2.5MB的test.txt文件加载字符串HTML格式数据。首次启动WebView活动加载较快,后续加载耗时增加。博主尝试多种方法,如设置硬件加速、渲染优先级等均无效,还给出了相关代码,希望得到解决办法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

嗨,我正在处理一个应用程序,因为我在使用

Android WebView.

每当我启动webview活动时,从test.txt文件加载字符串html格式的数据.

test.txt文件包含近2.5 MB的数据,加载test.txt文件后,如果幻灯片屏幕结束读取所有数据并按回来.

然后随后推出的webview活动花费更多的时间来呈现数据.在第一次推出webview时,花费最少的时间.

在启动此活动之前,我没有收到任何错误/异常/崩溃.

我正在使用Android API等级18及以上

我不能使android:hardwareAccelerated =“true”< - 为什么因为它有很多副作用(仍然我使用这个,但没有发现这个问题的变化). 我不能使用

webview.getSettings()setRenderPriority(RenderPriority.HIGH).

setRenderPriority() – > API方法18中已弃用此方法.

不建议调整线程优先级,以后的版本不支持.

我的DataLoader.java类

public class DataLoader extends Activity {

private WebView webView = null;

// Progress Dialog

private ProgressDialog progressDialog;

String dataContent = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.web_view);

webView = (WebView)findViewById(R.id.text);

// Loading webview in Background Thread

new LoadWebView().execute();

}

class LoadWebView extends AsyncTask {

@Override

protected void onPreExecute() {

super.onPreExecute();

progressDialog = new ProgressDialog(DataLoader.this);

progressDialog.setTitle("Loading...");

progressDialog.setMessage("Please wait.");

progressDialog.setCancelable(false);

progressDialog.setIndeterminate(true);

progressDialog.show();

}

protected String doInBackground(String... args) {

// AssetFileReader read the txt file and return data in the form of string

dataContent = AssetFileReader.read(getApplicationContext(), "test.txt");

return null;

}

protected void onPostExecute(String str) {

// dismiss the dialog

progressDialog.dismiss();

if (dataContent != null) {

// updating UI from Background Thread

runOnUiThread(new Runnable() {

public void run() {

WebSettings s = webView.getSettings();

s.setUseWideViewPort(true);

s.setSupportZoom(true);

s.setBuiltInZoomControls(true);

s.setDisplayZoomControls(false);

s.setJavaScriptEnabled(true);

webView.loadData(dataContent, "text/html", "utf-8");

}

});

}

}

}

@Override

public void onDestroy() {

super.onDestroy();

webView.clearCache(true);

webView.clearHistory();

}

}

我的web_view.xml文件

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/text">

如果您有任何关于这个问题的工作,请让我知道.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值