Java Code Examples for android.webkit.WebView#setWebChromeClient()

The following examples show how to use android.webkit.WebView#setWebChromeClient() . 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: TestCaseListActivity.java    From Android-Plugin-Framework with MIT License 6 votes vote down vote up
private void setClient(WebView web) {

        web.setWebChromeClient(new WebChromeClient() {
        });

        // 如果要自动唤起自定义的scheme,不能设置WebViewClient,
        // 否则,需要在shouldOverrideUrlLoading中自行处理自定义scheme
        // webView.setWebViewClient();
        web.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

        });
    }
 
Example 2
Source File: RideRequestView.java    From rides-android-sdk with MIT License 6 votes vote down vote up
/**
 * Initialize the layout, properties, and inner web view.
 */
private void init(@NonNull Context context) {
    inflate(getContext(), R.layout.ub__ride_request_view, this);
    webView = (WebView) findViewById(R.id.ub__ride_request_webview);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setGeolocationEnabled(true);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    webView.setWebChromeClient(new RideRequestWebChromeClient());
    webView.setWebViewClient(new RideRequestWebViewClient(new RideRequestWebViewClientCallback() {
        @Override
        public void onErrorParsed(@NonNull RideRequestViewError error) {
            if (rideRequestViewCallback != null) {
                rideRequestViewCallback.onErrorReceived(error);
            }
        }
    }));
}
 
Example 3
Source File: WebViewPresenter.java    From gank with GNU General Public License v3.0 5 votes vote down vote up
public void setWebViewSettings(WebView webView, String url) {
    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setLoadWithOverviewMode(true);
    settings.setAppCacheEnabled(true);
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    settings.setSupportZoom(true);
    webView.setWebChromeClient(new ChromeClient());
    webView.setWebViewClient(new GankClient());
    webView.loadUrl(url);
}
 
Example 4
Source File: MainActivity.java    From chromium-webview-samples with Apache License 2.0 5 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    mWebRTCWebView = (WebView) rootView.findViewById(R.id.fragment_main_webview);

    setUpWebViewDefaults(mWebRTCWebView);

    mWebRTCWebView.loadUrl("https://apprtc-m.appspot.com/");

    mWebRTCWebView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onPermissionRequest(final PermissionRequest request) {
            Log.d(TAG, "onPermissionRequest");
            getActivity().runOnUiThread(new Runnable() {
                @TargetApi(Build.VERSION_CODES.L)
                @Override
                public void run() {
                    if(request.getOrigin().toString().equals("https://apprtc-m.appspot.com/")) {
                        request.grant(request.getResources());
                    } else {
                        request.deny();
                    }
                }
            });
        }

    });

    return rootView;
}
 
Example 5
Source File: WebViewActivity.java    From LRecyclerView with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);


    final PullScrollView refreshLayout = (PullScrollView) findViewById(R.id.refresh_layout);
    final WebView webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient());
    webView.setWebViewClient(new WebViewClient());
    //设置头部加载颜色
    refreshLayout.setHeaderViewColor(R.color.colorAccent, R.color.dark ,android.R.color.white);
    refreshLayout.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
    refreshLayout.setRefreshListener(new PullScrollView.RefreshListener() {
        @Override
        public void onRefresh() {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    webView.loadUrl("http://www.stay4it.com");
                    refreshLayout.setRefreshCompleted();
                }
            }, 1000);
        }
    });

    refreshLayout.refreshWithPull();
}
 
Example 6
Source File: ContentFragment.java    From Study_Android_Demo with Apache License 2.0 5 votes vote down vote up
private void setupView(View view) {
    webView = (WebView) view.findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    //监听
    //没有该监听,app没有什么问题
    webView.setWebChromeClient(new WebChromeClient());
    //没有该监听,app空白。还直接跳到手机浏览器访问你要访问的地址
    webView.setWebViewClient(new WebViewClient());
}
 
