【问题标题】:Hide Print / download options for pdf in Webview using Google docs使用 Google 文档在 Webview 中隐藏 pdf 的打印/下载选项
【发布时间】:2017-02-22 04:52:35
【问题描述】:

我想在使用 Google 文档在 webview 中查看 pdf 链接时隐藏 pdf 下载和打印选项。有没有什么想法可以实现。这个我试过了。

mWebView.setWebViewClient(new Callback());

                String urlEncoded = null;
                try {
                    urlEncoded = URLEncoder.encode(url, "UTF-8");
                    url = "http://docs.google.com/viewer?url=" + urlEncoded;

                    Log.d("BrowserACtivity", "doc: "+url);
                    mWebView.loadUrl( url);                   
                } catch (Exception e) {
                    Log.d("BrowserActivity", "Exc: "+e.toString());                
                }

 private class Callback extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return(false);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            try {
                mWebView.loadUrl("javascript:(function() { " +
                        "document.getElementsByClassName('ndfHFb-c4YZDc-GSQQnc-LgbsSe ndfHFb-c4YZDc-to915-LgbsSe VIpgJd-TzA9Ye-eEGnhe ndfHFb-c4YZDc-LgbsSe')[0].style.display='none'; })()");
            } catch (Exception e) {
                Log.d("BrowserActivity", "onPageFinished -- Exc: " + e.toString());
            }
        }
    } 

但我无法隐藏它。你能建议我任何想法吗?提前致谢。

【问题讨论】:

  • 是否可以分享文档网址

标签: android android-webview


【解决方案1】:

请试试这个示例代码,它对我来说工作正常,它禁用按钮下载。

    public class PDFViewActivity extends AppCompatActivity {

        WebView webView;
        Bundle b;
        String str_file_name = "",str_url = "";
        private ProgressDialog mProg;

        Toolbar toolbar;
        int MyDeviceAPI= Build.VERSION.SDK_INT;
        Drawable upArrow;


        @Override
        public boolean onSupportNavigateUp(){
            finish();
            return true;
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_pdfview);

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

            toolbar = (Toolbar) findViewById(R.id.toolbar_pdf_view);
            setSupportActionBar(toolbar);

            getSupportActionBar().setTitle(getResources().getString(R.string.pdf_view));

            if(MyDeviceAPI>=21) {
                upArrow = getResources().getDrawable(R.mipmap.back_arrow,null);
            }
            else
            {
                upArrow = getResources().getDrawable(R.mipmap.back_arrow);
            }

            if(MyDeviceAPI>=23)
            {
                upArrow.setColorFilter(getResources().getColor(R.color.color_white,null), PorterDuff.Mode.SRC_ATOP);
            }
            else
            {
                upArrow.setColorFilter(getResources().getColor(R.color.color_white), PorterDuff.Mode.SRC_ATOP);
            }

            getSupportActionBar().setHomeAsUpIndicator(upArrow);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);


            if(MyDeviceAPI>=23)
            {
                toolbar.setTitleTextColor(getResources().getColor(R.color.color_white,null));
            }
            else
            {
                toolbar.setTitleTextColor(getResources().getColor(R.color.color_white));
            }

            mProg = MyProgressDailog.showProgressDialog(PDFViewActivity.this, getResources().getString(R.string.please_wait));

            b = getIntent().getExtras();
            if(b != null)
            {
                str_file_name = b.getString("FileName");
                str_url = "http://docs.google.com/gview?embedded=true&url="+ Utils.pdf_path+str_file_name;
                webView.setWebViewClient(new MyBrowser());
                webView.getSettings().setBuiltInZoomControls(false);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.getSettings().setSupportZoom(true);
                webView.loadUrl(str_url);
            }
        }

        private class MyBrowser extends WebViewClient {

            @Override
            public void onUnhandledKeyEvent(WebView view, KeyEvent ke) {

                Log.e("Unhandled Key Event",ke.toString());
                mProg.cancel();

            }

            @Override
            public void onPageFinished(WebView view, String url) {

                mProg.cancel();
                super.onPageFinished(view, url);
                webView.loadUrl("javascript:(function() { " +
                        "document.getElementsByClassName('ndfHFb-c4YZDc-GSQQnc-LgbsSe ndfHFb-c4YZDc-to915-LgbsSe VIpgJd-TzA9Ye-eEGnhe ndfHFb-c4YZDc-LgbsSe')[0].style.display='none'; })()");
            }

        }
    }

【讨论】:

    【解决方案2】:
    // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
    
        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());
    
        // startWebView("http://google.com");
    
        mWebView.setDownloadListener(new DownloadListener() {
    
            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimetype,
                                        long contentLength) {
                DownloadManager.Request request = new DownloadManager.Request(
                        Uri.parse(url));
                String mimeType = null;
                request.setMimeType(mimeType);
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,  URLUtil.guessFileName(url, contentDisposition,mimeType));
                DownloadManager dm = (DownloadManager) getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
                dm.enqueue(request);
                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
                intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
                intent.setType("*/*");//any application,any extension
                Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
                        Toast.LENGTH_LONG).show();
    
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 2020-12-09
      相关资源
      最近更新 更多