Java Code Examples for android.webkit.WebSettings#setJavaScriptEnabled()

The following examples show how to use android.webkit.WebSettings#setJavaScriptEnabled() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: lua_web.java    From stynico with MIT License 6 votes vote down vote up
private void initWebViewSettings()
{
    WebSettings webSettings = wv_web.getSettings();
    //可以有缓存
    webSettings.setAppCacheEnabled(true);
    webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    //设置支持页面js可用
    webSettings.setJavaScriptEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    //设置允许访问文件数据
    webSettings.setAllowFileAccess(true);
    //可以使用localStorage
    webSettings.setDomStorageEnabled(true);
    //可以有数据库
    webSettings.setDatabaseEnabled(true);
    //设置定位的数据库路径
    String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
    webSettings.setGeolocationDatabasePath(dir);
    //启用地理定位
    webSettings.setGeolocationEnabled(true);
}
 
Example 2
Source File: WebFormActivity.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  WebView mainWebView = new WebView(this);
  setContentView(mainWebView);
  mainWebView.loadData(
        "<html>"
        + "<body>"
        + "<script>"
        + "  function onSubmit() {"
        + "    value = document.getElementById('input').value;"
        + "    document.getElementById('info').innerHTML = 'Submitted: ' + value;"
        + "  }"
        + "</script>"
        + "<form action='javascript:onSubmit()'>"
        + "  Input: <input type='text' id='input' value='sample'>"
        + "  <input type='submit' id='submit' value='Submit'>"
        + "</form>"
        + "<p id='info'>Enter input and click the Submit button.</p>"
        + "</body>"
      + "</html>", "text/html", null);
  WebSettings settings = mainWebView.getSettings();
  settings.setJavaScriptEnabled(true);
}
 
Example 3
Source File: WebViewActivity.java    From NestedTouchScrollingLayout with MIT License 6 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
private void initWebSettings() {
    WebSettings settings = this.mWebView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setUseWideViewPort(true);
    settings.setLoadWithOverviewMode(true);
    settings.setSupportZoom(false);
    settings.setDisplayZoomControls(false);
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
    settings.supportMultipleWindows();
    settings.setSupportMultipleWindows(true);
    settings.setDomStorageEnabled(true);
    settings.setDatabaseEnabled(true);
    settings.setAppCacheEnabled(true);
    settings.setAppCachePath(this.mWebView.getContext().getCacheDir().getAbsolutePath());
    settings.setAllowFileAccess(true);
    settings.setLoadsImagesAutomatically(true);

    settings.setDefaultTextEncodingName("UTF-8");
}
 
Example 4
Source File: BaseDetailFragment.java    From Cotable with Apache License 2.0 6 votes vote down vote up
@SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"})
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
protected void initWebView(WebView webView) {
    WebSettings settings = webView.getSettings();
    settings.setDefaultFontSize(15);
    settings.setJavaScriptEnabled(true);
    settings.setSupportZoom(true);
    settings.setBuiltInZoomControls(true);

    int sysVersion = Build.VERSION.SDK_INT;
    if (sysVersion >= 11) {
        settings.setDisplayZoomControls(false);
    } else {
        ZoomButtonsController zbc = new ZoomButtonsController(webView);
        zbc.getZoomControls().setVisibility(View.GONE);
    }

}
 
Example 5
Source File: WebViewSignInActivity.java    From input-samples with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.login_webview_activity);

    WebView webView = findViewById(R.id.webview);
    WebSettings webSettings = webView.getSettings();
    webView.setWebViewClient(new WebViewClient());
    webSettings.setJavaScriptEnabled(true);

    String url = getIntent().getStringExtra("url");
    if (url == null) {
        url = "file:///android_res/raw/sample_form.html";
    }
    if (DEBUG) Log.d(TAG, "Clearing WebView data");
    webView.clearHistory();
    webView.clearFormData();
    webView.clearCache(true);
    Log.i(TAG, "Loading URL " + url);
    webView.loadUrl(url);
}
 
