Webview App内部链接在android 23以上无法正常工作

时间:2018-07-04 13:07:23

标签: android webview

我正在制作android webview应用。而且我几乎做到了,顺便说一句,它在android 23以上无法正常工作。在网站上,有许多链接覆盖有图像等。在摩托罗拉(android 21)中,正如我测试过的,一切正常。但是在模拟器(Nexus 5x-android 24)中,滚动正常,但链接无效。 我的webview客户端代码如下:

plot(rnorm(100), main = paste(
  strwrap(
    'This is a very long title wrapped on multiple lines without the need to adjust it by hand',
    whitespace_only = TRUE,
    width = 50
  ),
  collapse = "\n"
))

我的Web视图设置部分如下:

public class CustomWebViewClient extends WebViewClient {

    private boolean mSuccess = true;

    private Map<String, String> reloadMap = new HashMap<>();

    public CustomWebViewClient(){
    }

    public void addToReloadMap(String from, String to) {
        reloadMap.put(from, to);
    }



    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        progress_view.setVisibility(View.VISIBLE);
        swipeRefreshLayout.setRefreshing(false); // TODO: called too often, find a better place
    }

    @Override
    public void onPageFinished(final WebView view, final String url) {
        progress_view.setVisibility(View.GONE);
        if (url.contains(C.BRING_BACK_LOGIN) && PrefsUtil.getBringBack()) {
            removeAllCookies();
            PrefsUtil.setBringBack(false);
            showLoginActivity();
        }

        if (reloadRound < 10) {

            if (url.contains(C.WEBVIEW_FACEBOOK_URL)) {
                if (PrefsUtil.get(FacebookProfile.class) != null) {
                    FacebookLoginService.INSTANCE.fbProfile().onNext(PrefsUtil.get(FacebookProfile.class));
                }
                removeAllCookies();
                showLoginActivity();
            }
            if (PrefsUtil.getRegisterCheck() && !url.contains(C.FACEBOOK_REGISTER_URL) && !url.contains(C.CONTAINER_URL) && !url.contains(C.REGISTER_URL_THREE)
                    && !url.contains(C.WEBVIEW_FACEBOOK_URL) && !url.contains(C.STANDARD_LOGIN_URL) && !PrefsUtil.isLoggedIn(PrefsUtil.getCity().name()) && !url.contains(C.REGISTER_URL_FOUR) && !url.contains(C.REGISTER_URL)) {
                if (!url.contains(C.LOGIN_SUCCESS_URL)) {
                    removeAllCookies();
                    showLoginActivity();
                } else {
                    UserLoginService.INSTANCE.getUserLoginUrlSubj().onNext(url);
                    Map<String, String> cookieMap = new HashMap<>();
                    cookieMap.put(C.USER_LOGGIN_KEY, C.USER_LOGGIN_VALUE);
                    PrefsUtil.saveUserCookies(cookieMap, PrefsUtil.getCity().name());
                }
            }
            if (PrefsUtil.getCityChange() && !PrefsUtil.isLoggedIn(PrefsUtil.getCity().name())) {
                showLoginActivity();
            }

            if (PrefsUtil.isLoggedIn(PrefsUtil.getCity().name()) && url.contains(C.LOGIN_URL)) {
                PrefsUtil.setLastUrl(url);
            }

            runTaskCallback(new Runnable() {
                public void run() {

                    if (mSuccess) {
                        showContent(3000); 

                    }

                    if (reloadMap.containsKey(url) || url.contains(FB_API_KEY)) {

                        final Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                reloadPage();
                            }
                        }, 1800);

                    }

                    Log.i(TAG, "onPageFinished: " + url);

                }
            });

            progress_view.setVisibility(View.GONE);
            if (PrefsUtil.getFlag() && !PrefsUtil.getPermission()) {
                startService(new Intent(MainActivity.this, PermissionDetectService.class));
            }
        }
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        if (failingUrl.contains("#")) {
            Log.v("LOG", "failing url:"+ failingUrl);
            final int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
            if (sdkVersion > Build.VERSION_CODES.GINGERBREAD) {
                String[] temp;
                temp = failingUrl.split("#");
                view.loadUrl(temp[0]); // load page without internal link

                try {
                    Thread.sleep(400);
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }
            }

            view.loadUrl(failingUrl);  // try again
        } else {
            view.loadUrl(failingUrl);
        }        }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        runTaskCallback(new Runnable() {
            @Override
            public void run() {
                mSuccess=false;
                if (NetworkManager.isOnline(MainActivity.this)) {
                    webView.reload();
                } else {
                    showOffline();
                }
            }
        });
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

        if (DownloadUtility.isDownloadableFile(url)) {
            Toast.makeText(MainActivity.this, R.string.fragment_main_downloading, Toast.LENGTH_LONG).show();

            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

                return false;
            }

            if (!PrefsUtil.getDownloadUrl().equals(url)) {
                PrefsUtil.saveDownloadUrl(url);
                DownloadUtility.downloadFile(MainActivity.this, url, DownloadUtility.getFileName(url));
            }
            return true;

        } else if (UrlUtil.isFBEvent(url)) {

            try {
                getPackageManager().getPackageInfo("com.facebook.katana", 0);
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(UrlUtil.getFBUrl(url))));
            } catch (Exception e) {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                Crashlytics.logException(e);
            }

            return true;
        } else if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
            return true;
        } else if (url != null && url.startsWith("mailto:")) {
            MailTo mailTo = MailTo.parse(url);
            startEmailActivity(mailTo.getTo(), mailTo.getSubject(), mailTo.getBody());
            return true;
        } else if (url != null && url.startsWith("tel:")) {
            startCallActivity(url);
            return true;
        } else if (url != null && url.startsWith("sms:")) {
            startSmsActivity(url);
            return true;
        } else if (url != null && url.startsWith("geo:")) {
            startMapSearchActivity(url);
            return true;
        } else {
            return false;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail) {
        if (!detail.didCrash()) {

            Log.e("MY_APP_TAG", "System killed the WebView rendering process " +
                    "to reclaim memory. Recreating...");

            if (webView != null) {
                ViewGroup webViewContainer =
                        (ViewGroup) findViewById(R.id.fragment_main_webview);
                webViewContainer.removeView(webView);
                webView.destroy();
                webView = null;
            }


            return true; // The app continues executing.
        }


        Log.e("MY_APP_TAG", "The WebView rendering process crashed!");


    }
}

正如我所说,应用程序在android 21-motorola上运行良好。我看了一些答案,但没有任何适当的答案。我尝试了很多次,但应用程序在android 23以上无法正常工作。我该如何解决这个问题?

0 个答案:

没有答案