gpt4 book ai didi

java - 在 Android WebView 中使用缓存

转载 作者:行者123 更新时间:2023-11-29 02:29:18 24 4
gpt4 key购买 nike

几天来,我一直在为我的 WebView 应用程序寻找一个好的缓存解决方案。我找到的解决方案似乎对我不起作用。我是制作应用程序的新手,而且对缓存的更改已弃用许多旧解决方案,这似乎阻碍了我。

本质上,我拥有的是一个大型 WebView,底部导航栏上有一排按钮可以更改 WebView 的 URL。我希望之前已经加载的页面从缓存中加载。这应该是 WebView 的默认行为,但它似乎没有加载得更快(它应该几乎是即时的,对吧?)。如果网络不可用,我还希望从缓存中加载 WebView。

这是我到目前为止的代码 fragment ,我之前的缓存尝试已被删除。

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

my_web_view = (WebView)findViewById(R.id.web_view); //grab a handle on the web wrapper

//set all of our variables for use later
home_URL = "https://akvcoc.com/app-home";
events_URL = "https://akvcoc.com/app-events";
directory_URL = "https://akvcoc.ctrn.co/directory/index.php";

my_web_view.loadUrl(home_URL); //set the web wrapper to the home page

my_web_view.getSettings().setJavaScriptEnabled(true); //and set javascript to true

}

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener()
{

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
{
switch (item.getItemId())
{
case R.id.navigation_home:
my_web_view.loadUrl(home_URL);
return true;
case R.id.navigation_events:
my_web_view.loadUrl(events_URL);
return true;
case R.id.navigation_directory:
my_web_view.loadUrl(directory_URL);
return true;
}
return false;
}
};

最佳答案

您添加此行以在 webview 中加载 javascript。

my_web_view.getSettings().setDomStorageEnabled(true);

关于java - 在 Android WebView 中使用缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50538668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com