在此先感谢我正在尝试将webview缓存机制实现到我已经完成的webview apk但我无法完成这项工作。 我非常不专业。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uy.celular.service.HR" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<activity android:name="A" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="activity_a" />
</intent-filter>
</activity>
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package uy.celular.service.HR;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(uy.celular.service.HR.R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
webView = (WebView) findViewById(R.id.activity_main_webview);
webView.setInitialScale(110);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new MyAppWebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
WebView webView = new WebView(context);
webView.getSettings().setAppCacheMaxSize(5 * 1024 * 1024); // 5MB
webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // load online by default
if (!isNetworkAvailable()) { // loading offline
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
view.loadUrl(url);
return false;
}
});
webView.loadUrl("http://www.celularservice.uy/p/mobile.html?m=1#trab");
webView.setWebViewClient(new MyAppWebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}});
}
@Override
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
}
package uy.celular.service.HR;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Context;
public class MyAppWebViewClient extends WebViewClient {
public Context context;
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) this.context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("http://www.celularservice.uy/p/wp.html?m=1")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
if (url != null && url.startsWith("https://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
else if (url.startsWith("tel:")) {
Intent tel = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
view.getContext().startActivity(tel);
return true;
}
else if (url.startsWith("sms:")) {
Intent sms = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
view.getContext().startActivity(sms);
return true;
}
else if (url.startsWith("mailto:")) {
Intent mail = new Intent(Intent.ACTION_SEND);
mail.setType("application/octet-stream");
mail.putExtra(Intent.EXTRA_EMAIL, new String[]{"celularservice.uy@gmail.com"});
view.getContext().startActivity(mail);
return true;
}
return true;
}
}
记住我是新手,请尽力回答
答案 0 :(得分:0)
getSystemService(Context.CONNECTIVITY_SERVICE)
需要上下文。您需要将上下文传递给WebViewClient
或将WebViewClient
作为活动的内部类
答案 1 :(得分:0)
替换这些:
webView.getSettings().setAppCacheMaxSize(5 * 1024 * 1024); // 5MB
webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // load online by
if (!isNetworkAvailable()) { // loading offline
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
与
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
你很高兴。我建议不要将webView用作WebView
变量。而且,setAppCacheMaxSize
已被弃用。