Example 7
Source File: UConfigActivity.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
  CookieManager cookieManager = CookieManager.getInstance();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    cookieManager.flush();
    cookieManager.removeAllCookies(null);
    cookieManager.removeSessionCookies(null);
  } else {
    CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
    cookieSyncManager.startSync();
    cookieManager.removeAllCookie();
    cookieManager.removeSessionCookie();
    cookieSyncManager.stopSync();
  }

  // Copy cookies from okhttp cookie store to CookieManager
  url = EhUrl.getUConfigUrl();
  EhCookieStore store = EhApplication.getEhCookieStore(this);
  for (Cookie cookie : store.getCookies(HttpUrl.parse(url))) {
    cookieManager.setCookie(url, cookie.toString());
  }

  setContentView(R.layout.activity_u_config);
  setNavigationIcon(R.drawable.v_arrow_left_dark_x24);
  webView = (WebView) findViewById(R.id.webview);
  webView.getSettings().setJavaScriptEnabled(true);
  webView.setWebViewClient(new UConfigWebViewClient());
  webView.setWebChromeClient(new DialogWebChromeClient(this));
  webView.loadUrl(url);
  progress = (ProgressView) findViewById(R.id.progress);

  Snackbar.make(webView, R.string.apply_tip, Snackbar.LENGTH_LONG).show();
}
 
Example 8
Source File: WebViewActivity.java    From Pas with Apache License 2.0 5 votes vote down vote up
private void initWebView() {

        url = getIntent().getStringExtra(URL);

        webView = new WebView(getApplicationContext());

        iView.rlMain.addView(webView);


        blockLoadingNetworkImage = true;

        webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webView.getSettings().setBlockNetworkImage(true);

        webView.setWebViewClient(new webViewClient());

        //如果访问的页面中有Javascript,则webview必须设置支持Javascript
        webView.setWebChromeClient(
                new WebChromeClient() {
                    @Override
                    public void onProgressChanged(WebView view, int newProgress) {
                        super.onProgressChanged(view, newProgress);
                        if (newProgress == 100) {
                            iView.flLoading.setVisibility(View.GONE);

                            if (blockLoadingNetworkImage) {
                                webView.getSettings().setBlockNetworkImage(false);
                                blockLoadingNetworkImage = false;
                            }
                        }
                    }
                });

        webView.loadUrl(url);
    }
 
Example 9
Source File: WebFragment.java    From Rabbits with Apache License 2.0 5 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mWebView = (WebView) LayoutInflater.from(getActivity()).inflate(R.layout.fragment_web, container, false);
    mWebView.setWebViewClient(new DefaultWebViewClient());
    mWebView.setWebChromeClient(new WebChromeClient());
    mWebView.getSettings().setUseWideViewPort(false);
    mWebView.getSettings().setJavaScriptEnabled(true);
    return mWebView;
}
 
Example 10
Source File: AdBlocksWebViewActivity.java    From AdBlockedWebView-Android with MIT License 5 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
private void bindView() {
    // Toolbar
    mTvTitle = (TextView) findViewById(R.id.toolbar_tv_title);
    mTvUrl = (TextView) findViewById(R.id.toolbar_tv_url);
    findViewById(R.id.toolbar_root).setBackgroundColor(getIntent().getIntExtra(EXTRA_COLOR, Color.BLACK));

    mCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.a_web_viewer_coordinatorlayout);
    mProgressBar = (ProgressBar) findViewById(R.id.a_web_viewer_pb);
    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.a_web_viewer_srl);
    mWebView = (WebView) findViewById(R.id.a_web_viewer_wv);
    mSwipeRefreshLayout.setOnRefreshListener(this);

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        webSettings.setDisplayZoomControls(false);
    }
    webSettings.setBuiltInZoomControls(true);
    webSettings.setSupportZoom(true);
    webSettings.setDomStorageEnabled(true);

    mWebView.setWebChromeClient(new MyWebChromeClient());
    mWebView.setWebViewClient(new MyWebViewClient());
    mWebView.setDownloadListener(this);
    mWebView.setOnCreateContextMenuListener(this);

    mBtnMore = (AppCompatImageButton) findViewById(R.id.toolbar_btn_more);

    //noinspection ConstantConditions
    findViewById(R.id.toolbar_btn_close).setOnClickListener(this);
    //noinspection ConstantConditions
    mBtnMore.setOnClickListener(this);

    // PopupWindow
    initPopupMenu();
}
 
