2017-02-22 57 views
0

我想在使用Google文檔在webview中查看PDF鏈接時隱藏pdf下載和打印選項。有沒有想法實現它。我試過這個。隱藏使用Google文檔的Webview中的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()); 
      } 
     } 
    } 

但我無法隱瞞它。你能告訴我任何想法。提前致謝。

+0

是可以共享文檔的URL – jaffar

回答

0

請嘗試此示例代碼,它爲我工作正常,它禁用按鈕下載。

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'; })()"); 
      } 

     } 
    } 
0
// 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(); 

     } 
    }); 
相關問題