Example 6
Source File: TestCaseListActivity.java    From Android-Plugin-Framework with MIT License 6 votes vote down vote up
private void setUpWebViewSetting(WebView web) {
    WebSettings webSettings = web.getSettings();

    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);// 根据cache-control决定是否从网络上取数据
    webSettings.setSupportZoom(true);
    webSettings.setBuiltInZoomControls(true);// 显示放大缩小
    webSettings.setJavaScriptEnabled(true);
    // webSettings.setPluginsEnabled(true);
    webSettings.setPluginState(WebSettings.PluginState.ON);
    webSettings.setUserAgentString(webSettings.getUserAgentString());
    webSettings.setDomStorageEnabled(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setAppCachePath(getCacheDir().getPath());
    webSettings.setUseWideViewPort(true);// 影响默认满屏和双击缩放
    webSettings.setLoadWithOverviewMode(true);// 影响默认满屏和手势缩放

}
 
Example 7
Source File: PluginWebViewActivity.java    From Android-Plugin-Framework with MIT License 6 votes vote down vote up
private void setUpWebViewSetting() {
	WebSettings webSettings = web.getSettings();

	webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);// 根据cache-control决定是否从网络上取数据
	webSettings.setSupportZoom(true);
	webSettings.setBuiltInZoomControls(true);// 显示放大缩小
	webSettings.setJavaScriptEnabled(true);
	// webSettings.setPluginsEnabled(true);
	webSettings.setPluginState(PluginState.ON);
	webSettings.setUserAgentString(webSettings.getUserAgentString());
	webSettings.setDomStorageEnabled(true);
	webSettings.setAppCacheEnabled(true);
	webSettings.setAppCachePath(getCacheDir().getPath());
	webSettings.setUseWideViewPort(true);// 影响默认满屏和双击缩放
	webSettings.setLoadWithOverviewMode(true);// 影响默认满屏和手势缩放

}
 
Example 8
Source File: MainActivity.java    From chromium-webview-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Convenience method to set some generic defaults for a
 * given WebView
 *
 * @param webView
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setUpWebViewDefaults(WebView webView) {
    WebSettings settings = webView.getSettings();

    // Enable Javascript
    settings.setJavaScriptEnabled(true);

    // Use WideViewport and Zoom out if there is no viewport defined
    settings.setUseWideViewPort(true);
    settings.setLoadWithOverviewMode(true);

    // Enable pinch to zoom without the zoom buttons
    settings.setBuiltInZoomControls(true);

    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
        // Hide the zoom controls for HONEYCOMB+
        settings.setDisplayZoomControls(false);
    }

    // Enable remote debugging via chrome://inspect
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        WebView.setWebContentsDebuggingEnabled(true);
    }
}
 
Example 9
Source File: NestedWebView.java    From rexxar-android with MIT License 6 votes vote down vote up
public NestedWebView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mChildHelper = new NestedScrollingChildHelper(this);
    setNestedScrollingEnabled(true);
    final ViewConfiguration configuration = ViewConfiguration.get(getContext());
    mTouchSlop = configuration.getScaledTouchSlop();

    WebSettings webSettings = getSettings();
    webSettings.setJavaScriptEnabled(true);
    // 通过注入方法,优化横滑体验
    addJavascriptInterface(new NestScrollHelper(), "Android_NestScrollHelper");
    // 不过渡滑动
    setOverScrollMode(OVER_SCROLL_NEVER);
    mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
    mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
    mScroller = new OverScroller(getContext());
}
 
Example 10
Source File: GraphView.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
public WebView getView(String html) {
    if (BuildConfig.DEBUG) {
        WebView.setWebContentsDebuggingEnabled(true);
    }

    WebView webView = new GraphWebView(mContext);

    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);

    webView.setClickable(true);
    webView.setFocusable(false);
    webView.setFocusableInTouchMode(false);

    settings.setBuiltInZoomControls(mIsFullScreen);
    settings.setSupportZoom(mIsFullScreen);
    settings.setDisplayZoomControls(mIsFullScreen);

    // Improve performance
    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);

    this.myHTML = html;
    webView.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "utf-8", null);
    return webView;
}
 
