简体   繁体   中英

PDF in WebView opens browser automatically

Hi everyone i want to display a pdf in my activity using a webview so i did:

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.lasergroup.ami.utilities.PDFActivity">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/wv_PDF"/>

</RelativeLayout>

JAVA

WebView wv = (WebView)findViewById(R.id.wv_PDF);
String pdfURL = "http://www.randomurl.com/randompdf.pdf"
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginState(WebSettings.PluginState.ON);
wv.loadUrl("http://docs.google.com/gview?embedded=true&url="+pdfURL);

This worked well on emulator but when i use it on my device (LG G3 Android 5.0 API 21) it opens the browser (Chrome) automatically after wv.loadUrl. This is a very strange issue, so i want to understand if it's a code problem or a device one, and i want to know if someone can tell me why this happens. I haven't any intent to open the browser so i can't understand what happens (only on some devices).

You should add WebViewClient before call loadUrl

wv.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM