无法从UIDAI下载Android Webview下载

时间:2019-01-31 13:26:33

标签: android android-webview android-download-manager

  

我的Android应用程序中有一个webview。当用户转到webview并单击链接下载文件时,没有任何反应。

我正在尝试通过在UIDAI网站中输入aadhaar编号来下载offlineaadhaaar zip。但我无法下载该文件。请检查我的代码。我尝试使用下载列表器。但是没有调用下载监听器。

public class WebviewActivity extends AppCompatActivity {
     private WebView webView;
     private ProgressDialog dialog;
     private ProgressDialog progress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    webView=(WebView) findViewById(R.id.webView);

    webView.setInitialScale(0);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);

 webView.getSettings().setSupportMultipleWindows(true);   
 webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(false);
    webView.getSettings().setDomStorageEnabled(true);
    if (Build.VERSION.SDK_INT >= 19) {
        webView.setWebContentsDebuggingEnabled(false);
    }
    if (Build.VERSION.SDK_INT >= 26) {
        webView.getSettings().setMixedContentMode(0);
    }

    webView.clearCache(true);
    webView.clearHistory();
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setScrollbarFadingEnabled(false);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl("https://resident.uidai.gov.in/offlineaadhaar");

    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            if (newProgress > 0) {
                showProgressMessageDialog("Please Wait");
            }
            if (newProgress >= 100) {
                hideProgressMessageDialog();
            }
        }
    });

    webView.setDownloadListener(new DownloadListener()
    {

        @Override
        public void onDownloadStart(String url, String 
     userAgent,String contentDisposition, String mimeType,long 
    contentLength) {

            DownloadManager.Request request = new 
      DownloadManager.Request(Uri.parse(url));
            request.setMimeType(mimeType);
            String cookies = 
       CookieManager.getInstance().getCookie(url);
            request.addRequestHeader("cookie", cookies);
            request.addRequestHeader("User-Agent", userAgent);
            request.setDescription("Downloading file...");
            request.setTitle(URLUtil.guessFileName(url, 
             contentDisposition,mimeType));
            request.allowScanningByMediaScanner();






    request.setNotificationVisibility
    (DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);


request.setDestinationInExternalFilesDir
        (WebviewActivity.this,Environment. 
   DIRECTORY_DOWNLOADS,".zip");
            DownloadManager dm = (DownloadManager) 
getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
            Toast.makeText(getApplicationContext(), "Downloading 
 File",
                    Toast.LENGTH_LONG).show();
          }});
   }

    public void showProgressMessageDialog(final String msg) {

    runOnUiThread(new Runnable() {
        public void run() {
            if (progress == null || !progress.isShowing()) {
                progress = ProgressDialog.show(WebviewActivity.this, 
        "Loading...", msg);
            }
        }
        });
   }

    public void hideProgressMessageDialog() {
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            try {
                if (progress.isShowing())
                    progress.dismiss();
            } catch (Throwable e) {

            }
          }
       });
   }


}

0 个答案:

没有答案