1

我尝试使用另一个应用程序在 Xwalkview 中打开 Web PDF,但文件只会被下载。

用例是用于 Kiosk-Devices 的 WebApp,用于显示带有 URL 白名单的 Intranet 内容(HTML 和 PDF)。

这是我的代码:

public class MainActivity extends Activity {
XWalkView mXwalkView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mXwalkView = (XWalkView) findViewById(R.id.mybrowser);
    mXwalkView.setResourceClient(new MyResourceClient(mXwalkView){

        @Override
        public XWalkWebResourceResponse shouldInterceptLoadRequest(
                XWalkView view, XWalkWebResourceRequest request) {
            if(request.getUrl().getHost().endsWith("example.company.com")) return null;
            if(request.getUrl().getHost().startsWith("data://")) return null;
            if(request.getUrl().getHost().startsWith("javascript")) return null;
            if(request.getUrl().getHost().endsWith(".pdf"))


                {Intent pdfintent = new Intent(Intent.ACTION_VIEW);
                pdfintent.setComponent(new ComponentName("org.ninthfloor.copperpdf", "co.copperhead.pdfviewer.PdfViewer"));
                startActivity(pdfintent);}

            else;
            {
                ByteArrayInputStream bis =
                        new ByteArrayInputStream(
                                "Website not allowed!".getBytes());
                return createXWalkWebResourceResponse("text/plain", "UTF-8", bis);
            }
        }

    });
    mXwalkView.setUIClient(new MyUIClient(mXwalkView));
    mXwalkView.load("http://example.company.com", null);
    mXwalkView.setUserAgentString("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML,"
            + " like Gecko) Chrome/41.0.2228.0 Safari/537.36");
    mXwalkView.getSettings().setUseWideViewPort(false);
    mXwalkView.getSettings().setCacheMode(2);
    mXwalkView.setInitialScale(100);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    SystemClock.sleep(5000);
}
4

0 回答 0