Example 11
Source File: Utils.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
public static void displayArticle(WebView webView, Article article, Context context) {
    String styles = "iframe, img { width: 100%; }";
    if (isDarkTheme(context)) {
        webView.setBackgroundColor(Color.BLACK);
        styles += "body { background-color: #000000; color: #F6F6F6; } a { color: #0099FF; }";
    }
    String html = String.format("<html><head><meta charset=\"utf-8\"><link rel=\"stylesheet\" type=\"text/css\" href=\"http://cdn.uservoice.com/stylesheets/vendor/typeset.css\"/><style>%s</style></head><body class=\"typeset\" style=\"font-family: sans-serif; margin: 1em\"><h3>%s</h3>%s</body></html>", styles, article.getTitle(), article.getHtml());
    webView.setWebChromeClient(new WebChromeClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginState(PluginState.ON);
    webView.loadUrl(String.format("data:text/html;charset=utf-8,%s", Uri.encode(html)));
}
 
Example 12
Source File: WebFragment.java    From Android_framework with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void initView() {
    webView = (WebView) mViewContainer;
    webView.setWebViewClient(webViewClient);
    webView.setWebChromeClient(chromeClient);

    WebSettings settings = webView.getSettings();
    //设置网页大小自适应
    settings.setUseWideViewPort(true);
    settings.setLoadWithOverviewMode(true);
    //支持js
    settings.setJavaScriptEnabled(true);
}
 
Example 13
Source File: Instructions.java    From augmentedreality with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.instructions_layout);
	mWebView = (WebView) findViewById(R.id.instructions_webview);
	
	WebSettings webSettings = mWebView.getSettings();
       webSettings.setSupportZoom(true);
       webSettings.setBuiltInZoomControls(true);
       
       WebChromeClient client = new WebChromeClient();
       mWebView.setWebChromeClient(client);
               
	mWebView.loadUrl("file:///android_asset/help/"+getResources().getString(R.string.help_file));
}
 
Example 14
Source File: WebViewActivity.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    Intent intent = getIntent();
    if (intent == null) {
        finish();
        return;
    }

    String url = intent.getStringExtra(EXTRA_URL);
    if (url == null) {
        finish();
        return;
    }

    String title = intent.getStringExtra(EXTRA_TITLE);
    if (title != null) {
        setTitle(title);
    }

    boolean ssl = intent.getBooleanExtra(EXTRA_SSL, false);
    mSSL = ssl ? SSL_ON : SSL_OFF;

    mWebView = (WebView) findViewById(R.id.activity_web_view);
    if (mWebView != null) {
        mWebView.setWebViewClient(mWebViewClient);
        mWebView.setWebChromeClient(mChromeClient);
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.addJavascriptInterface(new JavaScriptInterface(), "Android");

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(false);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

        // Android 4.0以上では、chromeからデバッグするための機能が存在する
        // ここでは、DEBUGビルドの場合のみ使えるように設定している。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (DEBUG) {
                if (0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE)) {
                    WebView.setWebContentsDebuggingEnabled(true);
                }
            }
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            webSettings.setAllowFileAccessFromFileURLs(true);
            webSettings.setAllowUniversalAccessFromFileURLs(true);
        }

        loadUrl(mWebView, url);
    }
}
 
Example 15
Source File: MobclickAgentJSInterface.java    From letv with Apache License 2.0 4 votes vote down vote up
public MobclickAgentJSInterface(Context context, WebView webView, WebChromeClient webChromeClient) {
    this.a = context;
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new a(this, webChromeClient));
}
 
Example 16
Source File: WebViewFacebook.java    From aptoide-client with GNU General Public License v2.0 4 votes vote down vote up
@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//Aptoide.getThemePicker().setAptoideTheme(this);
		setContentView(R.layout.webview_social);

		Bundle parameters = new Bundle();
//		parameters.putString("client_id", APP_ID);
		parameters.putString("type", "user_agent");
		parameters.putString("redirect_uri", REDIRECT_URI);
		url = OAUTH_ENDPOINT + "?" + encodeUrl(parameters);