Example 11
Source File: Web3View.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
public void init() {
    jsInjectorClient = new JsInjectorClient(getContext());
    webViewClient = new Web3ViewClient(jsInjectorClient, new UrlHandlerManager());
    WebSettings webSettings = getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setDisplayZoomControls(false);
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setDomStorageEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setUserAgentString(webSettings.getUserAgentString()
                                           + "AlphaWallet(Platform=Android&AppVersion=" + BuildConfig.VERSION_NAME + ")");
    WebView.setWebContentsDebuggingEnabled(true); //so devs can debug their scripts/pages
    addJavascriptInterface(new SignCallbackJSInterface(
            this,
            innerOnSignTransactionListener,
            innerOnSignMessageListener,
            innerOnSignPersonalMessageListener,
            innerOnSignTypedMessageListener), "alpha");
}
 
Example 12
Source File: Help.java    From haxsync with GNU General Public License v2.0 6 votes vote down vote up
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.help);
	WebView myWebView = (WebView) findViewById(R.id.webview);
	WebSettings webSettings = myWebView.getSettings();
	webSettings.setJavaScriptEnabled(true);
	myWebView.setWebViewClient(new WebViewClient() {  
		  @Override  
		  public boolean shouldOverrideUrlLoading(WebView view, String url)  
		  {  
		    view.loadUrl(url);
		    return true;
		  }  
		}); 
	myWebView.loadUrl("file:///android_asset/help/index.html");

	/*AlertDialog.Builder dialog = new AlertDialog.Builder(this);
	dialog.setMessage("Copyright (c) 2011 Mathias Roth. \n" +
			"Uses Code by Sam Steele (www.c99.org) licensed under the Apache Public license.");
	dialog.show();*/
}
 
Example 13
Source File: Web3View.java    From Web3View with GNU General Public License v3.0 6 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
private void init() {
    jsInjectorClient = new JsInjectorClient(getContext());
    webViewClient = new Web3ViewClient(jsInjectorClient, new UrlHandlerManager());
    WebSettings webSettings = super.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setDisplayZoomControls(false);
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setDomStorageEnabled(true);
    addJavascriptInterface(new SignCallbackJSInterface(
            this,
            innerOnSignTransactionListener,
            innerOnSignMessageListener,
            innerOnSignPersonalMessageListener,
            innerOnSignTypedMessageListener), "trust");

    super.setWebViewClient(webViewClient);
}
 
Example 14
Source File: WebViewFragment.java    From quill with MIT License 5 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
@NonNull @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    @LayoutRes int layoutId = getArguments().getInt(KEY_LAYOUT_ID);
    View view = inflater.inflate(layoutId, container, false);
    // not using ButterKnife to ensure WebView is private
    // but still need to call bindView() to maintain base class contract
    bindView(view);
    mWebView = (WebView) view.findViewById(R.id.web_view);
    mUrl = getArguments().getString(BundleKeys.URL);
    if (TextUtils.isEmpty(mUrl)) {
        throw new IllegalArgumentException("Empty URL passed to WebViewFragment!");
    }
    Crashlytics.log(Log.DEBUG, TAG, "Loading URL: " + mUrl);

    WebSettings settings = mWebView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setDomStorageEnabled(true);

    // enable remote debugging
    if (0 != (getActivity().getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE) &&
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        WebView.setWebContentsDebuggingEnabled(true);
    }

    mWebView.setWebViewClient(new DefaultWebViewClient());
    mWebView.loadUrl(mUrl);

    return view;
}
 
Example 15
Source File: JsEngine.java    From MaterialQQLite with Apache License 2.0 5 votes vote down vote up
public JsEngine(Context context) {
	m_context = context;
	m_webView = new WebView(m_context);
	m_jsObject = new JsObject();
	
	WebSettings setting = m_webView.getSettings();
	setting.setJavaScriptEnabled(true);
	
	m_webView.addJavascriptInterface(m_jsObject, "JsObject");
}
 
Example 16
Source File: WebViewCompatUtils.java    From rexxar-android with MIT License 5 votes vote down vote up
/**
 * fix bug: NPE in android.webkit.AccessibilityInjector$TextToSpeechWrapper (Android 4.2.1)
 * see: https://code.google.com/p/android/issues/detail?id=40944
 * 
 * @param context
 * @param webSettings
 */
