Method of calling system download in Android in WebView
- 2021-09-11 21:14:25
- OfStack
Preface
Recently found in the project WebView loading download page is a blank, there is no download, so simply call the system download to download it.
Process
Customize a download monitor to realize the interface of DownloadListener
class MyDownloadStart implements DownloadListener{
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
downUrl = url;
// Get the file name from the link
String dirNameString = url.substring(url.lastIndexOf("/") + 1);
// Get the size of the downloaded file
DecimalFormat decimalFormat = new DecimalFormat("0.00");
float size = contentLength;
dirName.setText(dirNameString);
if (size < 1024){
dirSize.setText(size + "B");
}else if (size < 1048576){
String dirSizeStringKB = decimalFormat.format(size / 1024);
dirSize.setText(dirSizeStringKB + "K");
}else if (size < 1073741824){
String dirSizeString = decimalFormat.format(size / 1048576);
dirSize.setText(dirSizeString + "M");
}else {
String dirStringG = decimalFormat.format(size / 1073741824);
dirSize.setText(dirStringG + "G");
}
// Displays whether to download or not dialog
downdialog.show();
}
}
Set MyDownloadStart to WebView;
mWebView.setWebViewDownListener(new MyDownloadStart());
Set Dialog, click Yes to call the system to download
DownloadManager downloadManager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downUrl));
// When downloading, display notification after downloading
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// Download path, the 1 The parameter is the folder name, the 2 The parameter is the name of the downloaded file
request.setDestinationInExternalPublicDir("SooDown",dirName.getText().toString());
request.setVisibleInDownloadsUi(true);
downloadManager.enqueue(request);
So you can download it, but we don't know when it will be finished. After downloading through DownloadManager, the system will send a broadcast. What we have to do is to receive the broadcast and process it
public class DownloadReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())){
Toast.makeText(context," Download complete ",Toast.LENGTH_SHORT).show();
}else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(intent.getAction())){
// Click on the notification bar to enter the download management page
Intent intent1 = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}
}
In the last step, don't forget to configure BroadcastReceiver
Configured in AndroidManifest. xml
<receiver android:name=".Utils.DownloadReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
<action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"/>
</intent-filter>
</receiver>
In this way, it is almost ok.