下面列出了android.webkit.WebSettings#setAllowContentAccess ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initWebView() {
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("https://www.analysys.cn");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setAllowContentAccess(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view,
String url) { //
// 重写此方法表明点击网页里面的链接还是在当前的webview里跳转,不跳到浏览器那边
view.loadUrl(url);
return true;
}
});
}
private void initWebView() {
WebView webView = (WebView) findViewById(R.id.test_activity_webView);
webView.loadUrl("https://www.analysys.cn");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setAllowContentAccess(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view,
String url) { //
// 重写此方法表明点击网页里面的链接还是在当前的webview里跳转,不跳到浏览器那边
view.loadUrl(url);
return true;
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setTopBarTitle("详情页面");
Intent getIntent = getIntent();
btUrl=getIntent.getStringExtra("url");
lt = new LoadToast(this);
webView.setWebChromeClient(new MyWebChromeClient());
WebSettings webSettings = webView.getSettings();
webSettings.setAppCacheEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.supportMultipleWindows();
webSettings.setAllowContentAccess(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setJavaScriptEnabled(false);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(btUrl);
lt.setTranslationY(200).setBackgroundColor(getResources().getColor(R.color.colorMain)).setProgressColor(getResources().getColor(R.color.white));
lt.show();
}
/**
* Initialises YoutubeWebView with given videoId and youtubeListener
*/
@SuppressLint("SetJavaScriptEnabled")
private void initWebView(String webViewUrl) {
WebSettings set = this.getSettings();
set.setJavaScriptEnabled(true);
set.setUseWideViewPort(true);
set.setLoadWithOverviewMode(true);
set.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
set.setCacheMode(WebSettings.LOAD_DEFAULT);
set.setPluginState(WebSettings.PluginState.ON_DEMAND);
set.setAllowContentAccess(true);
set.setAllowFileAccess(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
set.setMediaPlaybackRequiresUserGesture(false);
}
this.setLayerType(View.LAYER_TYPE_NONE, null);
this.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
this.loadUrl(webViewUrl);
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setWebContentsDebuggingEnabled(true);
}
this.setWebViewClient(initWebViewClient());
}
@SuppressLint("SetJavaScriptEnabled")
public void init() {
mUiThread = Thread.currentThread();
audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
addJavascriptInterface(this, IBase.JS_BASE_INTERFACE);
WebSettings settings = getSettings();
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
settings.setBuiltInZoomControls(false);
settings.setDefaultFontSize(16);
settings.setTextZoom(100);
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccess(true);
settings.setAllowContentAccess(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
setRelativeFontSize(Preferences.Main.getWebViewSize(getContext()));
setBackgroundColor(App.getColorFromAttr(getContext(), R.attr.background_base));
}
@SuppressLint({ "SetJavaScriptEnabled", "AddJavascriptInterface" })
@Override public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_webview, container, false);
webView = (WebView) view.findViewById(R.id.webview);
mLayout = (ViewGroup) view;
WebSettings webViewSettings = webView.getSettings();
webViewSettings.setSupportZoom(true);
webViewSettings.setBuiltInZoomControls(false);
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setGeolocationEnabled(true);
webViewSettings.setAllowFileAccess(false);
webViewSettings.setAllowContentAccess(false);
webView.setWebChromeClient(new GeoWebChromeClient());
AirMapType mapType = AirMapType.fromBundle(getArguments());
webView.loadDataWithBaseURL(mapType.getDomain(), mapType.getMapData(getResources()),
"text/html", "base64", null);
webView.addJavascriptInterface(new MapsJavaScriptInterface(), "AirMapView");
return view;
}
@SuppressWarnings("deprecation")
@SuppressLint({"SetJavaScriptEnabled", "NewApi", "ObsoleteSdkInt"})
public void initializeSettings(WebSettings settings) {
settings.setJavaScriptEnabled(true);
addJavascriptInterface(new JavaScriptUtil(), "DappJsBridge");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
settings.setAppCacheMaxSize(Long.MAX_VALUE);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
settings.setEnableSmoothTransition(true);
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
settings.setMediaPlaybackRequiresUserGesture(true);
}
WebView.setWebContentsDebuggingEnabled(true);
settings.setDomStorageEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setAppCacheEnabled(true);
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
settings.setLoadsImagesAutomatically(true);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
settings.setDatabaseEnabled(true);
settings.setDisplayZoomControls(false);
settings.setAllowContentAccess(true);
settings.setAllowFileAccess(false);
settings.setRenderPriority(WebSettings.RenderPriority.LOW);
setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
settings.setDefaultTextEncodingName("utf-8");
settings.setAllowFileAccessFromFileURLs(false);
settings.setAllowUniversalAccessFromFileURLs(false);
// 特别注意:5.1以上默认禁止了https和http混用,以下方式是开启
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
}
}
/**
* 初始化传入的webView
*/
@SuppressLint("SetJavaScriptEnabled")
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public WebView initialWebView(WebView webView){
webView.setHorizontalScrollBarEnabled(false);
//不能纵向滚动
webView.setVerticalScrollBarEnabled(false);
//允许截图
webView.setDrawingCacheEnabled(true);
//屏蔽长按事件
webView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
});
//初始化WebSettings
final WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
final String ua = settings.getUserAgentString();
settings.setUserAgentString(ua + "Latte");
//隐藏缩放控件
settings.setBuiltInZoomControls(false);
settings.setDisplayZoomControls(false);
//禁止缩放
settings.setSupportZoom(false);
//文件权限
settings.setAllowFileAccess(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setAllowContentAccess(true);
//缓存相关
settings.setAppCacheEnabled(true);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
return webView;
}
private void init(Context context) {
mPlayer = new WebPlayer(this);
setBackgroundColor(Color.BLACK);
enableJavascript();
WebSettings webSettings = getSettings();
webSettings.setAllowContentAccess(true);
webSettings.setAllowFileAccess(true);
webSettings.setAppCacheEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDatabasePath(context.getDir("database", Context.MODE_PRIVATE).getPath());
webSettings.setDomStorageEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setSupportMultipleWindows(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
webSettings.setMediaPlaybackRequiresUserGesture(false);
}
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
}
setWebChromeClient(new ChromeClient());
setWebViewClient(new ViewClient());
}
private synchronized void initWebSettings() {
WebSettings webSettings = getSettings();
userAgentOriginal = webSettings.getUserAgentString();
webSettings.setAllowContentAccess(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAppCacheEnabled(true);
webSettings.setAppCachePath(context.getCacheDir().toString());
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setGeolocationDatabasePath(context.getFilesDir().toString());
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setDefaultTextEncodingName(BrowserUnit.URL_ENCODING);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webSettings.setLoadsImagesAutomatically(true);
} else {
webSettings.setLoadsImagesAutomatically(false);
}
}
@Override
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected WebView createViewInstance(ThemedReactContext reactContext) {
ReactWebView webView = createReactWebViewInstance(reactContext);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onConsoleMessage(ConsoleMessage message) {
if (ReactBuildConfig.DEBUG) {
return super.onConsoleMessage(message);
}
// Ignore console logs in non debug builds.
return true;
}
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
reactContext.addLifecycleEventListener(webView);
mWebViewConfig.configWebView(webView);
WebSettings settings = webView.getSettings();
settings.setBuiltInZoomControls(true);
settings.setDisplayZoomControls(false);
settings.setDomStorageEnabled(true);
settings.setAllowFileAccess(false);
settings.setAllowContentAccess(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
settings.setAllowFileAccessFromFileURLs(false);
setAllowUniversalAccessFromFileURLs(webView, false);
}
setMixedContentMode(webView, "never");
// Fixes broken full-screen modals/galleries due to body height being 0.
webView.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
setGeolocationEnabled(webView, false);
if (ReactBuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
return webView;
}
@SuppressWarnings("deprecation")
@SuppressLint({ "SetJavaScriptEnabled", "NewApi" })
public void initializeSettings(WebSettings settings, Context context) {
//setPageCacheCapacity2(settings);
if (API < 18) {
settings.setAppCacheMaxSize(Long.MAX_VALUE);
}
if (API < 17) {
settings.setEnableSmoothTransition(true);
}
if (API > 16) {
settings.setMediaPlaybackRequiresUserGesture(true);
}
if (API >= Build.VERSION_CODES.LOLLIPOP && !mBrowserController.isIncognito()) {
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
} else if (API >= Build.VERSION_CODES.LOLLIPOP) {
// We're in Incognito mode, reject
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
}
settings.setDomStorageEnabled(true);
settings.setAppCacheEnabled(true);
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
settings.setDatabaseEnabled(true);
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
settings.setDisplayZoomControls(false);
settings.setAllowContentAccess(true);
settings.setAllowFileAccess(true);
settings.setDefaultTextEncodingName("utf-8");
if (API > 16) {
settings.setAllowFileAccessFromFileURLs(false);
settings.setAllowUniversalAccessFromFileURLs(false);
}
settings.setAppCachePath(context.getDir("appcache", 0).getPath());
settings.setGeolocationDatabasePath(context.getDir("geolocation", 0).getPath());
if (API < Build.VERSION_CODES.KITKAT) {
settings.setDatabasePath(context.getDir("databases", 0).getPath());
}
}
@SuppressLint("SetJavaScriptEnabled") // We explicitly want to enable JavaScript
private void configureDefaultSettings(Context context, WebSettings settings) {
settings.setJavaScriptEnabled(true);
// Needs to be enabled to display some HTML5 sites that use local storage
settings.setDomStorageEnabled(true);
// Enabling built in zooming shows the controls by default
settings.setBuiltInZoomControls(true);
// So we hide the controls after enabling zooming
settings.setDisplayZoomControls(false);
// To respect the html viewport:
settings.setLoadWithOverviewMode(true);
// Also increase text size to fill the viewport (this mirrors the behaviour of Firefox,
// Chrome does this in the current Chrome Dev, but not Chrome release).
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
// Disable access to arbitrary local files by webpages - assets can still be loaded
// via file:///android_asset/res, so at least error page images won't be blocked.
settings.setAllowFileAccess(false);
settings.setAllowFileAccessFromFileURLs(false);
settings.setAllowUniversalAccessFromFileURLs(false);
final String appName = context.getResources().getString(R.string.useragent_appname);
settings.setUserAgentString(buildUserAgentString(context, settings, appName));
// Right now I do not know why we should allow loading content from a content provider
settings.setAllowContentAccess(false);
// The default for those settings should be "false" - But we want to be explicit.
settings.setAppCacheEnabled(false);
settings.setDatabaseEnabled(false);
settings.setJavaScriptCanOpenWindowsAutomatically(false);
// We do not implement the callbacks - So let's disable it.
settings.setGeolocationEnabled(false);
// We do not want to save any data...
settings.setSaveFormData(false);
//noinspection deprecation - This method is deprecated but let's call it in case WebView implementations still obey it.
settings.setSavePassword(false);
}
public EditAreaView(Context context, AttributeSet attrs) {
super(context, attrs);
if (L.debug) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setWebContentsDebuggingEnabled(true);
}
}
setLongClickable(false);
setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
});
WebSettings ws = getSettings();
ws.setDefaultZoom(WebSettings.ZoomDensity.FAR);
ws.setAllowContentAccess(true);
ws.setAllowFileAccess(true);
ws.setBuiltInZoomControls(false);
ws.setDefaultTextEncodingName("utf-8");
ws.setDisplayZoomControls(false);
ws.setSupportZoom(false);
ws.setLoadWithOverviewMode(false);
ws.setJavaScriptEnabled(true);
ws.setAppCacheEnabled(false);
ws.setDomStorageEnabled(true);
ws.setAppCacheMaxSize(1024*1024*80);
ws.setAppCachePath(context.getCacheDir().getPath());
// ws.setAllowFileAccess(true);
ws.setCacheMode(WebSettings.LOAD_DEFAULT);
addJavascriptInterface(new JavascriptApi(), "AndroidEditor");
setWebViewClient(new EditorViewClient());
setWebChromeClient(new EditorViewChromeClient());
pref = Pref.getInstance(getContext());
ThemeList.Theme theme = pref.getThemeInfo();
boolean isDark = false;
if (theme != null) {
isDark = theme.isDark;
}
String html = null;
try {
InputStream is = getContext().getAssets().open("editor.html");
html = IOUtils.readFile(is, "utf-8");
is.close();
} catch (Exception e) {
L.e(e);
UIUtils.toast(getContext(), R.string.editor_create_unknown_exception);
return;
}
if (!isDark)
html = html.replaceAll("<\\!\\-\\-\\{DARK\\-START\\}\\-\\->[\\w\\W]+?<\\!\\-\\-\\{DARK\\-END\\}\\-\\->", "");
loadDataWithBaseURL("file:///android_asset/", html, "text/html", "utf-8", "file:///android_asset/");
//fix dark theme background spark
setBackgroundColor(Color.TRANSPARENT);
pref.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(null, Pref.KEY_FONT_SIZE);
onSharedPreferenceChanged(null, Pref.KEY_CURSOR_WIDTH);
onSharedPreferenceChanged(null, Pref.KEY_SHOW_LINE_NUMBER);
onSharedPreferenceChanged(null, Pref.KEY_WORD_WRAP);
onSharedPreferenceChanged(null, Pref.KEY_SHOW_WHITESPACE);
onSharedPreferenceChanged(null, Pref.KEY_TAB_SIZE);
onSharedPreferenceChanged(null, Pref.KEY_AUTO_INDENT);
onSharedPreferenceChanged(null, Pref.KEY_AUTO_CAPITALIZE);
onSharedPreferenceChanged(null, Pref.KEY_INSERT_SPACE_FOR_TAB);
onSharedPreferenceChanged(null, Pref.KEY_THEME);
onSharedPreferenceChanged(null, Pref.KEY_TOUCH_TO_ADJUST_TEXT_SIZE);
enableHighlight(pref.isHighlight());
setReadOnly(pref.isReadOnly());
}
public static void setAllowContentAccess(WebSettings settings, boolean allow) {
settings.setAllowContentAccess(allow);
}