How to enable download in sketchware project

How to enable download in sketchware project

Hi there today we will learn how you can enable download permission in your sketchware made app.

You can use this code for your browser app or any app that you want to enable download feature.

For this we need the internet permission of course and the storage permission. So Let’s follow these easy steps to get started.

Step 1 : Add a webview

Go to the main activity and drag and drop a webview in your app and remember the ID of this webview. In my case the ID of my webview is webview1

If yyour webview has a different id then note the ID and change the in the step No 3

Step 2 : setup onCreate and onBackPressed

Go to the on create event and add a file block called make directory path

This will add the android.permission.WRITE_EXTERNAL_STORAGE permission.

Now go to on back pressed and setup it like this. Alternatively you can use the double tap to exit option. Read this post to enable double click to exit feature in your apo

Step 3 : setup the code using asd block

Now in your on create event add the following codes using a single add source directly block.

Asd-1 : if your app has a different webview id than webview1, then replace the ID of your own webview with webview1. Otherwise just leave it as it is.

Remember your app will download all the files in to your phone’s “download” folder.

 webview1.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading please wait...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype));
request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));

Ads-2 : Set the download toast message, when the download begins. For me I have set the download toast dialog to Downloading please wait……

You can set your own by changing the below code.

DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
showMessage("Downloading please wait....");

Asd 3 : show message when download is finished. For me it is Download complete!

BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
showMessage("Download Complete!");
unregisterReceiver(this);
}};
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
});

Now just run your project. This will enable download option in your sketchware project. Thanks for reading.

This Post Has One Comment

Leave a Reply