我在网络视图中加载了网址,但是单击下载按钮时,下载功能在网络视图中不起作用



我在应用程序的 Web 视图中加载了 url,但单击下载按钮时下载功能在 Web 视图中不起作用,没有任何反应,请给我解决方案。

Public class FinancialActivity extends AppCompatActivity {
 WebView webview;
 public static final String SHARED_PREF_NAME = "myloginapp";
 SharedPreferences sharedPreferences;
 String id, studid, degree, user_type;
 String url;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_financial);
  sharedPreferences = FinancialActivity.this.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
  studid = sharedPreferences.getString(LoginActivity.ID_SHARED_PREF, "Not Available");
  degree = sharedPreferences.getString(LoginActivity.KEY_DEGREE_SOUGHT, "Not Available");
  user_type = sharedPreferences.getString("user_type", "Not Available");
  url = Urlinfo.Financial + "stud_id=" + studid + "&user_type=" + user_type + "&subscription_status=" + DrawerActivity.subvalue + "&stud_degree=" + degree;
  Log.d("urlapplication", "" + url);
  WebView w = new WebView(this);
  w.setWebViewClient(new WebViewClient() {
   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return true;
   }
  });
   w.setDownloadListener(new DownloadListener() {
   public void onDownloadStart(String url, String userAgent,
    String contentDisposition, String mimetype,
    long contentLength) {
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
   }
  });
  setContentView(w);
  w.loadUrl(url);    
 }
}
}

在这里试试下面的代码

   @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    // handle different requests for different type of files
                    // this example handles downloads requests for .apk and .mp3 files
                    // everything else the webview can handle normally
                    if (url.endsWith(".apk")) {  //change .apk to any format you want to download
                        Uri source = Uri.parse(url);
                        // Make a new request pointing to the .apk url
                        DownloadManager.Request request = new DownloadManager.Request(source);
                        // appears the same in Notification bar while downloading
                        request.setDescription("Description for the DownloadManager Bar");
                        request.setTitle("YourApp.apk");
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                            request.allowScanningByMediaScanner();
                            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                        }
                        // save the file in the "Downloads" folder of SDCARD
                        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "SmartPigs.apk");
                        // get download service and enqueue file
                        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                        manager.enqueue(request);
                    }
                    else if(url.endsWith(".mp3")) {
                        // if the link points to an .mp3 resource do something else
                    }
                    // if there is a link to anything else than .apk or .mp3 load the URL in the webview
                    else view.loadUrl(url);
                    return true;                
            }

希望这有帮助

最新更新