//		Log.d(this.getClass().getName(), "url: " + url);

		waitingText = (TextView) findViewById(R.id.waiting_text);
		waitingProgress = (ProgressBar) findViewById(R.id.waiting_bar);

		webView = (WebView) findViewById(R.id.webview);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.setVerticalScrollBarEnabled(false);
		webView.setHorizontalScrollBarEnabled(false);
		webView.setWebViewClient(new FbWebViewClient());

		webView.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				setProgress(progress * 100);

				if (progress == 100) {
					waitingText.setVisibility(View.GONE);
					waitingProgress.setVisibility(View.GONE);
				}
			}
		});

		webView.loadUrl(url);

		System.out.println("Debug: WebViewActionBar: " + getSupportActionBar());

		Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
		mToolbar.setCollapsible(false);

		setSupportActionBar(mToolbar);

		if (getSupportActionBar() != null) {
			getSupportActionBar().setTitle("");
			getSupportActionBar().setHomeButtonEnabled(true);
			getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		}

    }
 
Example 17
Source File: WebViewFragment.java    From Android-PullToNextLayout with Apache License 2.0 4 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    index=  getArguments().getInt("index");

    View   v= inflater.inflate(R.layout.fragment_web_view, container, false);

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

    String[]  urls=getResources().getStringArray(R.array.urls);

    webView.loadUrl(urls[index]);
    webView.setWebChromeClient(new WebChromeClient());

    return  v;
}
 
Example 18
Source File: WebVideoPresenter.java    From Gank.io with GNU General Public License v3.0 4 votes vote down vote up
public void loadWebVideo(WebView webView, String url) {
  webView.setWebChromeClient(new Chrome());
  webView.loadUrl(url);
}
 
Example 19
Source File: ViewWebsiteFragment.java    From VyAPI with MIT License 4 votes vote down vote up
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.view_website_fragment,container,false);
        wv_personal_website = (WebView) view.findViewById(R.id.wv_personal_website);

        Bundle bundle = getArguments();
        String URL = bundle.getString("URL");

        // Check if URL begins with http
        if (!URL.matches("^(http|https)://.*$")){
            URL = "http://" + URL;
        }

        wv_personal_website.setWebViewClient(new WebViewClient());
        webSettings = wv_personal_website.getSettings();
        webSettings.setJavaScriptEnabled(true);
        wv_personal_website.setWebChromeClient(new WebChromeClient());

//        wv_personal_website.getSettings().setJavaScriptEnabled(true);
//        wv_personal_website.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
//        wv_personal_website.canGoBack();
//        wv_personal_website.goBack();

        // If URL is valid, open it
        if (URLUtil.isValidUrl(URL)){
            wv_personal_website.loadUrl(URL);
        }
        // If URL is invalid
        else {
            // Show a custom display_data_from_db webpage
            String htmlData ="<html><body><div align=\"center\" ><h2> Error loading URL: </h2><p>The link you are trying to access seems to be invalid. Please correct the URL and try again. </p><span style=\"color:red\"><h3><br/>" + URL + "</h3></span></div></body>";

            wv_personal_website.loadUrl("about:blank");
            wv_personal_website.loadDataWithBaseURL(null,htmlData, "text/html", "UTF-8",null);
            wv_personal_website.invalidate();
        }

        wv_personal_website.setWebViewClient(new WebViewClient());

        return view;
    }
 
Example 20
Source File: AuthActivity.java    From android-atleap with Apache License 2.0 2 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().requestFeature(Window.FEATURE_PROGRESS);
    getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

    super.onCreate(savedInstanceState);

    if(TextUtils.isEmpty(getString(R.string.github_oauth_client_id)) || TextUtils.isEmpty(getString(R.string.github_oauth_client_secret))) {
        throw new IllegalStateException("Please specify github_oauth_client_id and github_oauth_client_secret in file ./src/main/res/values/settings.xml");
    }


    setContentView(R.layout.activity_auth);

    mWebView = (WebView)findViewById(R.id.web_view);



    mWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            AuthActivity.this.setProgress(newProgress * 1000);
        }
    });

    mWebView.setWebViewClient(new AuthWebViewClient());

    CookieSyncManager.createInstance(this);

    mState = UUID.randomUUID().toString();


    mFullAuthorizeUrl = Uri.parse(AUTHORIZE_URL).buildUpon()
            .appendQueryParameter("client_id", getString(R.string.github_oauth_client_id))
            .appendQueryParameter("redirect_uri", getString(R.string.github_oauth_client_callback_url))
            .appendQueryParameter("scope", getString(R.string.github_oauth_scope))
            .appendQueryParameter("state", mState)
            .build()
            .toString();


    Log.v(TAG, "Staring authorization page");
    mWebView.loadUrl(mFullAuthorizeUrl);
}