我的应用程序是关于带有recycleview的列表,您可以在其中选择文件并单击下载按钮,然后打开一个webview,开始下载该文件。
我的应用程序适合我,但我注意到有些用户无法下载这些文件,而是使用他们拥有的默认媒体播放器自动打开。无论如何,当我能阻止它时?请查看webview类的代码:
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webview);
webView.setHorizontalScrollBarEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new webviewclient());
// ALLOW DOWNLOAD
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.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
final String filename= URLUtil.guessFileName(url, contentDisposition, mimetype);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
});
// LOAD URL ( here where the download url start )
String url = getIntent().getStringExtra("url");
webView.loadUrl(url);
if (savedInstanceState != null) {
webView.restoreState(savedInstanceState);
}
}
@Override
public void onBackPressed()
{
super.onBackPressed();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
webView.saveState(outState);
}
public class webviewclient extends WebViewClient {
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
view.loadData("<html><head></head><body><center></br></br></br></br></br></br></br></br><h1>OOPS! Error Occurred</br> </br>Error occuring due to internet connection Issue or server on maintanance.</br></br>Please check your internet connection and relaunch application.</br></br>Or</br></br> Try after sometime. </h1></body></html>", "text/html", null);
webview_java.this.finish();
}
@Override
public boolean shouldOverrideUrlLoading(WebView client, String request) {
client.loadUrl(request);
return true;
}
}
}
Manifest Permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" tools:node="remove"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:node="remove"/>