public static void enableJavaScriptForWebView(Context context, WebSettings webSettings) {
    if (null == webSettings) {
        return;
    }
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
        disableAccessibility(context);
    }
    try {
        webSettings.setJavaScriptEnabled(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 17
Source File: HTML5WebView.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
private void init(Context context) {
	mContext = context;		
	Activity a = (Activity) mContext;
	
	mLayout = new FrameLayout(context);
	
	mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(a).inflate(R.layout.custom_screen, null);
	mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.main_content);
	mCustomViewContainer = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.fullscreen_custom_content);
	
	mLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);

	mWebChromeClient = new MyWebChromeClient();
    setWebChromeClient(mWebChromeClient);
    
    setWebViewClient(new MyWebViewClient());
       
    // Configure the webview
    WebSettings s = getSettings();
    s.setBuiltInZoomControls(true);
    s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    s.setUseWideViewPort(true);
    s.setLoadWithOverviewMode(true);
    s.setSavePassword(true);
    s.setSaveFormData(true);
    s.setJavaScriptEnabled(true);
    
    // enable navigator.geolocation 
    s.setGeolocationEnabled(true);
    s.setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/");
    
    // enable Web Storage: localStorage, sessionStorage
    s.setDomStorageEnabled(true);
    
    mContentView.addView(this);
}
 
Example 18
Source File: ArticleActivity.java    From WanAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * 配置WebView
 */
@SuppressLint("SetJavaScriptEnabled")
private void setSettings(WebSettings settings) {
    if (mPresenter.getNoImageState())
        settings.setBlockNetworkImage(true);
    else
        settings.setBlockNetworkImage(false);

    if(mPresenter.getAutoCacheState()){
        settings.setAppCacheEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setDatabaseEnabled(true);
        if(NetWorkUtil.isNetworkConnected(this))
            settings.setCacheMode(WebSettings.LOAD_DEFAULT);//(默认)根据cache-control决定是否从网络上取数据
        else
            settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//只要本地有,无论是否过期,或者no-cache,都使用缓存中的数据。
    }else {
        settings.setAppCacheEnabled(false);
        settings.setDatabaseEnabled(false);
        settings.setDatabaseEnabled(false);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不使用缓存,只从网络获取数据
    }

    //支持缩放
    settings.setJavaScriptEnabled(true);
    settings.setSupportZoom(true);
    settings.setBuiltInZoomControls(true);
    //不显示缩放按钮
    settings.setDisplayZoomControls(false);
    //设置自适应屏幕,两者合用
    //将图片调整到适合WebView的大小
    settings.setUseWideViewPort(true);
    //缩放至屏幕的大小
    settings.setLoadWithOverviewMode(true);
    //自适应屏幕
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
}
 
Example 19
Source File: MainActivity.java    From webTube with GNU General Public License v3.0 5 votes vote down vote up
public void setUpWebview() {
    // To save login info
    CookieHelper.acceptCookies(webView, true);

    // Some settings
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(false);
    webSettings.setAllowFileAccess(false);

    webSettings.setDatabaseEnabled(true);

    String cachePath = mApplicationContext
            .getDir("cache", Context.MODE_PRIVATE).getPath();
    webSettings.setAppCachePath(cachePath);
    webSettings.setAllowFileAccess(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

    webView.setHorizontalScrollBarEnabled(false);

    webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

    webView.setBackgroundColor(Color.WHITE);
    webView.setScrollbarFadingEnabled(true);
    webView.setNetworkAvailable(true);
}
 
Example 20
Source File: FeedBackActivity.java    From shinny-futures-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void initData() {
    mBinding = (ActivityFeedBackBinding) mViewDataBinding;
    WebSettings settings = mBinding.webView.getSettings();
    settings.setDomStorageEnabled(true);
    settings.setJavaScriptEnabled(true);  //设置WebView属性,运行执行js脚本
    settings.setUseWideViewPort(true);//将图片调整到适合webView的大小
    settings.setLoadWithOverviewMode(true);   //自适应屏幕
    settings.setBuiltInZoomControls(true);
    settings.setDisplayZoomControls(false);
    settings.setSupportZoom(true);//设定支持缩放
    settings.setDefaultTextEncodingName("utf-8");
    settings.setLoadsImagesAutomatically(true);
    mBinding.webView.loadUrl(CommonConstants.FEED_BACK_URL);//调用loadUrl方法为WebView加入链接
}