Существует андроид приложение с 3мя фрагментами. На одном из них есть вебвью. Идея в том, что пользователь скачивает DOCX файл и далее главный фрагмент обрабатывает его. Вопрос, Как показать пользователю прогресс скачки файла? Вот код фрагмента с WebView
private WebView webView;
public String filenameFromWeb;
Intent toInfoClass;
private DashboardViewModel dashboardViewModel;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
webView = view.findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true); // allow pinch to zooom
webView.getSettings().setDisplayZoomControls(false); // disable the default zoom controls on the page
webView.loadUrl("http://www.permaviat.ru/raspisanie-zamen/");
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(final String url, final String userAgent, String contentDisposition, String mimetype, long contentLength) {
//Checking runtime permission for devices above Marshmallow.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(getContext(),android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG, "Permission is granted");
downloadDialog(url, userAgent, contentDisposition, mimetype);
} else {
Log.v(TAG, "Permission is revoked");
//requesting permissions.
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
downloadDialog(url, userAgent, contentDisposition, mimetype);
}
} else {
//Code for devices below API 23 or Marshmallow
Log.v(TAG, "Permission is granted");
downloadDialog(url, userAgent, contentDisposition, mimetype);
}
}
});
return view;
}
public void downloadDialog(final String url,final String userAgent,String contentDisposition,String mimetype) {
//getting filename from url.
filenameFromWeb = URLUtil.guessFileName(url, contentDisposition, mimetype);
//alertdialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//title of alertdialog
builder.setTitle("Загрузка");
//message of alertdialog
builder.setMessage("Вы хотите сохранить " + filenameFromWeb);//Имя файла расписания
//if Yes button clicks.
builder.setPositiveButton("Да", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (filenameFromWeb.endsWith("docx")) {
System.out.println("Загрузка");
//DownloadManager.Request created with url.
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
//cookie
String cookie = CookieManager.getInstance().getCookie(url);
//Add cookie and User-Agent to request
request.addRequestHeader("Cookie", cookie);
request.addRequestHeader("User-Agent", userAgent);
//file scanned by MediaScannar
request.allowScanningByMediaScanner();
//Download is visible and its progress, after completion too.
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//DownloadManager created
DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(DOWNLOAD_SERVICE);
//Saving files in Download folder
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filenameFromWeb);
//download enqued
downloadManager.enqueue(request);
}
else {
Toast toast = Toast.makeText(getContext(),
"Мы работаем только с форматом docx",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
});
builder.setNegativeButton("Нет", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//cancel the dialog if Cancel clicks
dialog.cancel();
}
});
//alertdialog shows.
builder.create().show();
}